添加flutter_pdfview: ^1.2.1 组件
class PDFScreenextends StatefulWidget {
final Stringurl;
final Stringpath;
final Stringtitle;
PDFScreen({Key key,this.url, this.path, this.title}) :super(key: key);
_PDFScreenStatecreateState() =>_PDFScreenState();
}
class _PDFScreenStateextends Statewith WidgetsBindingObserver {
final Completer_controller =
Completer();
intpages =0;
intcurrentPage =0;
boolisReady =false;
StringerrorMessage ='';
@override
Widgetbuild(BuildContext context) {
return Scaffold(
appBar:AppBar(
elevation:0,
leading:new IconButton(
icon:Image.asset(
Utils.getImgPath('icon_back'),
width:18,
height:36,
),
onPressed: () {
Navigator.of(context).pop();
},
),
centerTitle:true,
title:Text(
widget.title,
style:TextStyle(fontSize:17.0),
),
),
body:Stack(
children: [
Positioned(
height: MediaQuery.of(context).size.height - (Utils.getHeightSize(80, context) *2),
width: MediaQuery.of(context).size.width,
child:PDFView(
filePath:widget.path,
enableSwipe:true,
swipeHorizontal:true,
autoSpacing:false,
pageFling:true,
pageSnap:true,
defaultPage:currentPage,
fitPolicy: FitPolicy.BOTH,
preventLinkNavigation:
false, // if set to true the link is handled in flutter
onRender: (_pages) {
setState(() {
pages = _pages;
isReady =true;
});
},
onError: (error) {
setState(() {
errorMessage = error.toString();
});
print(error.toString());
},
onPageError: (page, error) {
setState(() {
errorMessage ='$page: ${error.toString()}';
});
print('$page: ${error.toString()}');
},
onViewCreated: (PDFViewController pdfViewController) {
_controller.complete(pdfViewController);
},
onLinkHandler: (String uri) {
print('goto uri: $uri');
},
onPageChanged: (int page, int total) {
print('page change: $page/$total');
setState(() {
currentPage = page;
});
},
),
),
Positioned(
bottom:0,
height: Utils.getHeightSize(80, context),
width: MediaQuery.of(context).size.width,
child:Container(
// padding: EdgeInsets.only(left: 10.0, right: 10.0,top: 10.0,bottom: 10.0),
decoration:BoxDecoration(
color: Colors.white,
border:Border.all(color: AppColors.shadeGary),
boxShadow: [
//refer to :https://ninghao.net/video/6443
BoxShadow(
color: AppColors.shadeGary,
offset:Offset(0.0, 0.0),
blurRadius:3.0,
spreadRadius:0.0),
],
),
child:Stack(
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(),
Expanded(child:SizedBox()),
Container(
height:42.0,
width: Utils.getWidthSize(90, context),
margin:EdgeInsets.only(right:20.0,bottom:5.0),
decoration:BoxDecoration(//边框线
borderRadius:BorderRadius.circular(21.0), //圆角
gradient:LinearGradient(
colors: [Color(0xFF5FD27A), Color(0xFF3FAF6F)],
),
),
child:TextButton(
style:ButtonStyle(
overlayColor: MaterialStateProperty.all(Colors.transparent),
foregroundColor: MaterialStateProperty.resolveWith(
(states) {
if (states.contains(MaterialState.pressed)) {
//按下时的颜色
return Colors.transparent;
}
//默认状态使用灰色
return Colors.transparent;
},
),
),
child:Text(
globalTranslations.text("msg_download"),
style:TextStyle(color: Colors.white),
),
onPressed: () {
launchPdfURL(widget.url);
},
),
),
],
),
],
),
)),
errorMessage.isEmpty
? !isReady
?Center(
child:CircularProgressIndicator(),
)
:Container()
:Center(
child:Text(errorMessage),
)
],
),
// floatingActionButton: FutureBuilder(
// future: _controller.future,
// builder: (context, AsyncSnapshot snapshot) {
// if (snapshot.hasData) {
// return FloatingActionButton.extended(
// label: Text("Go to ${pages ~/ 2}"),
// onPressed: () async {
// await snapshot.data.setPage(pages ~/ 2);
// },
// );
// }
//
// return Container();
// },
// ),
);
}
launchPdfURL(String url) {
launch(url);
}
}