import 'dart:async'; import 'dart:io'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/main/model/main_model.dart'; import 'package:fcs/pages/widgets/local_text.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_pdfview/flutter_pdfview.dart'; import 'package:http/http.dart' as http; import 'package:provider/provider.dart'; import 'package:share/share.dart'; class PaymentPDFScreen extends StatefulWidget { final String path; PaymentPDFScreen({Key key, this.path}) : super(key: key); _PaymentPDFScreenState createState() => _PaymentPDFScreenState(); } class _PaymentPDFScreenState extends State with WidgetsBindingObserver { final Completer _controller = Completer(); int pages = 0; int currentPage = 0; bool isReady = false; String errorMessage = ''; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( centerTitle: true, backgroundColor: primaryColor, title: LocalText(context, 'invoice.pdf', color: Colors.white, fontSize: 20), leading: new IconButton( icon: new Icon(CupertinoIcons.back), onPressed: () { Navigator.of(context).pop(); }), actions: [ IconButton( icon: Icon(Icons.file_download), onPressed: () async { // var _dir = (await getApplicationDocumentsDirectory()).path; // _downloadFile(widget.path, 'invoice.pdf', _dir); }, ), IconButton( icon: Icon(Icons.share), onPressed: () => _share(widget.path), ), ], ), body: Stack( children: [ 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) { 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; }); }, ), ], ), ); } Future _downloadFile(String url, String filename, String dir) async { var req = await http.Client().get(Uri.parse(url)); var file = File('$dir/$filename'); return file.writeAsBytes(req.bodyBytes); } _share(String url) async { MainModel mainModel = Provider.of(context, listen: false); String appUrl = mainModel.setting.appUrl; final RenderBox box = context.findRenderObject(); await Share.share( "Join us on FCS Logistics App. Here is the link:\n $appUrl\n" + url, subject: "Invitation to FCS Logistics App", sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size); } }