1- import 'dart:io' ;
21import 'dart:math' ;
3-
42import 'package:file_picker/file_picker.dart' ;
53import 'package:flutter/material.dart' ;
64import 'dart:async' ;
@@ -15,7 +13,7 @@ class MyApp extends StatefulWidget {
1513}
1614
1715class _MyAppState extends State <MyApp > {
18- PDFDoc _pdfDoc;
16+ PDFDoc ? _pdfDoc;
1917 String _text = "" ;
2018
2119 bool _buttonsEnabled = true ;
@@ -37,38 +35,41 @@ class _MyAppState extends State<MyApp> {
3735 padding: EdgeInsets .all (10 ),
3836 child: ListView (
3937 children: < Widget > [
40- FlatButton (
38+ TextButton (
4139 child: Text (
4240 "Pick PDF document" ,
4341 style: TextStyle (color: Colors .white),
4442 ),
45- color: Colors .blueAccent,
43+ style: TextButton .styleFrom (
44+ padding: EdgeInsets .all (5 ),
45+ backgroundColor: Colors .blueAccent),
4646 onPressed: _pickPDFText,
47- padding: EdgeInsets .all (5 ),
4847 ),
49- FlatButton (
48+ TextButton (
5049 child: Text (
5150 "Read random page" ,
5251 style: TextStyle (color: Colors .white),
5352 ),
54- color: Colors .blueAccent,
53+ style: TextButton .styleFrom (
54+ padding: EdgeInsets .all (5 ),
55+ backgroundColor: Colors .blueAccent),
5556 onPressed: _buttonsEnabled ? _readRandomPage : () {},
56- padding: EdgeInsets .all (5 ),
5757 ),
58- FlatButton (
58+ TextButton (
5959 child: Text (
6060 "Read whole document" ,
6161 style: TextStyle (color: Colors .white),
6262 ),
63- color: Colors .blueAccent,
63+ style: TextButton .styleFrom (
64+ padding: EdgeInsets .all (5 ),
65+ backgroundColor: Colors .blueAccent),
6466 onPressed: _buttonsEnabled ? _readWholeDoc : () {},
65- padding: EdgeInsets .all (5 ),
6667 ),
6768 Padding (
6869 child: Text (
6970 _pdfDoc == null
7071 ? "Pick a new PDF document and wait for it to load..."
71- : "PDF document loaded, ${_pdfDoc .length } pages\n " ,
72+ : "PDF document loaded, ${_pdfDoc ! .length } pages\n " ,
7273 style: TextStyle (fontSize: 18 ),
7374 textAlign: TextAlign .center,
7475 ),
@@ -91,9 +92,11 @@ class _MyAppState extends State<MyApp> {
9192
9293 /// Picks a new PDF document from the device
9394 Future _pickPDFText () async {
94- File file = await FilePicker .getFile ();
95- _pdfDoc = await PDFDoc .fromFile (file);
96- setState (() {});
95+ var filePickerResult = await FilePicker .platform.pickFiles ();
96+ if (filePickerResult != null ) {
97+ _pdfDoc = await PDFDoc .fromPath (filePickerResult.files.single.path! );
98+ setState (() {});
99+ }
97100 }
98101
99102 /// Reads a random page of the document
@@ -106,7 +109,7 @@ class _MyAppState extends State<MyApp> {
106109 });
107110
108111 String text =
109- await _pdfDoc.pageAt (Random ().nextInt (_pdfDoc.length) + 1 ).text;
112+ await _pdfDoc! .pageAt (Random ().nextInt (_pdfDoc! .length) + 1 ).text;
110113
111114 setState (() {
112115 _text = text;
@@ -123,7 +126,7 @@ class _MyAppState extends State<MyApp> {
123126 _buttonsEnabled = false ;
124127 });
125128
126- String text = await _pdfDoc.text;
129+ String text = await _pdfDoc! .text;
127130
128131 setState (() {
129132 _text = text;
0 commit comments