123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:kmpdfkit_demo/widgets/constains.dart';
- import 'package:kmpdfkit_demo/widgets/extension/color_extension.dart';
- import 'package:kmpdfkit_demo/widgets/models/annot_bean.dart';
- import 'models/annot_attribute_bean.dart';
- const _methodChannel = MethodChannel(ChannelNames.eventCPDFReaderView);
- const _modifyAnnotAttrChannel =
- MethodChannel(ChannelNames.eventModifyAnnotationAttr);
- //Listen to PDFReaderView events.
- const _readerViewCallBackEventChannel =
- EventChannel(ChannelNames.eventReaderViewCallback);
- const _contextMenuHelperEventChannel =
- EventChannel(ChannelNames.eventReaderViewContextMenuHelper);
- typedef CancelListener = void Function();
- enum eventSinkId {
- readerViewCallBack,
- readerViewContextMenuHelper,
- }
- /// Listen to PDFReaderView events<br>
- /// onTapMainDocArea : Click CPDFReaderView display area callback
- /// onMoveToChild : The pdf document is moved to the specified page, and the page number is returned by 'pageIndex'
- /// onRecordLastJumpNum : Returns the page number of the last record and returns the page number via the 'pageIndex' parameter
- /// onScrolling : CPDFReaderView page sliding monitoring
- /// onScrollEnd : CPDFReaderView page sliding end monitoring
- ///
- ///Please use in addOnPlatformViewCreatedListener
- ///```dart
- /// ..addOnPlatformViewCreatedListener((id) {
- /// setReaderViewCallbackListener(
- /// onTapMainDocArea: () {
- /// print("onTapMainDocArea");
- /// },
- /// onMoveToChild: (pageIndex) {},
- /// onScrolling: () {},
- /// onScrollEnd: () {},
- /// onRecordLastJumpPageNum: (pageIndex) {});
- /// })
- ///```
- ///
- CancelListener setReaderViewCallbackListener(
- {VoidCallback? onTapMainDocArea,
- Function(int pageIndex)? onMoveToChild,
- VoidCallback? onScrolling,
- VoidCallback? onScrollEnd,
- Function(int pageIndex)? onRecordLastJumpPageNum}) {
- var subscription = _readerViewCallBackEventChannel
- .receiveBroadcastStream(eventSinkId.readerViewCallBack.index)
- .listen((data) {
- Map<dynamic, dynamic> result = data;
- String eventType = result[EventParameters.eventType];
- switch (eventType) {
- case EventParameters.onTapMainDocArea:
- if (onTapMainDocArea != null) {
- onTapMainDocArea();
- }
- break;
- case EventParameters.onMoveToChild:
- if (onMoveToChild != null) {
- int pageIndex = result[EventParameters.pageIndex];
- onMoveToChild(pageIndex);
- }
- break;
- case EventParameters.onRecordLastJumpPageNum:
- if (onRecordLastJumpPageNum != null) {
- int pageIndex = result[EventParameters.pageIndex];
- onRecordLastJumpPageNum(pageIndex);
- }
- break;
- case EventParameters.onScrollEnd:
- if (onScrollEnd != null) {
- onScrollEnd();
- }
- break;
- case EventParameters.onScrolling:
- if (onScrolling != null) {
- onScrolling();
- }
- break;
- default:
- break;
- }
- }, cancelOnError: true);
- return () {
- subscription.cancel();
- };
- }
- /// Set up an event channel for "cpdfreaderview" to trigger an event when the user clicks on the "Properties" menu item.
- /// When the event is triggered, the native side will return the selected annotation type and related properties.
- /// The Flutter side will display an annotation property adjustment interface
- /// based on the returned annotation type and properties, allowing users to modify the annotation properties.
- /// After modifying the annotation properties, the Flutter side will
- /// send the modified property values back to the native side to update
- /// the annotation in the PDF file.
- /// ```dart
- /// setReaderViewContextMenuHelperListener((annotBean) {
- /// //show annotation attribute options widget
- /// _showAttributeOptionsModalBottomSheet(context, annotBean,(AnnotAttributeBean bean) {
- /// modifyAnnotationAttribute(
- /// annotationType: annotBean.annotType, bean: bean);
- /// }, (){
- /// dismissModifyAnnotationAttribute();
- /// });
- /// });
- ///
- /// ```
- void setReaderViewContextMenuHelperListener(
- Function(AnnotBean) showAnnotationAttributeOptions) {
- _contextMenuHelperEventChannel.receiveBroadcastStream({
- EventParameters.eventType: eventSinkId.readerViewContextMenuHelper.name
- }).listen((event) {
- Map<dynamic, dynamic> result = event;
- String annotType = result[EventParameters.annotType];
- AnnotationType type = AnnotationType.values.byName(annotType);
- showAnnotationAttributeOptions(AnnotBean(
- annotType: type,
- attributeBean: AnnotAttributeBean.parseMapValue(type, result),
- ));
- }, cancelOnError: true);
- }
- ///This method is used to select annotations on the page and adjust their properties.
- /// Before using this method, please make sure that an annotation object has been selected.
- /// To use this method, you need to first call the "setReaderViewContextMenuHelperListener" method to set up the context menu.
- /// ```dart
- /// setReaderViewContextMenuHelperListener((annotBean) {
- /// //show annotation attribute options widget
- /// _showAttributeOptionsModalBottomSheet(context, annotBean,(AnnotAttributeBean bean) {
- /// //modify annotation attribute
- /// modifyAnnotationAttribute(
- /// annotationType: annotBean.annotType, bean: bean);
- /// }, (){
- /// dismissModifyAnnotationAttribute();
- /// });
- /// });
- ///
- /// ```
- void modifyAnnotationAttribute(
- {required AnnotationType annotationType,
- required AnnotAttributeBean bean}) async {
- await _modifyAnnotAttrChannel.invokeMethod(
- Functions.modifyAnnotAttribute, bean.toMapValues(annotationType));
- }
- /// Please call this method after the annotation attribute adjustment interface is closed.
- void dismissModifyAnnotationAttribute() async {
- await _modifyAnnotAttrChannel
- .invokeMethod(Functions.dismissModifyAnnotationAttr);
- }
- ///get current CPDFReaderView scroll direction
- ///true : [ScrollDirection.vertical]
- ///false : [ScrollDirection.horizontal]
- ///```dart
- /// bool isVertical = await scrollDirectionIsVerticalMode();
- /// ```
- Future<bool> scrollDirectionIsVerticalMode() async {
- String scrollDirection =
- await _methodChannel.invokeMethod(Functions.getScrollDirection);
- return scrollDirection == ScrollDirection.vertical;
- }
- /// set CPDFReaderView scroll direction
- ///```dart
- ///bool isVertical = await setScrollDirection(direction);
- ///
- ///```
- /// Returns the current scroll direction
- /// true : [ScrollDirection.vertical]
- /// false : [ScrollDirection.horizontal]
- ///
- Future<bool> setScrollDirection(String direction) async {
- String scrollDirection = await _methodChannel.invokeMethod(
- Functions.setScrollDirection,
- {EventParameters.scrollDirection: direction});
- return scrollDirection == ScrollDirection.vertical;
- }
- ///Get the current CPDFReaderView page mode
- ///```dart
- /// bool doublePage = await isDoublePage();
- ///
- ///```
- Future<bool> isDoublePage() async {
- return await _methodChannel.invokeMethod(Functions.getPageMode);
- }
- ///set page mode
- ///true: double page
- ///false: single page
- ///```dart
- /// bool isDoublePage = await setPageMode(doublePage);
- ///
- ///```
- Future<bool> setPageMode(bool doublePage) async {
- return await _methodChannel.invokeMethod(
- Functions.setPageMode, {EventParameters.isDoublePage: doublePage});
- }
- ///Get whether 'CPDFReaderView' is in continuous scrolling mode
- ///```dart
- /// bool isContinue = await isContinueMode();
- /// ```
- Future<bool> isContinueMode() async {
- return await _methodChannel.invokeMethod(Functions.getPageContinue);
- }
- ///Set page scrolling mode
- ///true: continue Mode
- ///```dart
- /// bool isContinueMode = await setIsContinueMode(isContinueMode);
- /// ```
- Future<bool> setIsContinueMode(bool isContinueMode) async {
- return await _methodChannel.invokeMethod(Functions.setPageContinue,
- {EventParameters.isContinueMode: isContinueMode});
- }
- ///Get whether 'ComPDFReaderView' is in cover display mode
- ///```dart
- /// bool isCoverPageMode = await isCoverPageMode();
- /// ```
- Future<bool> isCoverPageMode() async {
- return await _methodChannel.invokeMethod(Functions.isCoverPageMode);
- }
- ///set is cover display mode
- ///true: is cover page mode
- ///```dart
- /// bool isCoverPageMode = await setIsCoverPageMode(isCoverPageMode);
- /// ```
- Future<bool> setIsCoverPageMode(bool isCoverPageMode) async {
- return await _methodChannel.invokeMethod(Functions.setCoverPageMode,
- {EventParameters.isCoverPageMode: isCoverPageMode});
- }
- ///Get 'ComPDFReaderView' whether to crop the display mode
- ///```dart
- /// bool isCropMode = await isCropPageMode();
- /// ```
- Future<bool> isCropPageMode() async {
- return await _methodChannel.invokeMethod(Functions.isCropPageMode);
- }
- ///set is crop display mode
- ///true: is crop page mode
- ///```dart
- /// bool isCropMode = await setIsCropPageMode(isCropPageMode);
- /// ```
- Future<bool> setIsCropPageMode(bool isCropPageMode) async {
- return await _methodChannel.invokeMethod(Functions.setIsCropPageMode,
- {EventParameters.isCropPageMode: isCropPageMode});
- }
- /// get reader view background color
- ///```dart
- /// int color = await getReadBackgroundColor();
- ///```
- /// return current reader view background color
- Future<int> getReadBackgroundColor() async {
- return await _methodChannel.invokeMethod(Functions.getReadBackgroundColor);
- }
- /// set reader view background color
- /// Changing the appearance mode will change the PDF rendering style, but it does not modify the PDF on disk. To set the color mode:
- /// normal color mode : #FFFFFF
- /// Dark mode : #000000
- /// Sepia mode : #FFEFBE
- /// Reseda mode : #CDE6D0
- /// Custom color mode : any hexadecimal value
- ///```dart
- /// int color = await setReadBackgroundColor(backgroundColor);
- ///
- ///```
- ///return current reader view background color
- Future<int> setReadBackgroundColor(int backgroundColor) async {
- return await _methodChannel.invokeMethod(Functions.setReadBackgroundColor,
- {EventParameters.readBackgroundColor: backgroundColor});
- }
- /// Get annotation attributes based on annotation type
- ///
- /// ```dart
- /// AnnotAttributeBean highlight = await getAnnotAttribute(AnnotType.highlight);
- /// ```
- /// return annotation attributes color and alpha
- Future<AnnotAttributeBean> getAnnotAttribute(AnnotationType annotType) async {
- Map<dynamic, dynamic> annotAttrMap = await _methodChannel.invokeMethod(
- Functions.getAnnotAttribute, {EventParameters.annotType: annotType.name});
- return AnnotAttributeBean.parseMapValue(annotType, annotAttrMap);
- }
- ///set annotation attributes
- ///```dart
- /// AnnotAttributeBean attr = await setAnnotAttribute(
- /// annotType: annotBean.annotType, color: color, alpha: alpha);
- ///```
- /// return annotation attributes color and alpha
- void setAnnotAttribute(
- {required AnnotationType annotationType,
- required AnnotAttributeBean bean}) async {
- await _methodChannel.invokeMethod(Functions.setAnnotAttribute, {
- EventParameters.annotType: annotationType.name,
- EventParameters.annotAttribute: bean.toMapValues(annotationType)
- });
- }
- /// set CPDFReaderView current focused type
- /// This setting will change the type of action you swipe on CPDFReaderView
- /// If you need to set it as a browsing state, please set
- /// focusedType :[AnnotationType.unknown]
- /// touchMode : [TouchMode.browse]
- ///
- /// If you need to set to add Annotation state, please set
- /// focusedType : [AnnotationType]
- /// touchMode : [TouchMode.add_annot]
- ///```dart
- /// setCPDFReaderViewFocusType(annotationType)
- ///```
- ///
- void setCPDFReaderViewFocusType(AnnotationType annotationType) async {
- await _methodChannel.invokeMethod(Functions.setCurrentFocusedType, {
- EventParameters.touchMode: annotationType == AnnotationType.unknown
- ? TouchMode.browse.name
- : TouchMode.add_annot.name,
- EventParameters.focusedType: annotationType.name
- });
- }
|