events.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:kmpdfkit_demo/widgets/contains.dart';
  4. import 'package:kmpdfkit_demo/widgets/extension/color_extension.dart';
  5. import 'package:kmpdfkit_demo/widgets/models/annot_bean.dart';
  6. import 'models/annot_attribute_bean.dart';
  7. const _methodChannel = MethodChannel(ChannelNames.eventCPDFReaderView);
  8. const _modifyAnnotAttrChannel =
  9. MethodChannel(ChannelNames.eventModifyAnnotationAttr);
  10. //Listen to PDFReaderView events.
  11. const _readerViewCallBackEventChannel =
  12. EventChannel(ChannelNames.eventReaderViewCallback);
  13. const _contextMenuHelperEventChannel =
  14. EventChannel(ChannelNames.eventReaderViewContextMenuHelper);
  15. const _focusedChangeEventChannel =
  16. EventChannel(ChannelNames.eventFocusedChange);
  17. typedef CancelListener = void Function();
  18. enum eventSinkId {
  19. readerViewCallBack,
  20. readerViewContextMenuHelper,
  21. readerViewFocusedChange
  22. }
  23. /// Listen to PDFReaderView events<br>
  24. /// onTapMainDocArea : Click CPDFReaderView display area callback
  25. /// onMoveToChild : The pdf document is moved to the specified page, and the page number is returned by 'pageIndex'
  26. /// onRecordLastJumpNum : Returns the page number of the last record and returns the page number via the 'pageIndex' parameter
  27. /// onScrolling : CPDFReaderView page sliding monitoring
  28. /// onScrollEnd : CPDFReaderView page sliding end monitoring
  29. ///
  30. ///Please use in addOnPlatformViewCreatedListener
  31. ///```dart
  32. /// ..addOnPlatformViewCreatedListener((id) {
  33. /// setReaderViewCallbackListener(
  34. /// onTapMainDocArea: () {
  35. /// print("onTapMainDocArea");
  36. /// },
  37. /// onMoveToChild: (pageIndex) {},
  38. /// onScrolling: () {},
  39. /// onScrollEnd: () {},
  40. /// onRecordLastJumpPageNum: (pageIndex) {});
  41. /// })
  42. ///```
  43. ///
  44. CancelListener setReaderViewCallbackListener(
  45. {VoidCallback? onTapMainDocArea,
  46. Function(int pageIndex)? onMoveToChild,
  47. VoidCallback? onScrolling,
  48. VoidCallback? onScrollEnd,
  49. Function(int pageIndex)? onRecordLastJumpPageNum}) {
  50. var subscription = _readerViewCallBackEventChannel
  51. .receiveBroadcastStream(eventSinkId.readerViewCallBack.index)
  52. .listen((data) {
  53. Map<dynamic, dynamic> result = data;
  54. String eventType = result[EventParameters.eventType];
  55. switch (eventType) {
  56. case EventParameters.onTapMainDocArea:
  57. if (onTapMainDocArea != null) {
  58. onTapMainDocArea();
  59. }
  60. break;
  61. case EventParameters.onMoveToChild:
  62. if (onMoveToChild != null) {
  63. int pageIndex = result[EventParameters.pageIndex];
  64. onMoveToChild(pageIndex);
  65. }
  66. break;
  67. case EventParameters.onRecordLastJumpPageNum:
  68. if (onRecordLastJumpPageNum != null) {
  69. int pageIndex = result[EventParameters.pageIndex];
  70. onRecordLastJumpPageNum(pageIndex);
  71. }
  72. break;
  73. case EventParameters.onScrollEnd:
  74. if (onScrollEnd != null) {
  75. onScrollEnd();
  76. }
  77. break;
  78. case EventParameters.onScrolling:
  79. if (onScrolling != null) {
  80. onScrolling();
  81. }
  82. break;
  83. default:
  84. break;
  85. }
  86. }, cancelOnError: true);
  87. return () {
  88. subscription.cancel();
  89. };
  90. }
  91. /// Set up an event channel for "cpdfreaderview" to trigger an event when the user clicks on the "Properties" menu item.
  92. /// When the event is triggered, the native side will return the selected annotation type and related properties.
  93. /// The Flutter side will display an annotation property adjustment interface
  94. /// based on the returned annotation type and properties, allowing users to modify the annotation properties.
  95. /// After modifying the annotation properties, the Flutter side will
  96. /// send the modified property values back to the native side to update
  97. /// the annotation in the PDF file.
  98. /// ```dart
  99. /// setReaderViewContextMenuHelperListener((annotBean) {
  100. /// //show annotation attribute options widget
  101. /// _showAttributeOptionsModalBottomSheet(context, annotBean,(AnnotAttributeBean bean) {
  102. /// modifyAnnotationAttribute(
  103. /// annotationType: annotBean.annotType, bean: bean);
  104. /// }, (){
  105. /// dismissModifyAnnotationAttribute();
  106. /// });
  107. /// });
  108. ///
  109. /// ```
  110. void setReaderViewContextMenuHelperListener(
  111. Function(AnnotBean) showAnnotationAttributeOptions) {
  112. _contextMenuHelperEventChannel.receiveBroadcastStream({
  113. EventParameters.eventType: eventSinkId.readerViewContextMenuHelper.name
  114. }).listen((event) {
  115. Map<dynamic, dynamic> result = event;
  116. String annotType = result[EventParameters.annotType];
  117. print('setReaderViewContextMenuHelperListener:$event');
  118. AnnotationType type = AnnotationType.values.byName(annotType);
  119. showAnnotationAttributeOptions(AnnotBean(
  120. annotType: type,
  121. attributeBean: AnnotAttributeBean.parseMapValue(type, result),
  122. ));
  123. }, cancelOnError: true);
  124. }
  125. ///Converts the focused type from a string to an AnnotationType enum value.
  126. void setReaderViewFocusedChangeListener(Function(AnnotationType) callback) {
  127. _focusedChangeEventChannel
  128. .receiveBroadcastStream(eventSinkId.readerViewFocusedChange.name)
  129. .listen((event) {
  130. String focusedType = event;
  131. //Converts the focused type from a string to an AnnotationType enum value.
  132. AnnotationType type = AnnotationType.values.byName(focusedType.toLowerCase());
  133. // If the focused type is a shape type, treat it as a general shape type.
  134. if(type.isShapeType){
  135. type = AnnotationType.shape;
  136. }
  137. // Calls the callback function with the determined AnnotationType value.
  138. callback(type);
  139. }, cancelOnError: true);
  140. }
  141. ///This method is used to select annotations on the page and adjust their properties.
  142. /// Before using this method, please make sure that an annotation object has been selected.
  143. /// To use this method, you need to first call the "setReaderViewContextMenuHelperListener" method to set up the context menu.
  144. /// ```dart
  145. /// setReaderViewContextMenuHelperListener((annotBean) {
  146. /// //show annotation attribute options widget
  147. /// _showAttributeOptionsModalBottomSheet(context, annotBean,(AnnotAttributeBean bean) {
  148. /// //modify annotation attribute
  149. /// modifyAnnotationAttribute(
  150. /// annotationType: annotBean.annotType, bean: bean);
  151. /// }, (){
  152. /// dismissModifyAnnotationAttribute();
  153. /// });
  154. /// });
  155. ///
  156. /// ```
  157. void modifyAnnotationAttribute(
  158. {required AnnotationType annotationType,
  159. required AnnotAttributeBean bean}) async {
  160. await _modifyAnnotAttrChannel.invokeMethod(
  161. Functions.modifyAnnotAttribute, bean.toMapValues(annotationType));
  162. }
  163. /// Please call this method after the annotation attribute adjustment interface is closed.
  164. void dismissModifyAnnotationAttribute() async {
  165. await _modifyAnnotAttrChannel
  166. .invokeMethod(Functions.dismissModifyAnnotationAttr);
  167. }
  168. ///get current CPDFReaderView scroll direction
  169. ///true : [ScrollDirection.vertical]
  170. ///false : [ScrollDirection.horizontal]
  171. ///```dart
  172. /// bool isVertical = await scrollDirectionIsVerticalMode();
  173. /// ```
  174. Future<bool> scrollDirectionIsVerticalMode() async {
  175. String scrollDirection =
  176. await _methodChannel.invokeMethod(Functions.getScrollDirection);
  177. return scrollDirection == ScrollDirection.vertical;
  178. }
  179. /// set CPDFReaderView scroll direction
  180. ///```dart
  181. ///bool isVertical = await setScrollDirection(direction);
  182. ///
  183. ///```
  184. /// Returns the current scroll direction
  185. /// true : [ScrollDirection.vertical]
  186. /// false : [ScrollDirection.horizontal]
  187. ///
  188. Future<bool> setScrollDirection(String direction) async {
  189. String scrollDirection = await _methodChannel.invokeMethod(
  190. Functions.setScrollDirection,
  191. {EventParameters.scrollDirection: direction});
  192. return scrollDirection == ScrollDirection.vertical;
  193. }
  194. ///Get the current CPDFReaderView page mode
  195. ///```dart
  196. /// bool doublePage = await isDoublePage();
  197. ///
  198. ///```
  199. Future<bool> isDoublePage() async {
  200. return await _methodChannel.invokeMethod(Functions.getPageMode);
  201. }
  202. ///set page mode
  203. ///true: double page
  204. ///false: single page
  205. ///```dart
  206. /// bool isDoublePage = await setPageMode(doublePage);
  207. ///
  208. ///```
  209. Future<bool> setPageMode(bool doublePage) async {
  210. return await _methodChannel.invokeMethod(
  211. Functions.setPageMode, {EventParameters.isDoublePage: doublePage});
  212. }
  213. ///Get whether 'CPDFReaderView' is in continuous scrolling mode
  214. ///```dart
  215. /// bool isContinue = await isContinueMode();
  216. /// ```
  217. Future<bool> isContinueMode() async {
  218. return await _methodChannel.invokeMethod(Functions.getPageContinue);
  219. }
  220. ///Set page scrolling mode
  221. ///true: continue Mode
  222. ///```dart
  223. /// bool isContinueMode = await setIsContinueMode(isContinueMode);
  224. /// ```
  225. Future<bool> setIsContinueMode(bool isContinueMode) async {
  226. return await _methodChannel.invokeMethod(Functions.setPageContinue,
  227. {EventParameters.isContinueMode: isContinueMode});
  228. }
  229. ///Get whether 'ComPDFReaderView' is in cover display mode
  230. ///```dart
  231. /// bool isCoverPageMode = await isCoverPageMode();
  232. /// ```
  233. Future<bool> isCoverPageMode() async {
  234. return await _methodChannel.invokeMethod(Functions.isCoverPageMode);
  235. }
  236. ///set is cover display mode
  237. ///true: is cover page mode
  238. ///```dart
  239. /// bool isCoverPageMode = await setIsCoverPageMode(isCoverPageMode);
  240. /// ```
  241. Future<bool> setIsCoverPageMode(bool isCoverPageMode) async {
  242. return await _methodChannel.invokeMethod(Functions.setCoverPageMode,
  243. {EventParameters.isCoverPageMode: isCoverPageMode});
  244. }
  245. ///Get 'ComPDFReaderView' whether to crop the display mode
  246. ///```dart
  247. /// bool isCropMode = await isCropPageMode();
  248. /// ```
  249. Future<bool> isCropPageMode() async {
  250. return await _methodChannel.invokeMethod(Functions.isCropPageMode);
  251. }
  252. ///set is crop display mode
  253. ///true: is crop page mode
  254. ///```dart
  255. /// bool isCropMode = await setIsCropPageMode(isCropPageMode);
  256. /// ```
  257. Future<bool> setIsCropPageMode(bool isCropPageMode) async {
  258. return await _methodChannel.invokeMethod(Functions.setIsCropPageMode,
  259. {EventParameters.isCropPageMode: isCropPageMode});
  260. }
  261. /// get reader view background color
  262. ///```dart
  263. /// int color = await getReadBackgroundColor();
  264. ///```
  265. /// return current reader view background color
  266. Future<int> getReadBackgroundColor() async {
  267. return await _methodChannel.invokeMethod(Functions.getReadBackgroundColor);
  268. }
  269. /// set reader view background color
  270. /// Changing the appearance mode will change the PDF rendering style, but it does not modify the PDF on disk. To set the color mode:
  271. /// normal color mode : #FFFFFF
  272. /// Dark mode : #000000
  273. /// Sepia mode : #FFEFBE
  274. /// Reseda mode : #CDE6D0
  275. /// Custom color mode : any hexadecimal value
  276. ///```dart
  277. /// int color = await setReadBackgroundColor(backgroundColor);
  278. ///
  279. ///```
  280. ///return current reader view background color
  281. Future<int> setReadBackgroundColor(int backgroundColor) async {
  282. return await _methodChannel.invokeMethod(Functions.setReadBackgroundColor,
  283. {EventParameters.readBackgroundColor: backgroundColor});
  284. }
  285. /// Get annotation attributes based on annotation type
  286. ///
  287. /// ```dart
  288. /// AnnotAttributeBean highlight = await getAnnotAttribute(AnnotType.highlight);
  289. /// ```
  290. /// return annotation attributes color and alpha
  291. Future<AnnotAttributeBean> getAnnotAttribute(AnnotationType annotType) async {
  292. Map<dynamic, dynamic> annotAttrMap = await _methodChannel.invokeMethod(
  293. Functions.getAnnotAttribute, {EventParameters.annotType: annotType.name});
  294. return AnnotAttributeBean.parseMapValue(annotType, annotAttrMap)
  295. ..showShapeTypeWidget = true;
  296. }
  297. ///set annotation attributes
  298. ///```dart
  299. /// AnnotAttributeBean attr = await setAnnotAttribute(
  300. /// annotType: annotBean.annotType, color: color, alpha: alpha);
  301. ///```
  302. /// return annotation attributes color and alpha
  303. void setAnnotAttribute(
  304. {required AnnotationType annotationType,
  305. required AnnotAttributeBean bean}) async {
  306. await _methodChannel.invokeMethod(Functions.setAnnotAttribute, {
  307. EventParameters.annotType: annotationType.name,
  308. EventParameters.annotAttribute: bean.toMapValues(annotationType)
  309. });
  310. }
  311. /// set CPDFReaderView current focused type
  312. /// This setting will change the type of action you swipe on CPDFReaderView
  313. /// If you need to set it as a browsing state, please set
  314. /// focusedType :[AnnotationType.unknown]
  315. /// touchMode : [TouchMode.browse]
  316. ///
  317. /// If you need to set to add Annotation state, please set
  318. /// focusedType : [AnnotationType]
  319. /// touchMode : [TouchMode.add_annot]
  320. ///```dart
  321. /// setCPDFReaderViewFocusType(annotationType)
  322. ///```
  323. ///
  324. void setCPDFReaderViewFocusType(AnnotationType annotationType) async {
  325. await _methodChannel.invokeMethod(Functions.setCurrentFocusedType, {
  326. EventParameters.touchMode: annotationType == AnnotationType.unknown
  327. ? TouchMode.browse.name
  328. : TouchMode.add_annot.name,
  329. EventParameters.focusedType: annotationType.name
  330. });
  331. }
  332. /// get pdf document file page total count
  333. Future<int> getDocumentPageCount() async{
  334. Map<dynamic, dynamic> map = await _methodChannel.invokeMethod(Functions.getDocumentPageInfo);
  335. return map[EventParameters.documentPageCount];
  336. }