123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import 'dart:io';
- import 'package:compdfkit_flutter/core/cpdf_view_ctrl.dart';
- import 'package:compdfkit_flutter/cpdf_configuration.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/gestures.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/rendering.dart';
- import 'package:flutter/services.dart';
- /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
- ///
- /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- /// This notice may not be removed from this file.
- ///
- typedef ComPDFKitWidgetCreateCallback = void Function(CPDFViewCtrl ctrl);
- class CPDFReaderWidget extends StatefulWidget {
- /// pdf file path
- final String? documentPath;
- /// init ComPDFKit SDK configuration
- final CPDFConfiguration configuration;
- /// init callback
- final ComPDFKitWidgetCreateCallback? onComPDFKitWidgetCreate;
- const CPDFReaderWidget(
- {Key? key,
- this.documentPath,
- required this.configuration,
- this.onComPDFKitWidgetCreate})
- : super(key: key);
- @override
- State<CPDFReaderWidget> createState() => _CPDFReaderWidgetState();
- }
- class _CPDFReaderWidgetState extends State<CPDFReaderWidget> {
- @override
- Widget build(BuildContext context) {
- const String viewType = 'com.compdfkit.flutter.pdfviewer';
- final Map<String, dynamic> creationParams = <String, dynamic>{
- 'document': widget.documentPath,
- 'configuration': widget.configuration.toConfiguration()
- };
- if (Platform.isAndroid) {
- return PlatformViewLink(
- surfaceFactory:
- (BuildContext context, PlatformViewController controller) {
- return AndroidViewSurface(
- controller: controller as AndroidViewController,
- hitTestBehavior: PlatformViewHitTestBehavior.opaque,
- gestureRecognizers: const <Factory<
- OneSequenceGestureRecognizer>>{});
- },
- onCreatePlatformView: (PlatformViewCreationParams params) {
- return PlatformViewsService.initSurfaceAndroidView(
- id: params.id,
- viewType: viewType,
- creationParams: creationParams,
- creationParamsCodec: const StandardMessageCodec(),
- layoutDirection: TextDirection.ltr)
- ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
- ..addOnPlatformViewCreatedListener(onPlatformViewCreated)
- ..create();
- },
- viewType: viewType);
- } else if (Platform.isIOS) {
- return UiKitView(
- viewType: viewType,
- layoutDirection: TextDirection.ltr,
- hitTestBehavior: PlatformViewHitTestBehavior.opaque,
- creationParams: creationParams,
- creationParamsCodec: const StandardMessageCodec(),
- onPlatformViewCreated: (id) {
- onPlatformViewCreated(id);
- },
- );
- // return const Center(child: Text('ios platform'));
- } else {
- return const Center(child: Text('demo only support android and ios'));
- }
- }
- Future<void> onPlatformViewCreated(int id) async {
- if (widget.onComPDFKitWidgetCreate != null) {
- widget.onComPDFKitWidgetCreate!(CPDFViewCtrl(id));
- }
- }
- }
|