add invoice pdf
This commit is contained in:
@@ -1,23 +1,20 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:fcs/helpers/cache_mgr.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/pages/main/model/main_model.dart';
|
||||
import 'package:fcs/pages/main/util.dart';
|
||||
import 'package:fcs/pages/widgets/local_text.dart';
|
||||
import 'package:fcs/pages/widgets/progress.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;
|
||||
final String url;
|
||||
|
||||
PaymentPDFScreen({Key key, this.path}) : super(key: key);
|
||||
PaymentPDFScreen({Key key, this.url}) : super(key: key);
|
||||
|
||||
_PaymentPDFScreenState createState() => _PaymentPDFScreenState();
|
||||
}
|
||||
@@ -30,88 +27,100 @@ class _PaymentPDFScreenState extends State<PaymentPDFScreen>
|
||||
int currentPage = 0;
|
||||
bool isReady = false;
|
||||
String errorMessage = '';
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@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');
|
||||
},
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
backgroundColor: Colors.white,
|
||||
shadowColor: Colors.transparent,
|
||||
title: LocalText(context, 'invoice.pdf',
|
||||
color: Colors.white, fontSize: 20),
|
||||
leading: new IconButton(
|
||||
icon: new Icon(CupertinoIcons.back, color: primaryColor),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
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;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
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;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_share(String url) async {
|
||||
MainModel mainModel = Provider.of<MainModel>(context, listen: false);
|
||||
String appUrl = mainModel.setting.appUrl;
|
||||
_share() async {
|
||||
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",
|
||||
await Share.shareFiles([file.path],
|
||||
mimeTypes: ["application/pdf"],
|
||||
subject: "Invoice",
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user