118 lines
3.7 KiB
Dart
118 lines
3.7 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:dio/dio.dart';
|
|
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/services.dart';
|
|
import 'package:flutter_pdfview/flutter_pdfview.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:path_provider/path_provider.dart';
|
|
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<PaymentPDFScreen>
|
|
with WidgetsBindingObserver {
|
|
final Completer<PDFViewController> _controller =
|
|
Completer<PDFViewController>();
|
|
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: <Widget>[
|
|
IconButton(
|
|
icon: Icon(Icons.file_download),
|
|
onPressed: () async {
|
|
var file = await copyAsset();
|
|
print('file=> $file');
|
|
},
|
|
),
|
|
IconButton(
|
|
icon: Icon(Icons.share),
|
|
onPressed: () => _share(widget.path),
|
|
),
|
|
],
|
|
),
|
|
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) {
|
|
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;
|
|
});
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
_share(String url) async {
|
|
MainModel mainModel = Provider.of<MainModel>(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);
|
|
}
|
|
|
|
Future<File> copyAsset() async {
|
|
Directory tempDir = await getTemporaryDirectory();
|
|
String tempPath = tempDir.path;
|
|
File tempFile = File('$tempPath/Invoice-A092(A)-32.pdf');
|
|
ByteData bd = await rootBundle.load('assets/Invoice-A092(A)-32.pdf');
|
|
await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
|
|
return tempFile;
|
|
}
|
|
}
|