123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /// 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<ConvertConfigWidget> createState() => _ConvertConfigWidgetState();
- }
- class _ConvertConfigWidgetState extends State<ConvertConfigWidget> {
- @override
- Widget build(BuildContext context) {
- return Column(
- children: [
- Row(
- children: [
- const Text('ConvertType:'),
- Consumer<ConvertOptionsProvider>(
- 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<ConvertOptionsProvider>().convertType;
- _showConvertOptionsModalBottomSheet(context, convertType);
- },
- icon: const Icon(Icons.settings))
- ],
- ),
- SizedBox(
- width: double.infinity,
- child: ElevatedButton(
- onPressed: () async {
- ConvertType convertType =
- context.read<ConvertOptionsProvider>().convertType;
- if (convertType != ConvertType.none) {
- Options options =
- context.read<ConvertOptionsProvider>().getOptions();
- List<String> files = await getPDFFileList(context);
- var outputPath = await context
- .read<ConvertTaskQueueProvider>()
- .getOutputDir();
- context.read<ConvertTaskQueueProvider>().addTask(
- convertType, options, files, outputPath ?? "");
- }
- },
- child: const Text('Convert')))
- ],
- );
- }
- ///request storage permission
- Future<bool> 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<List<String>> 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<String?> pdfFiles = result.paths;
- var files = pdfFiles.where((element) => element != null);
- return List.from(files);
- } else {
- return List.empty();
- }
- }
- void _showConvertOptionsModalBottomSheet(context, ConvertType convertType) {
- showModalBottomSheet<int>(
- 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<ConvertType> 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<ConvertOptionsProvider>()
- .changeConvertType(type);
- Navigator.of(context).pop();
- },
- );
- });
- });
- }
- }
|