diff --git a/example/lib/main.dart b/example/lib/main.dart index 12f9761..8b15083 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -14,102 +14,86 @@ class PdfApp extends StatelessWidget { appBar: AppBar( title: const Text("pdf_flutter demo"), ), - body: const PDFListBody(), + body: PDFListBody(), )); } } class PDFListBody extends StatefulWidget { - const PDFListBody({ - Key key, - }) : super(key: key); - @override _PDFListBodyState createState() => _PDFListBodyState(); } class _PDFListBodyState extends State { - File localFile; - @override Widget build(BuildContext context) { - return Column( - children: [ - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Column( - children: [ - const Text("PDF.network(url)"), - PDF.network( - 'https://google-developer-training.github.io/android-developer-fundamentals-course-concepts/en/android-developer-fundamentals-course-concepts-en.pdf', - height: 300, - width: 150, - placeHolder: Image.asset("assets/images/pdf.png", - height: 200, width: 100), - ), - ], - ), - const SizedBox( - height: 10, - ), - Column( - children: [ - const Text("PDF.assets(assetname)"), - PDF.assets( + return Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + RaisedButton( + child: const Text("Pdf from asset"), + onPressed: () { + _navigateToPage( + title: "Pdf from asset", + child: PDF.asset( "assets/pdf/demo.pdf", - height: 300, - width: 150, placeHolder: Image.asset("assets/images/pdf.png", height: 200, width: 100), ), - ], - ), - ], - ), - const SizedBox( - height: 10, - ), - localFile != null - ? Column( - children: [ - const Text("PDF.file(fileName)"), - PDF.file( - localFile, - height: 300, - width: 200, - placeHolder: Image.asset("assets/images/pdf.png", - height: 200, width: 100), - ), - ], - ) - : InkWell( - onTap: () async { + ); + }, + ), + RaisedButton( + child: const Text("Pdf from network"), + onPressed: () { + _navigateToPage( + title: "Pdf from networkUrl", + child: PDF.network( + 'https://google-developer-training.github.io/android-developer-fundamentals-course-concepts/en/android-developer-fundamentals-course-concepts-en.pdf', + ), + ); + }, + ), + Builder( + builder: (context) { + return RaisedButton( + child: const Text("PDF from file"), + onPressed: () async { final file = await FilePicker.platform.pickFiles( allowedExtensions: ['pdf'], type: FileType.custom); - setState(() { - localFile = File(file.files[0].path); - }); - }, - child: Padding( - padding: const EdgeInsets.all(8.0), - child: Container( - height: 300, - decoration: BoxDecoration( - color: Colors.cyan, - borderRadius: BorderRadius.circular(20), - ), - child: const Center( - child: Text( - "Select PDF from device", - textAlign: TextAlign.center, - style: TextStyle(fontSize: 45, color: Colors.white), + if (file?.files[0]?.path != null) { + _navigateToPage( + title: "PDF from file", + child: PDF.file( + File(file.files[0].path), ), - ), - ), - ), - ) - ], + ); + } else { + Scaffold.of(context).showSnackBar( + const SnackBar( + content: Text("Failed to load Picked file"), + ), + ); + } + }, + ); + }, + ) + ], + ), + ); + } + + void _navigateToPage({String title, Widget child}) { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => Scaffold( + appBar: AppBar(title: Text(title)), + body: Center(child: child), + ), + ), ); } } diff --git a/lib/pdf_flutter.dart b/lib/pdf_flutter.dart index c81fe83..b54e7ff 100644 --- a/lib/pdf_flutter.dart +++ b/lib/pdf_flutter.dart @@ -15,8 +15,8 @@ class PDF extends StatefulWidget { this.file, this.networkURL, this.assetsPath, - this.width = 150, - this.height = 250, + this.width = double.maxFinite, + this.height = double.maxFinite, this.placeHolder, }); @@ -25,8 +25,8 @@ class PDF extends StatefulWidget { /// placeholder : Widget to show when pdf is loading from network. factory PDF.network( String url, { - double width = 150, - double height = 250, + double width = double.maxFinite, + double height = double.maxFinite, Widget placeHolder, }) { return PDF._( @@ -41,8 +41,8 @@ class PDF extends StatefulWidget { /// placeholder : [Widget] to show when pdf is loading from network. factory PDF.file( File file, { - double width = 150, - double height = 250, + double width = double.maxFinite, + double height = double.maxFinite, Widget placeHolder, }) { return PDF._( @@ -56,10 +56,10 @@ class PDF extends StatefulWidget { /// Load PDF from network /// assetPath : path like : `assets/pdf/demo.pdf` /// placeholder : Widget to show when pdf is loading from network. - factory PDF.assets( + factory PDF.asset( String assetPath, { - double width = 150, - double height = 250, + double width = double.maxFinite, + double height = double.maxFinite, Widget placeHolder, }) { return PDF._( @@ -117,7 +117,6 @@ class _PDFState extends State { } else if (widget.assetsPath != null) { await loadAssetPDF(); } - if (!mounted) return; setState(() {}); } @@ -167,7 +166,12 @@ class _PDFState extends State { child: Container( height: min(widget.height, widget.width), width: min(widget.height, widget.width), - child: const CircularProgressIndicator(), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const CircularProgressIndicator(), + ], + ), ), ), ),