<p>Flutter supports opening and viewing PDF files inside your apps through URLs, assets & local storage. font-size: 1rem;](</p> <p>font-size: 1rem;](In this tutorial, we are going to see how we can launch pdf files in a flutter app using a package called **font-size: 1rem;](flutter_plugin_pdf_viewer. **</p> <p>font-size: 1rem;](</p> <p>font-size: 1rem;](This package can also be used to load & view pdf files from the local storage.</p> <p>Let's have a look at the illustration before we dive into the code.</p> <p>);</p> <p>class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'PDFViewer Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'PDFViewer Demo'), ); } }</p> <p>class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title;</p> <p>@override _MyHomePageState createState() => _MyHomePageState(); }</p> <p>class _MyHomePageState extends State { bool _isLoading = true; PDFDocument doc;</p> <p _isLoading="true;">void _loadFromAssets() async { setState(() ); doc = await PDFDocument.fromAsset('assets/sample.pdf'); setState(() ); }</p> <p _isLoading="true;">void _loadFromUrl() async { setState(() ); doc = await PDFDocument.fromURL( 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'); setState(() ); }</p> <p>@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Flexible( flex: 8, child: _isLoading ? CircularProgressIndicator() : PDFViewer( document: doc, ), ), Flexible( flex: 2, child: Padding( padding: const EdgeInsets.all(8.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ RaisedButton( color: Colors.blue, child: Text( 'Load From Assets', style: TextStyle(color: Colors.white), ), onPressed: _loadFromAssets, ), RaisedButton( color: Colors.blue, child: Text( 'Load From URL', style: TextStyle(color: Colors.white), ), onPressed: _loadFromUrl, ),</p> <pre> ], ), ), ), ], ), ), ); </pre> <p>} }</p> <p><strong>Code Overview:</strong></p> <p>We add two **RaisedButton **which are responsible for triggering two different methods _loadFromAssets & _loadFromUrl. Each of these methods updates the value fo doc variable of type <strong>PDFDocument</strong> which is actually rendered to the screen of the app.</p> <p>Do comment below if you have any doubts or questions. Happy Fluttering :)</p>
How to open a PDF file in flutter
This Article is posted by abbas.devcode at 3/18/2020 9:18:38 AM
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter
Tags: pdf file in flutter
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter