/// convert_options_widget.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. import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:kmpdfkit_conversion_flutter/models/convert_type.dart'; import 'package:kmpdfkit_conversion_flutter/models/options/options.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; import '../states/convert_options_provider.dart'; import '../states/convert_task_queue_provider.dart'; import 'options/convert_csv_options_widget.dart'; import 'options/convert_excel_options_widget.dart'; import 'options/convert_html_options_widget.dart'; import 'options/convert_image_options_widget.dart'; import 'options/convert_ppt_options_widget.dart'; import 'options/convert_rtf_options_widget.dart'; import 'options/convert_word_options_widget.dart'; 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: () async { ConvertType convertType = context.read().convertType; if (convertType != ConvertType.none) { Options options = context.read().getOptions(); List files = await getPDFFileList(context); var outputPath = await context .read() .getOutputDir(); context.read().addTask( convertType, options, files, outputPath ?? ""); } }, child: const Text('Convert'))) ], ); } ///request storage permission Future getPermission() async { var status = await Permission.storage.request(); var manageStatus = await Permission.manageExternalStorage.request(); return status.isGranted && manageStatus.isGranted; } ///Select pdf files from android\ios using 'file_picker' Future> getPDFFileList(BuildContext context) async { bool isGranted = await getPermission(); if (!isGranted) { return List.empty(); } FilePickerResult? result = await FilePicker.platform.pickFiles( type: FileType.custom, allowedExtensions: ['pdf'], allowMultiple: true); if (result != null) { List pdfFiles = result.paths; var files = pdfFiles.where((element) => element != null); return List.from(files); } else { return List.empty(); } } 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(); }, ); }); }); } }