|
@@ -0,0 +1,147 @@
|
|
|
+/*
|
|
|
+ * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
|
|
|
+ *
|
|
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
+ * you may not use this file except in compliance with the License.
|
|
|
+ * You may obtain a copy of the License at
|
|
|
+ *
|
|
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
+ *
|
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+ * See the License for the specific language governing permissions and
|
|
|
+ * limitations under the License.
|
|
|
+ */
|
|
|
+
|
|
|
+import 'dart:async';
|
|
|
+import 'dart:io';
|
|
|
+
|
|
|
+import 'package:fis_lib_pdf/reportRender.dart';
|
|
|
+import 'package:flutter/foundation.dart';
|
|
|
+import 'package:flutter/material.dart';
|
|
|
+import 'package:open_file/open_file.dart';
|
|
|
+import 'package:path_provider/path_provider.dart';
|
|
|
+
|
|
|
+import 'package:pdf/pdf.dart';
|
|
|
+import 'package:pdf/widgets.dart' as pw;
|
|
|
+import 'package:printing/printing.dart';
|
|
|
+import 'package:fis_lib_pdf/ultils/data.dart';
|
|
|
+
|
|
|
+class PDFRenderContainer extends StatefulWidget {
|
|
|
+ const PDFRenderContainer({Key? key}) : super(key: key);
|
|
|
+
|
|
|
+ @override
|
|
|
+ PDFRenderContainerState createState() {
|
|
|
+ return PDFRenderContainerState();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class PDFRenderContainerState extends State<PDFRenderContainer>
|
|
|
+ with SingleTickerProviderStateMixin {
|
|
|
+ int _tab = 0;
|
|
|
+ TabController? _tabController;
|
|
|
+
|
|
|
+ PrintingInfo? printingInfo;
|
|
|
+ var _data = const CustomData();
|
|
|
+
|
|
|
+ @override
|
|
|
+ void initState() {
|
|
|
+ super.initState();
|
|
|
+ _init();
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ void didChangeDependencies() {
|
|
|
+ super.didChangeDependencies();
|
|
|
+ }
|
|
|
+
|
|
|
+ Future<void> _init() async {
|
|
|
+ final info = await Printing.info();
|
|
|
+
|
|
|
+ _tabController = TabController(
|
|
|
+ vsync: this,
|
|
|
+ length: 1,
|
|
|
+ initialIndex: _tab,
|
|
|
+ );
|
|
|
+ _tabController!.addListener(() {
|
|
|
+ if (_tab != _tabController!.index) {
|
|
|
+ setState(() {
|
|
|
+ _tab = _tabController!.index;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ setState(() {
|
|
|
+ printingInfo = info;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ void _showPrintedToast(BuildContext context) {
|
|
|
+ ScaffoldMessenger.of(context).showSnackBar(
|
|
|
+ const SnackBar(
|
|
|
+ content: Text('Document printed successfully'),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ void _showSharedToast(BuildContext context) {
|
|
|
+ ScaffoldMessenger.of(context).showSnackBar(
|
|
|
+ const SnackBar(
|
|
|
+ content: Text('Document shared successfully'),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ double? _scale = 1060;
|
|
|
+ Future<void> _saveAsFile(
|
|
|
+ BuildContext context,
|
|
|
+ LayoutCallback build,
|
|
|
+ PdfPageFormat pageFormat,
|
|
|
+ ) async {
|
|
|
+ final bytes = await build(pageFormat);
|
|
|
+
|
|
|
+ final appDocDir = await getApplicationDocumentsDirectory();
|
|
|
+ final appDocPath = appDocDir.path;
|
|
|
+ final file = File(appDocPath + '/' + 'document.pdf');
|
|
|
+ print('Save as file ${file.path} ...');
|
|
|
+ await file.writeAsBytes(bytes);
|
|
|
+ await OpenFile.open(file.path);
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ Widget build(BuildContext context) {
|
|
|
+ pw.RichText.debug = true;
|
|
|
+
|
|
|
+ if (_tabController == null) {
|
|
|
+ return const Center(child: CircularProgressIndicator());
|
|
|
+ }
|
|
|
+
|
|
|
+ final actions = <PdfPreviewAction>[
|
|
|
+ if (!kIsWeb)
|
|
|
+ PdfPreviewAction(
|
|
|
+ icon: const Icon(Icons.save),
|
|
|
+ onPressed: _saveAsFile,
|
|
|
+ )
|
|
|
+ ];
|
|
|
+
|
|
|
+ return Scaffold(
|
|
|
+ body: PdfPreview(
|
|
|
+ maxPageWidth: _scale,
|
|
|
+ onZoom: _onZoomInOrOut,
|
|
|
+ build: (format) => generateReport(format, _data),
|
|
|
+ actions: actions,
|
|
|
+ onPrinted: _showPrintedToast,
|
|
|
+ onShared: _showSharedToast,
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ void _onZoomInOrOut(PdfZoomMode mode) {
|
|
|
+ final newScale =
|
|
|
+ mode == PdfZoomMode.PdfZoomIn ? _scale! + 100.0 : _scale! - 100.0;
|
|
|
+ setState(() {
|
|
|
+ _scale = newScale;
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|