cpdf_reader_widget.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import 'dart:io';
  2. import 'package:compdfkit_flutter/core/cpdf_view_ctrl.dart';
  3. import 'package:compdfkit_flutter/cpdf_configuration.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/gestures.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/rendering.dart';
  8. import 'package:flutter/services.dart';
  9. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  10. ///
  11. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  12. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  13. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  14. /// This notice may not be removed from this file.
  15. ///
  16. typedef ComPDFKitWidgetCreateCallback = void Function(CPDFViewCtrl ctrl);
  17. class CPDFReaderWidget extends StatefulWidget {
  18. /// pdf file path
  19. final String? documentPath;
  20. /// init ComPDFKit SDK configuration
  21. final CPDFConfiguration configuration;
  22. /// init callback
  23. final ComPDFKitWidgetCreateCallback? onComPDFKitWidgetCreate;
  24. const CPDFReaderWidget(
  25. {Key? key,
  26. this.documentPath,
  27. required this.configuration,
  28. this.onComPDFKitWidgetCreate})
  29. : super(key: key);
  30. @override
  31. State<CPDFReaderWidget> createState() => _CPDFReaderWidgetState();
  32. }
  33. class _CPDFReaderWidgetState extends State<CPDFReaderWidget> {
  34. @override
  35. Widget build(BuildContext context) {
  36. const String viewType = 'com.compdfkit.flutter.pdfviewer';
  37. final Map<String, dynamic> creationParams = <String, dynamic>{
  38. 'document': widget.documentPath,
  39. 'configuration': widget.configuration.toConfiguration()
  40. };
  41. if (Platform.isAndroid) {
  42. return PlatformViewLink(
  43. surfaceFactory:
  44. (BuildContext context, PlatformViewController controller) {
  45. return AndroidViewSurface(
  46. controller: controller as AndroidViewController,
  47. hitTestBehavior: PlatformViewHitTestBehavior.opaque,
  48. gestureRecognizers: const <Factory<
  49. OneSequenceGestureRecognizer>>{});
  50. },
  51. onCreatePlatformView: (PlatformViewCreationParams params) {
  52. return PlatformViewsService.initSurfaceAndroidView(
  53. id: params.id,
  54. viewType: viewType,
  55. creationParams: creationParams,
  56. creationParamsCodec: const StandardMessageCodec(),
  57. layoutDirection: TextDirection.ltr)
  58. ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
  59. ..addOnPlatformViewCreatedListener(onPlatformViewCreated)
  60. ..create();
  61. },
  62. viewType: viewType);
  63. } else if (Platform.isIOS) {
  64. return UiKitView(
  65. viewType: viewType,
  66. layoutDirection: TextDirection.ltr,
  67. hitTestBehavior: PlatformViewHitTestBehavior.opaque,
  68. creationParams: creationParams,
  69. creationParamsCodec: const StandardMessageCodec(),
  70. onPlatformViewCreated: (id) {
  71. onPlatformViewCreated(id);
  72. },
  73. );
  74. // return const Center(child: Text('ios platform'));
  75. } else {
  76. return const Center(child: Text('demo only support android and ios'));
  77. }
  78. }
  79. Future<void> onPlatformViewCreated(int id) async {
  80. if (widget.onComPDFKitWidgetCreate != null) {
  81. widget.onComPDFKitWidgetCreate!(CPDFViewCtrl(id));
  82. }
  83. }
  84. }