cpdf_reader_widget.dart 3.4 KB

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