events.dart 13 KB

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