1

First, thank you in advance for any advice on this question!

Okay, so I'm having an issue with updating the UI after selecting a navigation button. When I click on one of the navigation buttons I can see that the setState is getting called, both the index and the pdfView's path is getting updated to the correct file.

Here is the code:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:pdf_viewer_plugin/pdf_viewer_plugin.dart';


class PDFPreview extends StatefulWidget{
  PDFPreview(this.aContext, {this.document, this.files, Key key}) : super(key:key);
  final BuildContext aContext;
  final String document;
  final List<File> files;

  @override
  _PDFPreview createState() => _PDFPreview();
}

class _PDFPreview extends State<PDFPreview> {
  _PDFWidget pdfView;
  int _index = 0;

  @override
  Widget build(BuildContext context) {
    if(widget.document == null) {
      pdfView = new _PDFWidget(widget.files[_index].path);
      print("Path: ${widget.files[_index].path}");
      print("PDF: ${pdfView.path}");
    }else{
      pdfView = new _PDFWidget(widget.document);
    }
    return Scaffold(
      bottomNavigationBar:

      widget.document == null && widget.files.length > 1 ?
        BottomAppBar(child:
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Expanded(child: IconButton(icon: Icon(Icons.arrow_back_ios), onPressed: () {
                if(_index != 0){
                  setState(() {
                    _index--;
                  });
                  print("Current Index: $_index");
                }
                },),),
              Expanded(child: IconButton(icon: Icon(Icons.edit), onPressed: () {Navigator.pop(context, 0); },),),
              Expanded(child: Text("${_index + 1} / ${widget.files.length}", textAlign: TextAlign.center,),),
              Expanded(child: IconButton(icon: Icon(Icons.done_outline), onPressed: () { Navigator.pop(context, 1);},),),
              Expanded(child: IconButton(icon: Icon(Icons.arrow_forward_ios), onPressed: () {
                  if(_index < widget.files.length - 1) {
                    setState(() {
                      _index++;
                    });
                    print("New Current Index: $_index");
                  }
                },),),
          ],),)
        :
          BottomAppBar(child:
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Expanded(child: IconButton(icon: Icon(Icons.edit), onPressed: () {Navigator.pop(context, 0); },),),
              Expanded(child: IconButton(icon: Icon(Icons.done_outline), onPressed: () { Navigator.pop(context, 1);},),),
            ],),)
        ,
      body: pdfView
    );
  }
}

class _PDFWidget extends StatelessWidget{
  _PDFWidget(this.path);
  final String path;

  @override
  Widget build(BuildContext context) {
    return new PdfViewer(
      filePath: path,
    );
  }
}

Here is the output (Navigation Forward)

flutter: Path: {longPath}/sample.pdf
flutter: PDF: {longPath}/sample.pdf

I was wondering if anyone has any potential solutions to get the PDF view to update. Also, if someone could clear this up for me it would be much appreciated when setState completes the build function automatically gets kicked off right?

7
  • add some print('_PDFWidget path: $path') inside _PDFWidget.build method, what do you see on the logs? Commented Nov 21, 2019 at 7:09
  • So when I print out the path in the _PDFWidget it is the correct path. The new one after getting updated. Commented Nov 21, 2019 at 15:34
  • 1
    so you can only blame PdfViewer widget (whatever it is - it is not a part of public flutter SDK) Commented Nov 21, 2019 at 15:36
  • Okay so other the rest of the code is correct tho, it is just an issue with the plugin? Commented Nov 21, 2019 at 15:45
  • 1
    i think so, try just for testing to return new Text(path); inside build() method and you will see that the text changes Commented Nov 21, 2019 at 15:51

1 Answer 1

1

As mentioned in the comments by pskink, checking if the plugin was correctly working was the issue. All of the information is being changed on my side and the UI does get updated but the plugin its self is having an issue with updating the file. There has been an issue created for the plugin so as of now that is all that can be done until the plugin is fixed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.