import 'package:ConversionFlutterDemo/models/convert_contain_options_bean.dart'; import 'package:ConversionFlutterDemo/models/convert_type.dart'; import 'package:ConversionFlutterDemo/states/convert_options_provider.dart'; import 'package:ConversionFlutterDemo/widgets/options/convert_csv_options_widget.dart'; import 'package:ConversionFlutterDemo/widgets/options/convert_excel_options_widget.dart'; import 'package:ConversionFlutterDemo/widgets/options/convert_html_options_widget.dart'; import 'package:ConversionFlutterDemo/widgets/options/convert_image_options_widget.dart'; import 'package:ConversionFlutterDemo/widgets/options/convert_ppt_options_widget.dart'; import 'package:ConversionFlutterDemo/widgets/options/convert_rtf_options_widget.dart'; import 'package:ConversionFlutterDemo/widgets/options/convert_word_options_widget.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; import '../states/convert_provider.dart'; /// convert_config.dart /// /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved. /// /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. /// This notice may not be removed from this file. class ConvertConfigWidget extends StatefulWidget { const ConvertConfigWidget({Key? key}) : super(key: key); @override State createState() => _ConvertConfigWidgetState(); } class _ConvertConfigWidgetState extends State { @override Widget build(BuildContext context) { return Column( children: [ Row( children: [ const Text('ConvertType:'), Consumer( builder: (context, optionsProvider, child) { return Expanded( child: TextButton( child: Text( optionsProvider.convertType == ConvertType.none ? 'Please click to select a conversion type' : optionsProvider.convertType.name, style: const TextStyle(fontSize: 12), ), onPressed: () async { _selectConvertType(context); }, )); }), IconButton( onPressed: () { ConvertType convertType = context.read().convertType; _showConvertOptionsModalBottomSheet(context, convertType); }, icon: const Icon(Icons.settings)) ], ), SizedBox( width: double.infinity, child: ElevatedButton( onPressed: () { ConvertType convertType = context.read().convertType; if (convertType != ConvertType.none) { getPDFFileList(context); } }, child: const Text('Convert'))) ], ); } ///request storage permission Future getPermission() async { var status = await Permission.storage.request(); return status.isGranted; } ///Select pdf files from android\ios using 'file_picker' void getPDFFileList(BuildContext context) async { bool isGranted = await getPermission(); if (!isGranted) { return; } FilePickerResult? result = await FilePicker.platform.pickFiles( type: FileType.custom, allowedExtensions: ['pdf'], allowMultiple: true); if (result != null) { List pdfFiles = result.paths; ConvertOptionsBean options = context.read().getOptions(); ConvertType convertType = context.read().convertType; context .read() .addConvertData(context, convertType, options, pdfFiles); } else {} } void _showConvertOptionsModalBottomSheet(context, ConvertType convertType) { showModalBottomSheet( context: context, isScrollControlled: false, builder: (BuildContext context) { switch (convertType) { case ConvertType.ppt: return const ConvertPPTOptionsWidget(); case ConvertType.word: return const ConvertWordOptionsWidget(); case ConvertType.excel: return const ConvertExcelOptionsWidget(); case ConvertType.image: return const ConvertImageOptionsWidget(); case ConvertType.csv: return const ConvertCSVOptionsWidget(); case ConvertType.rtf: return const ConvertRTFOptionsWidget(); case ConvertType.html: return const ConvertHtmlOptionsWidget(); default: return const Padding( padding: EdgeInsets.all(16), child: Text('Options not support')); } }); } /// show support convert mime type void _selectConvertType(context) { List convertList = [ ConvertType.ppt, ConvertType.word, ConvertType.excel, ConvertType.txt, ConvertType.image, ConvertType.csv, ConvertType.rtf, ConvertType.html ]; showModalBottomSheet( context: context, builder: (BuildContext context) { return ListView.builder( itemCount: convertList.length, itemBuilder: (BuildContext context, int index) { ConvertType type = convertList[index]; return ListTile( title: Text(type.name), onTap: () { context .read() .changeConvertType(type); Navigator.of(context).pop(); }, ); }); }); } }