main.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:file_picker/file_picker.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:kmpdfkit_demo/compdfkit.dart';
  4. import 'package:kmpdfkit_demo/widgets/page/pdf_page_widget.dart';
  5. import 'package:kmpdfkit_demo/widgets/page/setting_page_widget.dart';
  6. import 'package:kmpdfkit_demo/widgets/page_routes.dart';
  7. import 'package:permission_handler/permission_handler.dart';
  8. void main() {
  9. runApp(MaterialApp(
  10. home: const MyApp(),
  11. initialRoute: PageRoutes.home,
  12. routes: {
  13. PageRoutes.home: (context) => const MyApp(),
  14. PageRoutes.settings: (context) => const SettingPageWidget()
  15. },
  16. ));
  17. }
  18. class MyApp extends StatefulWidget {
  19. const MyApp({Key? key}) : super(key: key);
  20. @override
  21. State<MyApp> createState() => _MyAppState();
  22. }
  23. class _MyAppState extends State<MyApp> {
  24. String _comPDFKitVersion = '';
  25. @override
  26. void initState() {
  27. super.initState();
  28. initPlatformState();
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. return Scaffold(
  33. appBar: AppBar(
  34. title: Text(comPDFKitVersion()),
  35. ),
  36. body: Padding(
  37. padding: const EdgeInsets.all(8),
  38. child: Column(
  39. children: [
  40. Builder(
  41. builder: (BuildContext context) => SizedBox(
  42. width: double.infinity,
  43. child: ElevatedButton(
  44. onPressed: () {
  45. showDocument(context);
  46. },
  47. child: const Text('Simple PDF Viewer'),
  48. ))),
  49. ],
  50. ),
  51. ));
  52. }
  53. void initPlatformState() async {
  54. String? comPDFKitVersion = await ComPDFKit.getComPDFKitVersionCode();
  55. setState(() {
  56. _comPDFKitVersion = comPDFKitVersion ?? '';
  57. });
  58. }
  59. String comPDFKitVersion() {
  60. return 'ComPDFKit $_comPDFKitVersion';
  61. }
  62. ///request storage permission
  63. Future<bool> getPermission() async {
  64. var status = await Permission.storage.request();
  65. return status.isGranted;
  66. }
  67. Future<String?> getPDFFile() async {
  68. bool isGranted = await getPermission();
  69. if (!isGranted) {
  70. return null;
  71. }
  72. FilePickerResult? result = await FilePicker.platform.pickFiles(
  73. type: FileType.custom,
  74. allowedExtensions: ['pdf'],
  75. allowMultiple: false);
  76. if (result != null) {
  77. List<String?> files = result.paths;
  78. return files[0];
  79. } else {
  80. return null;
  81. }
  82. }
  83. void showDocument(BuildContext context) async {
  84. var documentPath = await getPDFFile();
  85. if (documentPath != null) {
  86. await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
  87. builder: (_) => PDFPageWidget(
  88. documentPath: documentPath,
  89. )));
  90. }
  91. }
  92. }