examples.dart 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. FilePickerResult? result = await FilePicker.platform.pickFiles(
  58. type: FileType.custom,
  59. allowedExtensions: ['pdf'],
  60. );
  61. if (result != null) {
  62. ComPDFKit.openDocument(result.files.first.path!,
  63. password: '', configuration: CPDFConfiguration());
  64. }
  65. }
  66. void showCPDFReaderWidget(context) async {
  67. File document = await extractAsset(context, _documentPath, shouldOverwrite: false);
  68. goTo(CPDFReaderWidgetExample(documentPath: document.path), context);
  69. }
  70. void showDarkThemeCPDFReaderWidget(context) async {
  71. File document = await extractAsset(context, _documentPath, shouldOverwrite: false);
  72. goTo(CPDFDarkThemeExample(documentPath: document.path), context);
  73. }
  74. void showCPDFReaderWidgetTest(context) async {
  75. File document = await extractAsset(context, _documentPath, shouldOverwrite: false);
  76. goTo(CPDFReaderWidgetControllerExample(documentPath: document.path), context);
  77. }
  78. void goTo(Widget widget, BuildContext context) =>
  79. Navigator.push(context, MaterialPageRoute(builder: (context) {
  80. return widget;
  81. }));