123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import 'dart:io';
- 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';
- import 'package:kmpdfkit_demo/widgets/contains.dart';
- ///
- /// compdfkit_widget.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();
- class ComPDFKitWidget extends StatefulWidget {
- final String? documentPath;
- final dynamic configuration;
- final ComPDFKitWidgetCreateCallback? onComPDFKitWidgetCreate;
- const ComPDFKitWidget(
- {Key? key,
- this.documentPath,
- this.configuration,
- this.onComPDFKitWidgetCreate})
- : super(key: key);
- @override
- State<ComPDFKitWidget> createState() => _ComPDFKitWidgetState();
- }
- class _ComPDFKitWidgetState extends State<ComPDFKitWidget> {
- @override
- Widget build(BuildContext context) {
- const String viewType = ChannelNames.comPDFKitViewType;
- final Map<String, dynamic> creationParams = <String, dynamic>{
- 'document': widget.documentPath,
- 'configuration': widget.configuration
- };
- 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);
- },
- );
- } else {
- return const Center(child: Text('demo only support android and ios'));
- }
- }
- Future<void> onPlatformViewCreated(int id) async {
- if (widget.onComPDFKitWidgetCreate != null) {
- widget.onComPDFKitWidgetCreate!();
- }
- }
- }
|