scientifichackers / flutter_pdf_viewer

A native PDF viewer for flutter.
MIT License
107 stars 40 forks source link

How to close correctly a screen with PdfViewer? #18

Closed d-apps closed 5 years ago

d-apps commented 5 years ago

I have a method to close the screen and to go back to previous screen but when I press back button I get a blackscreen and I this log in Run: D/jniPdfium( 7951): Destroy FPDF library.

I need to press back twice to go back to previous screen in my app, could you please help me with that?

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);
    }

    PdfViewer.loadFile(path);

  }

  Future<bool> _onBackPressed(){

    Navigator.pop(context);
    return Future.value(true);

  }

  @override
  Widget build(BuildContext context) {

      return WillPopScope(
        onWillPop: _onBackPressed,
        child: Container(),
      );
    }
  }
}
devxpy commented 5 years ago

You don't need to create a new screen. Just launch the PDF from the onPressed callback in the last widget (that navigates to PdfScreen).

You can view the example here.

devxpy commented 5 years ago

Going to close this, reopen if not solved.

d-apps commented 5 years ago

Problem was solved, thank you.