简体   繁体   中英

How to use this library to show a PDF when this screen is opened in Flutter

I have a pdf_screen.dart file that will show a PDF directly when opened, does anyone can explain me how to use the functions of this library called flutter pdf viewer just to show my pdf?

Flutter PDF Viewer Library Example

    class _PdfScreenState extends State<PdfScreen> {

      final DocumentSnapshot document;
      String path;
      var dir;

      _PdfScreenState(this.document);


      @override
      void initState() {
        super.initState();
        getPdf();
      }

      Future<void> getPdf() async {
        try {
          dir = await getApplicationDocumentsDirectory();
          setState(() {
            path = "${dir.path}/${document["title"]}.pdf";
          });
        } catch (e) {
          print(e);
        }
      }


      @override
      Widget build(BuildContext context) {

        if (path == null) {


   print("loading");
      return Container(
        color: Colors.white,
        child: Center(
          child: CircularProgressIndicator(backgroundColor: Colors.blueAccent,
          ),
        ),
      );
    } else {

      return Container(
             child: ??; // I need to show my PDF here, I need to pass the path 
                       // variable but PdfViewer is a function and it doesn't 
                       // return a widget, how to implement?
    }
  }
}

As the readme says, this library launches a new intent, so it replaces your Flutter app in the stack until you use the back button. The example is clear how to achieve this.

However, as the readme continues, a proof-of-concept is available allowing you to render the PDF inside your Flutter app. Check out the branch from github to explore more.

A proof of concept for inline PDFs is available at the inline branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM