Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
- import 'package:http/http.dart' as http;
- import 'package:path_provider/path_provider.dart';
- void main() =>
- runApp(new MaterialApp(
- home: new MTextInput()
- ));
- class MTextInput extends StatefulWidget {
- @override
- MTextInputState createState() => new MTextInputState();
- }
- List<Widget> listVdata = [];
- String searchTxt = "";
- List<String> books = [];
- List<Widget> dlbooks = [];
- List<Book> booksList = [];
- void changedSearch(String text) {
- searchTxt = Uri.encodeQueryComponent(text, encoding: UTF8);
- print(searchTxt);
- }
- class MTextInputState extends State<MTextInput> {
- _loadData() async {
- String dataURL = "http://mek.oszk.hu/kereses.mhtml?dc_creator=&dc_title=" +
- searchTxt;
- http.Response response = await http.get(dataURL);
- String html = response.body;
- print(dataURL);
- books.clear();
- booksList.clear();
- String str1 = "a href=\"http://mek.oszk.hu/";
- String str2 = "<span style=\"font-weight:bold\">";
- while (html.contains(str1)) {
- String title = html.substring(
- html.indexOf(str2) + str2.length,
- html.indexOf("<br/>", html.indexOf(str2)))
- .replaceAll(str2, "")
- .replaceAll("</span>", "");
- String code = html.substring(
- html.indexOf(str1) + str1.length,
- html.indexOf("\"", html.indexOf(str1) + str1.length));
- String imgUrl = "http://mek.oszk.hu/" + code + "/borito.jpg";
- books.add(title);
- booksList.add(new Book(title, imgUrl, code));
- print(title);
- print(imgUrl);
- print(code);
- html = html.replaceFirst(str1, "");
- html = html.replaceFirst(str1, "");
- html = html.replaceFirst(str2, "");
- html = html.replaceFirst(str2, "");
- }
- setState(() {
- listVdata = _getListData();
- });
- }
- goDownloads(List<File> files) async {
- for (int n = 0; n < files.length; n++) {
- String url = files[n].path;
- Widget container = new Container(
- child: new Row(
- children: <Widget>[
- new IconButton(
- onPressed: openBook(url),
- icon: new Icon(Icons.open_in_browser),
- ),
- new Text(
- files[n].path.substring(
- files[n].path.lastIndexOf("/") + 1)),
- ]
- )
- );
- dlbooks.add(container);
- }
- setState(() {
- Navigator.of(context).push(new MaterialPageRoute<Null>(
- builder: (BuildContext context) {
- return new Scaffold(
- appBar: new AppBar(title: new Text('My Page')),
- body: new Center(
- child: new ListView(
- children: dlbooks
- ),
- ),
- );
- },
- ));
- });
- }
- openBook(String url) {
- setState(() {
- Navigator.of(context).push(new MaterialPageRoute<Null>(
- builder: (BuildContext context) {
- return new WebviewScaffold(
- url: url,
- appBar: new AppBar(
- title: new Text("Widget webview"),
- ),
- );
- },
- ));
- });
- }
- @override
- Widget build(BuildContext context) {
- return new Scaffold(
- appBar: new AppBar(title: new Text("MEK books"),),
- body: new Container(
- child: new Center(
- child: new Column(
- children: <Widget>[
- new TextField(
- onChanged: (String str) {
- changedSearch(str);
- },
- ),
- new IconButton(
- icon: new Icon(Icons.search),
- onPressed: () {
- _loadData();
- },
- ),
- new Container(
- width: double.INFINITY, height: 400.0, child:
- new ListView(
- shrinkWrap: true,
- children: listVdata,
- ),
- )
- ]
- )
- )
- ),
- drawer:
- new Container(
- color: Colors.white,
- width: 280.0,
- child: new ListView(
- children: <Widget>[
- new DrawerHeader(child: new Text("MEK-books")),
- new ListTile(
- title: new Text("Letöltések" /*Downloads*/), onTap: () async {
- dlbooks.clear(); //resets the array
- Directory appDocDir = await getExternalStorageDirectory(); //gets internal storage path
- String appDocPath = appDocDir.path;
- Directory mekdir = new Directory(appDocPath + "/mekbooks/");
- if (!await mekdir.exists())
- mekdir.create();
- List<FileSystemEntity> files = mekdir
- .listSync(); //lists the downloaded books
- setState(() {
- goDownloads(files);
- });
- },),
- new ListTile(title: new Text("asd")),
- ],
- )
- )
- );
- }
- }
- _getListData() {
- List<Widget> widgets = [];
- for (int i = 0; i < books.length; i++) {
- widgets.add(booksList[i].getListItem());
- }
- return widgets;
- }
- class Book {
- String imgUrl;
- String title;
- String code;
- Future<File> _getLocalFile() async {
- String filename = (await listformats(code))[0];
- Directory appDocDir = await getExternalStorageDirectory();
- String appDocPath = appDocDir.path;
- print(appDocPath);
- Directory mekdir = new Directory(appDocPath + "/mekbooks/");
- if (!await mekdir.exists())
- mekdir.create();
- print(mekdir.path);
- return new File(mekdir.path + "/" + filename);
- }
- getData() async {
- String filename = (await listformats(code))[0];
- String dataURL = "http://mek.oszk.hu/" + code + "/" + filename;
- print(dataURL);
- http.Response response = await http.get(dataURL);
- return response.bodyBytes;
- }
- downloadBook() async {
- File filelocal = await _getLocalFile();
- filelocal.create();
- filelocal.openWrite();
- filelocal.writeAsBytes(await getData());
- listformats(code);
- }
- Widget getListItem() {
- return new Padding(
- padding: new EdgeInsets.all(10.0),
- child: new Row(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- new Image.network(
- imgUrl,
- ),
- new Container(
- width: 250.0,
- height: 80.0,
- child: new Column(
- children: <Widget>[
- new Container(
- width: 250.0,
- height: 30.0,
- child: new Text(title),
- ),
- new IconButton(
- icon: new Icon(Icons.file_download),
- onPressed: downloadBook)
- ],
- )
- ),
- ],
- ));
- }
- Book(this.title, this.imgUrl, this.code);
- }
- listformats(String code) async {
- List<String> dlFiles = [];
- String dataURL = "http://mek.oszk.hu/" + code;
- http.Response response = await http.get(dataURL);
- String html = response.body;
- String str1 = "class=\"bor02\"><b><a href=\"";
- while (html.contains(str1)) {
- String fileName = html.substring(
- html.indexOf(str1) + str1.length,
- html.indexOf("\"", html.indexOf(str1) + str1.length)
- );
- print(fileName);
- if (fileName != "#") {
- dlFiles.add(fileName);
- }
- html = html.replaceFirst(str1, "");
- }
- return dlFiles;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement