Advertisement
Guest User

main.dart

a guest
Mar 5th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 7.98 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4.  
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
  7. import 'package:http/http.dart' as http;
  8. import 'package:path_provider/path_provider.dart';
  9.  
  10. void main() =>
  11.     runApp(new MaterialApp(
  12.         home: new MTextInput()
  13.     ));
  14.  
  15. class MTextInput extends StatefulWidget {
  16.   @override
  17.   MTextInputState createState() => new MTextInputState();
  18. }
  19.  
  20. List<Widget> listVdata = [];
  21. String searchTxt = "";
  22. List<String> books = [];
  23. List<Widget> dlbooks = [];
  24. List<Book> booksList = [];
  25.  
  26. void changedSearch(String text) {
  27.   searchTxt = Uri.encodeQueryComponent(text, encoding: UTF8);
  28.   print(searchTxt);
  29. }
  30.  
  31. class MTextInputState extends State<MTextInput> {
  32.  
  33.   _loadData() async {
  34.     String dataURL = "http://mek.oszk.hu/kereses.mhtml?dc_creator=&dc_title=" +
  35.         searchTxt;
  36.     http.Response response = await http.get(dataURL);
  37.     String html = response.body;
  38.     print(dataURL);
  39.     books.clear();
  40.     booksList.clear();
  41.     String str1 = "a href=\"http://mek.oszk.hu/";
  42.     String str2 = "<span style=\"font-weight:bold\">";
  43.     while (html.contains(str1)) {
  44.       String title = html.substring(
  45.           html.indexOf(str2) + str2.length,
  46.           html.indexOf("<br/>", html.indexOf(str2)))
  47.           .replaceAll(str2, "")
  48.           .replaceAll("</span>", "");
  49.       String code = html.substring(
  50.           html.indexOf(str1) + str1.length,
  51.           html.indexOf("\"", html.indexOf(str1) + str1.length));
  52.       String imgUrl = "http://mek.oszk.hu/" + code + "/borito.jpg";
  53.       books.add(title);
  54.       booksList.add(new Book(title, imgUrl, code));
  55.       print(title);
  56.       print(imgUrl);
  57.       print(code);
  58.       html = html.replaceFirst(str1, "");
  59.       html = html.replaceFirst(str1, "");
  60.       html = html.replaceFirst(str2, "");
  61.       html = html.replaceFirst(str2, "");
  62.     }
  63.     setState(() {
  64.       listVdata = _getListData();
  65.     });
  66.   }
  67.  
  68.   goDownloads(List<File> files) async {
  69.     for (int n = 0; n < files.length; n++) {
  70.       String url = files[n].path;
  71.       Widget container = new Container(
  72.           child: new Row(
  73.               children: <Widget>[
  74.                 new IconButton(
  75.                   onPressed: openBook(url),
  76.                   icon: new Icon(Icons.open_in_browser),
  77.                 ),
  78.                 new Text(
  79.                     files[n].path.substring(
  80.                         files[n].path.lastIndexOf("/") + 1)),
  81.               ]
  82.           )
  83.       );
  84.       dlbooks.add(container);
  85.     }
  86.     setState(() {
  87.       Navigator.of(context).push(new MaterialPageRoute<Null>(
  88.         builder: (BuildContext context) {
  89.           return new Scaffold(
  90.             appBar: new AppBar(title: new Text('My Page')),
  91.             body: new Center(
  92.               child: new ListView(
  93.                   children: dlbooks
  94.               ),
  95.             ),
  96.           );
  97.         },
  98.       ));
  99.     });
  100.   }
  101.  
  102.   openBook(String url) {
  103.     setState(() {
  104.       Navigator.of(context).push(new MaterialPageRoute<Null>(
  105.         builder: (BuildContext context) {
  106.           return new WebviewScaffold(
  107.             url: url,
  108.             appBar: new AppBar(
  109.               title: new Text("Widget webview"),
  110.             ),
  111.           );
  112.         },
  113.       ));
  114.     });
  115.   }
  116.  
  117.   @override
  118.   Widget build(BuildContext context) {
  119.     return new Scaffold(
  120.         appBar: new AppBar(title: new Text("MEK books"),),
  121.         body: new Container(
  122.             child: new Center(
  123.                 child: new Column(
  124.                     children: <Widget>[
  125.                       new TextField(
  126.                         onChanged: (String str) {
  127.                           changedSearch(str);
  128.                         },
  129.                       ),
  130.                       new IconButton(
  131.                         icon: new Icon(Icons.search),
  132.                         onPressed: () {
  133.                           _loadData();
  134.                         },
  135.                       ),
  136.                       new Container(
  137.                         width: double.INFINITY, height: 400.0, child:
  138.                       new ListView(
  139.                         shrinkWrap: true,
  140.                         children: listVdata,
  141.                       ),
  142.                       )
  143.                     ]
  144.                 )
  145.             )
  146.         ),
  147.         drawer:
  148.         new Container(
  149.             color: Colors.white,
  150.             width: 280.0,
  151.             child: new ListView(
  152.               children: <Widget>[
  153.                 new DrawerHeader(child: new Text("MEK-books")),
  154.                 new ListTile(
  155.                   title: new Text("Letöltések" /*Downloads*/), onTap: () async {
  156.                   dlbooks.clear(); //resets the array
  157.                   Directory appDocDir = await getExternalStorageDirectory(); //gets internal storage path
  158.                   String appDocPath = appDocDir.path;
  159.                   Directory mekdir = new Directory(appDocPath + "/mekbooks/");
  160.                   if (!await mekdir.exists())
  161.                     mekdir.create();
  162.                   List<FileSystemEntity> files = mekdir
  163.                       .listSync(); //lists the downloaded books
  164.  
  165.                   setState(() {
  166.                     goDownloads(files);
  167.                   });
  168.                 },),
  169.                 new ListTile(title: new Text("asd")),
  170.               ],
  171.             )
  172.         )
  173.     );
  174.   }
  175. }
  176.  
  177. _getListData() {
  178.   List<Widget> widgets = [];
  179.   for (int i = 0; i < books.length; i++) {
  180.     widgets.add(booksList[i].getListItem());
  181.   }
  182.   return widgets;
  183. }
  184.  
  185. class Book {
  186.   String imgUrl;
  187.   String title;
  188.   String code;
  189.  
  190.   Future<File> _getLocalFile() async {
  191.     String filename = (await listformats(code))[0];
  192.     Directory appDocDir = await getExternalStorageDirectory();
  193.     String appDocPath = appDocDir.path;
  194.     print(appDocPath);
  195.     Directory mekdir = new Directory(appDocPath + "/mekbooks/");
  196.     if (!await mekdir.exists())
  197.       mekdir.create();
  198.     print(mekdir.path);
  199.     return new File(mekdir.path + "/" + filename);
  200.   }
  201.  
  202.   getData() async {
  203.     String filename = (await listformats(code))[0];
  204.     String dataURL = "http://mek.oszk.hu/" + code + "/" + filename;
  205.     print(dataURL);
  206.     http.Response response = await http.get(dataURL);
  207.     return response.bodyBytes;
  208.   }
  209.  
  210.   downloadBook() async {
  211.     File filelocal = await _getLocalFile();
  212.     filelocal.create();
  213.     filelocal.openWrite();
  214.     filelocal.writeAsBytes(await getData());
  215.     listformats(code);
  216.   }
  217.  
  218.   Widget getListItem() {
  219.     return new Padding(
  220.         padding: new EdgeInsets.all(10.0),
  221.         child: new Row(
  222.           crossAxisAlignment: CrossAxisAlignment.start,
  223.           children: <Widget>[
  224.             new Image.network(
  225.               imgUrl,
  226.             ),
  227.             new Container(
  228.                 width: 250.0,
  229.                 height: 80.0,
  230.                 child: new Column(
  231.                   children: <Widget>[
  232.                     new Container(
  233.                       width: 250.0,
  234.                       height: 30.0,
  235.                       child: new Text(title),
  236.                     ),
  237.                     new IconButton(
  238.                         icon: new Icon(Icons.file_download),
  239.                         onPressed: downloadBook)
  240.                   ],
  241.                 )
  242.             ),
  243.  
  244.           ],
  245.         ));
  246.   }
  247.  
  248.   Book(this.title, this.imgUrl, this.code);
  249. }
  250.  
  251. listformats(String code) async {
  252.   List<String> dlFiles = [];
  253.   String dataURL = "http://mek.oszk.hu/" + code;
  254.   http.Response response = await http.get(dataURL);
  255.   String html = response.body;
  256.   String str1 = "class=\"bor02\"><b><a href=\"";
  257.   while (html.contains(str1)) {
  258.     String fileName = html.substring(
  259.         html.indexOf(str1) + str1.length,
  260.         html.indexOf("\"", html.indexOf(str1) + str1.length)
  261.     );
  262.     print(fileName);
  263.     if (fileName != "#") {
  264.       dlFiles.add(fileName);
  265.     }
  266.     html = html.replaceFirst(str1, "");
  267.   }
  268.   return dlFiles;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement