Files
fcs/lib/pages/widgets/pdf_screen.dart

126 lines
3.7 KiB
Dart
Raw Normal View History

2020-06-24 16:06:15 +06:30
import 'dart:async';
2020-10-16 21:38:39 +06:30
import 'dart:io';
2020-06-24 16:06:15 +06:30
2020-10-26 04:41:24 +06:30
import 'package:fcs/helpers/cache_mgr.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/helpers/theme.dart';
2020-10-26 04:41:24 +06:30
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/progress.dart';
2020-06-24 16:06:15 +06:30
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
2020-10-16 21:38:39 +06:30
import 'package:share/share.dart';
2020-06-24 16:06:15 +06:30
2020-10-28 05:11:06 +06:30
class PDFScreen extends StatefulWidget {
final String title;
2020-10-26 04:41:24 +06:30
final String url;
2020-06-24 16:06:15 +06:30
2020-10-28 05:11:06 +06:30
PDFScreen({Key key, this.url, this.title}) : super(key: key);
2020-06-24 16:06:15 +06:30
2020-10-28 05:11:06 +06:30
_PDFScreenState createState() => _PDFScreenState();
2020-06-24 16:06:15 +06:30
}
2020-10-28 05:11:06 +06:30
class _PDFScreenState extends State<PDFScreen> with WidgetsBindingObserver {
2020-06-24 16:06:15 +06:30
final Completer<PDFViewController> _controller =
Completer<PDFViewController>();
int pages = 0;
int currentPage = 0;
bool isReady = false;
String errorMessage = '';
2020-10-26 04:41:24 +06:30
bool _isLoading = true;
void initState() {
super.initState();
download();
}
File file;
Future<void> download() async {
try {
File f = await PdfCacheMgr.pdfs.getSingleFile(widget.url);
setState(() {
file = f;
});
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
2020-06-24 16:06:15 +06:30
@override
Widget build(BuildContext context) {
2020-10-26 04:41:24 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: Colors.white,
shadowColor: Colors.transparent,
2020-10-28 05:11:06 +06:30
title:
Text(widget.title ?? "", style: TextStyle(color: primaryColor)),
2020-10-26 04:41:24 +06:30
leading: new IconButton(
icon: new Icon(CupertinoIcons.back, color: primaryColor),
onPressed: () => Navigator.of(context).pop(),
2020-06-24 16:06:15 +06:30
),
2020-10-26 04:41:24 +06:30
actions: <Widget>[
IconButton(
icon: Icon(
Icons.share,
color: primaryColor,
),
onPressed: _share,
),
],
),
body: Stack(
children: <Widget>[
_isLoading
? Container()
: PDFView(
filePath: file?.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) {
print(('pages => $pages'));
setState(() {
pages = _pages;
isReady = true;
});
},
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;
});
},
),
],
),
2020-06-24 16:06:15 +06:30
),
);
}
2020-10-16 21:38:39 +06:30
2020-10-26 04:41:24 +06:30
_share() async {
2020-10-16 21:38:39 +06:30
final RenderBox box = context.findRenderObject();
2020-10-26 04:41:24 +06:30
await Share.shareFiles([file.path],
mimeTypes: ["application/pdf"],
2020-10-28 05:11:06 +06:30
subject: "File",
2020-10-16 21:38:39 +06:30
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
}
2020-06-24 16:06:15 +06:30
}