123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- 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<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: () {
- ConvertType convertType =
- context.read<ConvertOptionsProvider>().convertType;
- if (convertType != ConvertType.none) {
- getPDFFileList(context);
- }
- },
- child: const Text('Convert')))
- ],
- );
- }
- ///request storage permission
- Future<bool> 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<String?> pdfFiles = result.paths;
- ConvertOptionsBean options =
- context.read<ConvertOptionsProvider>().getOptions();
- ConvertType convertType =
- context.read<ConvertOptionsProvider>().convertType;
- context
- .read<ConvertProvider>()
- .addConvertData(context, convertType, options, pdfFiles);
- } else {}
- }
- 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();
- },
- );
- });
- });
- }
- }
|