【发布时间】:2020-01-18 14:55:53
【问题描述】:
我正在尝试创建一个页面,该页面在打开页面时显示来自资产的 PDF。 PDF 加载,但在每次加载之前都会在标题中引发错误。我很确定这与函数的调用方式有关,但我不知道如何解决。
我的代码示例:
class Calendar2020PDFScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => _Calendar2020PDFScreenState();
}
class _Calendar2020PDFScreenState extends State<Calendar2020PDFScreen> {
bool _isLoading = false, _isInit = true;
PDFDocument document;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
leading: new IconButton(
icon: new Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.of(context).pop(),
),
title: Center(
child: Text(
'MCHD',
style: TextStyle(color: Colors.white),
)),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {
showSearch(context: context, delegate: DataSearch());
},
) //IconButton
], //,Widget>[]
), //Appbar
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: _isInit
? loadFromAssets() // Executes loadFromAssets function if _isInit is true
: _isLoading
? Center(
child:
CircularProgressIndicator(), //Shows indicator if _isLoading is true
) //Center
: PDFViewer(
document: document,
), //PDFViewer
), //Center
), //Expanded
], //<Widget>[]
), //Column
), //Scaffold
); //MaterialApp
}
Future<PDFDocument> loadFromAssets() async {
try {
setState(() {
_isInit = false; //remove text
_isLoading = true; //show loading
});
document = await PDFDocument.fromAsset("assets/PDFs/calendars_2020.pdf");
setState(() {
_isLoading = false; //remove loading
});
return document;
} catch (err) {
print('Caught error: $err');
} //catch
} //Future
}
【问题讨论】: