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?
print('_PDFWidget path: $path')inside_PDFWidget.buildmethod, what do you see on the logs?PdfViewerwidget (whatever it is - it is not a part of public flutter SDK)return new Text(path);insidebuild()method and you will see that the text changes