|
| 1 | +/// |
| 2 | +/// Copyright © 2022 PSPDFKit GmbH. 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 PSPDFKIT 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:async'; |
| 11 | + |
| 12 | +import 'package:flutter/services.dart'; |
| 13 | +import 'package:flutter/foundation.dart'; |
| 14 | +import 'package:flutter/gestures.dart'; |
| 15 | +import 'package:flutter/rendering.dart'; |
| 16 | +import 'package:flutter/cupertino.dart'; |
| 17 | +import 'package:flutter/material.dart'; |
| 18 | + |
| 19 | +import 'package:pspdfkit_flutter/src/widgets/pspdfkit_widget_controller.dart'; |
| 20 | + |
| 21 | +import 'platform_utils.dart'; |
| 22 | + |
| 23 | +class PspdfkitManualSaveExampleWidget extends StatefulWidget { |
| 24 | + final String documentPath; |
| 25 | + final dynamic configuration; |
| 26 | + |
| 27 | + const PspdfkitManualSaveExampleWidget( |
| 28 | + {Key? key, required this.documentPath, this.configuration}) |
| 29 | + : super(key: key); |
| 30 | + |
| 31 | + @override |
| 32 | + _PspdfkitManualSaveExampleWidgetState createState() => |
| 33 | + _PspdfkitManualSaveExampleWidgetState(); |
| 34 | +} |
| 35 | + |
| 36 | +class _PspdfkitManualSaveExampleWidgetState |
| 37 | + extends State<PspdfkitManualSaveExampleWidget> { |
| 38 | + late PspdfkitWidgetController pspdfkitWidgetController; |
| 39 | + |
| 40 | + @override |
| 41 | + Widget build(BuildContext context) { |
| 42 | + // This is used in the platform side to register the view. |
| 43 | + const String viewType = 'com.pspdfkit.widget'; |
| 44 | + // Pass parameters to the platform side. |
| 45 | + final Map<String, dynamic> creationParams = <String, dynamic>{ |
| 46 | + 'document': widget.documentPath, |
| 47 | + 'configuration': widget.configuration |
| 48 | + }; |
| 49 | + if (PlatformUtils.isCurrentPlatformSupported()) { |
| 50 | + return Scaffold( |
| 51 | + extendBodyBehindAppBar: |
| 52 | + defaultTargetPlatform == TargetPlatform.android, |
| 53 | + appBar: AppBar(), |
| 54 | + body: SafeArea( |
| 55 | + top: false, |
| 56 | + bottom: false, |
| 57 | + child: Container( |
| 58 | + padding: defaultTargetPlatform == TargetPlatform.iOS |
| 59 | + ? null |
| 60 | + : const EdgeInsets.only(top: kToolbarHeight), |
| 61 | + child: Column(children: <Widget>[ |
| 62 | + Expanded( |
| 63 | + child: PlatformUtils.isAndroid() |
| 64 | + ? PlatformViewLink( |
| 65 | + viewType: viewType, |
| 66 | + surfaceFactory: (BuildContext context, |
| 67 | + PlatformViewController controller) { |
| 68 | + return AndroidViewSurface( |
| 69 | + controller: |
| 70 | + controller as AndroidViewController, |
| 71 | + gestureRecognizers: const < |
| 72 | + Factory< |
| 73 | + OneSequenceGestureRecognizer>>{}, |
| 74 | + hitTestBehavior: |
| 75 | + PlatformViewHitTestBehavior.opaque, |
| 76 | + ); |
| 77 | + }, |
| 78 | + onCreatePlatformView: |
| 79 | + (PlatformViewCreationParams params) { |
| 80 | + return PlatformViewsService |
| 81 | + .initSurfaceAndroidView( |
| 82 | + id: params.id, |
| 83 | + viewType: viewType, |
| 84 | + layoutDirection: TextDirection.ltr, |
| 85 | + creationParams: creationParams, |
| 86 | + creationParamsCodec: |
| 87 | + const StandardMessageCodec(), |
| 88 | + onFocus: () { |
| 89 | + params.onFocusChanged(true); |
| 90 | + }, |
| 91 | + ) |
| 92 | + ..addOnPlatformViewCreatedListener( |
| 93 | + params.onPlatformViewCreated) |
| 94 | + ..addOnPlatformViewCreatedListener( |
| 95 | + onPlatformViewCreated) |
| 96 | + ..create(); |
| 97 | + }) |
| 98 | + : UiKitView( |
| 99 | + viewType: viewType, |
| 100 | + layoutDirection: TextDirection.ltr, |
| 101 | + creationParams: creationParams, |
| 102 | + onPlatformViewCreated: onPlatformViewCreated, |
| 103 | + creationParamsCodec: |
| 104 | + const StandardMessageCodec())), |
| 105 | + SizedBox( |
| 106 | + child: Column(children: <Widget>[ |
| 107 | + ElevatedButton( |
| 108 | + onPressed: () async { |
| 109 | + await pspdfkitWidgetController.save(); |
| 110 | + }, |
| 111 | + child: const Text('Save Document')) |
| 112 | + ])) |
| 113 | + ])))); |
| 114 | + } else { |
| 115 | + return Text( |
| 116 | + '$defaultTargetPlatform is not yet supported by PSPDFKit for Flutter.'); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Future<void> onPlatformViewCreated(int id) async { |
| 121 | + pspdfkitWidgetController = PspdfkitWidgetController(id); |
| 122 | + } |
| 123 | +} |
0 commit comments