convert_options_widget.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /// convert_options_widget.dart
  2. ///
  3. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  4. ///
  5. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  6. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  7. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  8. /// This notice may not be removed from this file.
  9. import 'package:file_picker/file_picker.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:kmpdfkit_conversion_flutter/models/convert_type.dart';
  12. import 'package:kmpdfkit_conversion_flutter/models/options/options.dart';
  13. import 'package:permission_handler/permission_handler.dart';
  14. import 'package:provider/provider.dart';
  15. import '../states/convert_options_provider.dart';
  16. import '../states/convert_task_queue_provider.dart';
  17. import 'options/convert_csv_options_widget.dart';
  18. import 'options/convert_excel_options_widget.dart';
  19. import 'options/convert_html_options_widget.dart';
  20. import 'options/convert_image_options_widget.dart';
  21. import 'options/convert_ppt_options_widget.dart';
  22. import 'options/convert_rtf_options_widget.dart';
  23. import 'options/convert_word_options_widget.dart';
  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: () async {
  65. ConvertType convertType =
  66. context.read<ConvertOptionsProvider>().convertType;
  67. if (convertType != ConvertType.none) {
  68. Options options =
  69. context.read<ConvertOptionsProvider>().getOptions();
  70. List<String> files = await getPDFFileList(context);
  71. var outputPath = await context
  72. .read<ConvertTaskQueueProvider>()
  73. .getOutputDir();
  74. context.read<ConvertTaskQueueProvider>().addTask(
  75. convertType, options, files, outputPath ?? "");
  76. }
  77. },
  78. child: const Text('Convert')))
  79. ],
  80. );
  81. }
  82. ///request storage permission
  83. Future<bool> getPermission() async {
  84. var status = await Permission.storage.request();
  85. var manageStatus = await Permission.manageExternalStorage.request();
  86. return status.isGranted && manageStatus.isGranted;
  87. }
  88. ///Select pdf files from android\ios using 'file_picker'
  89. Future<List<String>> getPDFFileList(BuildContext context) async {
  90. bool isGranted = await getPermission();
  91. if (!isGranted) {
  92. return List.empty();
  93. }
  94. FilePickerResult? result = await FilePicker.platform.pickFiles(
  95. type: FileType.custom, allowedExtensions: ['pdf'], allowMultiple: true);
  96. if (result != null) {
  97. List<String?> pdfFiles = result.paths;
  98. var files = pdfFiles.where((element) => element != null);
  99. return List.from(files);
  100. } else {
  101. return List.empty();
  102. }
  103. }
  104. void _showConvertOptionsModalBottomSheet(context, ConvertType convertType) {
  105. showModalBottomSheet<int>(
  106. context: context,
  107. isScrollControlled: false,
  108. builder: (BuildContext context) {
  109. switch (convertType) {
  110. case ConvertType.ppt:
  111. return const ConvertPPTOptionsWidget();
  112. case ConvertType.word:
  113. return const ConvertWordOptionsWidget();
  114. case ConvertType.excel:
  115. return const ConvertExcelOptionsWidget();
  116. case ConvertType.image:
  117. return const ConvertImageOptionsWidget();
  118. case ConvertType.csv:
  119. return const ConvertCSVOptionsWidget();
  120. case ConvertType.rtf:
  121. return const ConvertRTFOptionsWidget();
  122. case ConvertType.html:
  123. return const ConvertHtmlOptionsWidget();
  124. default:
  125. return const Padding(
  126. padding: EdgeInsets.all(16),
  127. child: Text('Options not support'));
  128. }
  129. });
  130. }
  131. /// show support convert mime type
  132. void _selectConvertType(context) {
  133. List<ConvertType> convertList = [
  134. ConvertType.ppt,
  135. ConvertType.word,
  136. ConvertType.excel,
  137. ConvertType.txt,
  138. ConvertType.image,
  139. ConvertType.csv,
  140. ConvertType.rtf,
  141. ConvertType.html
  142. ];
  143. showModalBottomSheet(
  144. context: context,
  145. builder: (BuildContext context) {
  146. return ListView.builder(
  147. itemCount: convertList.length,
  148. itemBuilder: (BuildContext context, int index) {
  149. ConvertType type = convertList[index];
  150. return ListTile(
  151. title: Text(type.name),
  152. onTap: () {
  153. context
  154. .read<ConvertOptionsProvider>()
  155. .changeConvertType(type);
  156. Navigator.of(context).pop();
  157. },
  158. );
  159. });
  160. });
  161. }
  162. }