You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.9 KiB

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
class PDFScreen extends StatefulWidget {
final String path;
final int inlet;
PDFScreen({Key key, this.path, this.inlet}) : super(key: key);
_PDFScreenState createState() => _PDFScreenState();
}
class _PDFScreenState extends State<PDFScreen> with WidgetsBindingObserver {
final Completer<PDFViewController> _controller =
Completer<PDFViewController>();
int pages = 0;
int currentPage = 0;
int pageTotal = 0;
bool isReady = false;
String errorMessage = '';
@override
Widget build(BuildContext context) {
return widget.inlet == 1
? Material(
type: MaterialType.transparency,
child: Stack(
children: <Widget>[
Center(
child: Container(
height: 250,
decoration: BoxDecoration(
color: Colors.white,
),
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) {
if (total > 0) {
print('page change: $page/$total');
setState(() {
5 months ago
currentPage = page;
pageTotal = total;
});
} else {
pageTotal = 1;
}
},
),
),
),
errorMessage.isEmpty
? !isReady
? Center(
child: CircularProgressIndicator(),
)
: Container()
: Center(
child: Text(errorMessage),
),
//pdfindex显示
Positioned(
width: MediaQuery.of(context).size.width,
bottom: 45,
child: Center(
5 months ago
child: Text("${currentPage + 1}/${(pageTotal > 0)?pageTotal:1}",
style: TextStyle(color: Colors.white, fontSize: 16)),
),
),
],
),
)
: Scaffold(
body: Stack(
children: <Widget>[
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) {
if (total > 0) {
print('page change: $page/$total');
setState(() {
5 months ago
currentPage = page;
pageTotal = total;
});
} else {
pageTotal = 1;
}
},
),
errorMessage.isEmpty
? !isReady
? Center(
child: CircularProgressIndicator(),
)
: Container()
: Center(
child: Text(errorMessage),
),
//pdfindex显示
Positioned(
bottom: 45,
width: MediaQuery.of(context).size.width,
child: Center(
5 months ago
child: Text("${currentPage + 1}/${(pageTotal > 0)?pageTotal:1}",
style: TextStyle(color: Colors.black, fontSize: 16)),
),
),
Positioned(
//右上角关闭按钮
right: 10,
top: MediaQuery.of(context).padding.top,
child: IconButton(
icon: Icon(
Icons.close,
size: 30,
color: Colors.black,
),
onPressed: () {
Navigator.of(context).pop();
},
),
),
],
),
);
}
}