convert_config.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import 'package:ConversionFlutterDemo/models/convert_contain_options_bean.dart';
  2. import 'package:ConversionFlutterDemo/models/convert_type.dart';
  3. import 'package:ConversionFlutterDemo/states/convert_options_provider.dart';
  4. import 'package:ConversionFlutterDemo/widgets/options/convert_csv_options_widget.dart';
  5. import 'package:ConversionFlutterDemo/widgets/options/convert_excel_options_widget.dart';
  6. import 'package:ConversionFlutterDemo/widgets/options/convert_html_options_widget.dart';
  7. import 'package:ConversionFlutterDemo/widgets/options/convert_image_options_widget.dart';
  8. import 'package:ConversionFlutterDemo/widgets/options/convert_ppt_options_widget.dart';
  9. import 'package:ConversionFlutterDemo/widgets/options/convert_rtf_options_widget.dart';
  10. import 'package:ConversionFlutterDemo/widgets/options/convert_word_options_widget.dart';
  11. import 'package:file_picker/file_picker.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:permission_handler/permission_handler.dart';
  14. import 'package:provider/provider.dart';
  15. import '../states/convert_provider.dart';
  16. /// convert_config.dart
  17. ///
  18. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  19. ///
  20. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  21. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  22. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  23. /// This notice may not be removed from this file.
  24. class ConvertConfigWidget extends StatefulWidget {
  25. const ConvertConfigWidget({Key? key}) : super(key: key);
  26. @override
  27. State<ConvertConfigWidget> createState() => _ConvertConfigWidgetState();
  28. }
  29. class _ConvertConfigWidgetState extends State<ConvertConfigWidget> {
  30. @override
  31. Widget build(BuildContext context) {
  32. return Column(
  33. children: [
  34. Row(
  35. children: [
  36. const Text('ConvertType:'),
  37. Consumer<ConvertOptionsProvider>(
  38. builder: (context, optionsProvider, child) {
  39. return Expanded(
  40. child: TextButton(
  41. child: Text(
  42. optionsProvider.convertType == ConvertType.none
  43. ? 'Please click to select a conversion type'
  44. : optionsProvider.convertType.name,
  45. style: const TextStyle(fontSize: 12),
  46. ),
  47. onPressed: () async {
  48. _selectConvertType(context);
  49. },
  50. ));
  51. }),
  52. IconButton(
  53. onPressed: () {
  54. ConvertType convertType =
  55. context.read<ConvertOptionsProvider>().convertType;
  56. _showConvertOptionsModalBottomSheet(context, convertType);
  57. },
  58. icon: const Icon(Icons.settings))
  59. ],
  60. ),
  61. SizedBox(
  62. width: double.infinity,
  63. child: ElevatedButton(
  64. onPressed: () {
  65. ConvertType convertType =
  66. context.read<ConvertOptionsProvider>().convertType;
  67. if (convertType != ConvertType.none) {
  68. getPDFFileList(context);
  69. }
  70. },
  71. child: const Text('Convert')))
  72. ],
  73. );
  74. }
  75. ///request storage permission
  76. Future<bool> getPermission() async {
  77. var status = await Permission.storage.request();
  78. return status.isGranted;
  79. }
  80. ///Select pdf files from android\ios using 'file_picker'
  81. void getPDFFileList(BuildContext context) async {
  82. bool isGranted = await getPermission();
  83. if (!isGranted) {
  84. return;
  85. }
  86. FilePickerResult? result = await FilePicker.platform.pickFiles(
  87. type: FileType.custom, allowedExtensions: ['pdf'], allowMultiple: true);
  88. if (result != null) {
  89. List<String?> pdfFiles = result.paths;
  90. ConvertOptionsBean options =
  91. context.read<ConvertOptionsProvider>().getOptions();
  92. ConvertType convertType =
  93. context.read<ConvertOptionsProvider>().convertType;
  94. context
  95. .read<ConvertProvider>()
  96. .addConvertData(context, convertType, options, pdfFiles);
  97. } else {}
  98. }
  99. void _showConvertOptionsModalBottomSheet(context, ConvertType convertType) {
  100. showModalBottomSheet<int>(
  101. context: context,
  102. isScrollControlled: false,
  103. builder: (BuildContext context) {
  104. switch (convertType) {
  105. case ConvertType.ppt:
  106. return const ConvertPPTOptionsWidget();
  107. case ConvertType.word:
  108. return const ConvertWordOptionsWidget();
  109. case ConvertType.excel:
  110. return const ConvertExcelOptionsWidget();
  111. case ConvertType.image:
  112. return const ConvertImageOptionsWidget();
  113. case ConvertType.csv:
  114. return const ConvertCSVOptionsWidget();
  115. case ConvertType.rtf:
  116. return const ConvertRTFOptionsWidget();
  117. case ConvertType.html:
  118. return const ConvertHtmlOptionsWidget();
  119. default:
  120. return const Padding(
  121. padding: EdgeInsets.all(16),
  122. child: Text('Options not support'));
  123. }
  124. });
  125. }
  126. /// show support convert mime type
  127. void _selectConvertType(context) {
  128. List<ConvertType> convertList = [
  129. ConvertType.ppt,
  130. ConvertType.word,
  131. ConvertType.excel,
  132. ConvertType.txt,
  133. ConvertType.image,
  134. ConvertType.csv,
  135. ConvertType.rtf,
  136. ConvertType.html
  137. ];
  138. showModalBottomSheet(
  139. context: context,
  140. builder: (BuildContext context) {
  141. return ListView.builder(
  142. itemCount: convertList.length,
  143. itemBuilder: (BuildContext context, int index) {
  144. ConvertType type = convertList[index];
  145. return ListTile(
  146. title: Text(type.name),
  147. onTap: () {
  148. context
  149. .read<ConvertOptionsProvider>()
  150. .changeConvertType(type);
  151. Navigator.of(context).pop();
  152. },
  153. );
  154. });
  155. });
  156. }
  157. }