examples.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  3. *
  4. * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. * This notice may not be removed from this file.
  8. *
  9. */
  10. import 'dart:io';
  11. import 'package:compdfkit_flutter/compdfkit.dart';
  12. import 'package:compdfkit_flutter/configuration/cpdf_configuration.dart';
  13. import 'package:compdfkit_flutter_example/cpdf_reader_widget_controller_example.dart';
  14. import 'package:compdfkit_flutter_example/cpdf_reader_widget_dark_theme_example.dart';
  15. import 'package:compdfkit_flutter_example/utils/file_util.dart';
  16. import 'package:file_picker/file_picker.dart';
  17. import 'package:flutter/material.dart';
  18. import 'cpdf_reader_widget_example.dart';
  19. import 'widgets/cpdf_fun_item.dart';
  20. const String _documentPath = 'pdfs/PDF_Document.pdf';
  21. List<Widget> examples(BuildContext context) => [
  22. Padding(padding: const EdgeInsets.only(top: 8, bottom: 8), child: Text(
  23. 'Widget Examples',
  24. style: Theme.of(context).textTheme.bodyLarge?.copyWith(fontWeight: FontWeight.w500),
  25. )),
  26. FeatureItem(
  27. title: 'Show CPDFReaderWidget',
  28. description: 'Display PDF view in flutter widget',
  29. onTap: () => showCPDFReaderWidget(context)),
  30. if (Platform.isAndroid) ...[
  31. FeatureItem(title: 'CPDFReaderWidget Dark Theme',
  32. description: 'Opens a document in night mode with a custom dark theme',
  33. onTap: () => showDarkThemeCPDFReaderWidget(context))
  34. ],
  35. FeatureItem(title: 'Widget Controller Examples',
  36. description: 'CPDFReaderWidget Controller fun example',
  37. onTap: () => showCPDFReaderWidgetTest(context)),
  38. Padding(padding: const EdgeInsets.only(top: 8, bottom: 8), child: Text(
  39. 'Modal View Examples',
  40. style: Theme.of(context).textTheme.bodyLarge?.copyWith(fontWeight: FontWeight.w500),
  41. )),
  42. FeatureItem(
  43. title: 'Basic Example',
  44. description: 'Open sample pdf document',
  45. onTap: () => showDocument(context)),
  46. FeatureItem(
  47. title: 'Select External Files',
  48. description: 'Select pdf document from system file manager',
  49. onTap: () => pickDocument())
  50. ];
  51. void showDocument(context) async {
  52. File document = await extractAsset(context, _documentPath);
  53. ComPDFKit.openDocument(document.path,
  54. password: '', configuration: CPDFConfiguration());
  55. }
  56. void pickDocument() async {
  57. if (Platform.isIOS) {
  58. await ComPDFKit.getPdfFilePath().then((value) {
  59. ComPDFKit.openDocument(value!,
  60. password: '', configuration: CPDFConfiguration());
  61. });
  62. } else {
  63. FilePickerResult? result = await FilePicker.platform.pickFiles(
  64. type: FileType.custom,
  65. allowedExtensions: ['pdf'],
  66. );
  67. if (result != null) {
  68. ComPDFKit.openDocument(result.files.first.path!,
  69. password: '', configuration: CPDFConfiguration());
  70. }
  71. }
  72. }
  73. void showCPDFReaderWidget(context) async {
  74. File document = await extractAsset(context, _documentPath, shouldOverwrite: false);
  75. goTo(CPDFReaderWidgetExample(documentPath: document.path), context);
  76. }
  77. void showDarkThemeCPDFReaderWidget(context) async {
  78. File document = await extractAsset(context, _documentPath, shouldOverwrite: false);
  79. goTo(CPDFDarkThemeExample(documentPath: document.path), context);
  80. }
  81. void showCPDFReaderWidgetTest(context) async {
  82. File document = await extractAsset(context, _documentPath, shouldOverwrite: false);
  83. goTo(CPDFReaderWidgetControllerExample(documentPath: document.path), context);
  84. }
  85. void goTo(Widget widget, BuildContext context) =>
  86. Navigator.push(context, MaterialPageRoute(builder: (context) {
  87. return widget;
  88. }));