events.dart 12 KB

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