pdf_bottom_annot_fun_widget.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_svg/flutter_svg.dart';
  3. import 'package:kmpdfkit_demo/widgets/constains.dart';
  4. import 'package:kmpdfkit_demo/widgets/events.dart';
  5. import 'package:kmpdfkit_demo/widgets/function/annot_attribute_options_widget.dart';
  6. import 'package:kmpdfkit_demo/widgets/function/attrwidget/attr_signature_list_widget.dart';
  7. import 'package:kmpdfkit_demo/widgets/models/annot_attribute_bean.dart';
  8. import 'package:kmpdfkit_demo/widgets/models/annot_bean.dart';
  9. /// pdf_bottom_annot_fun_widget.dart
  10. ///
  11. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  12. ///
  13. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  14. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  15. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  16. /// This notice may not be removed from this file.
  17. class PDFBottomAnnotFunWidget extends StatefulWidget {
  18. const PDFBottomAnnotFunWidget({Key? key}) : super(key: key);
  19. @override
  20. State<PDFBottomAnnotFunWidget> createState() =>
  21. _PDFBottomAnnotFunWidgetState();
  22. }
  23. class _PDFBottomAnnotFunWidgetState extends State<PDFBottomAnnotFunWidget> {
  24. AnnotationType selectAnnot = AnnotationType.unknown;
  25. var annotFunList = [
  26. AnnotBean(
  27. annotType: AnnotationType.highlight,
  28. normalImagePath: 'assets/images/ic_reader_highlight.svg',
  29. selectImagePath: 'assets/images/ic_reader_highlight_select.svg',
  30. attributeBean: AnnotAttributeBean(color: Colors.red),
  31. ),
  32. AnnotBean(
  33. annotType: AnnotationType.underline,
  34. normalImagePath: 'assets/images/ic_reader_underline.svg',
  35. selectImagePath: 'assets/images/ic_reader_underline_select.svg',
  36. attributeBean: AnnotAttributeBean(color: Colors.blue)),
  37. AnnotBean(
  38. annotType: AnnotationType.strikeout,
  39. normalImagePath: 'assets/images/ic_reader_strikethrough.svg',
  40. selectImagePath: 'assets/images/ic_reader_strikethrough_select.svg',
  41. attributeBean: AnnotAttributeBean(color: Colors.green)),
  42. AnnotBean(
  43. annotType: AnnotationType.squiggly,
  44. normalImagePath: 'assets/images/ic_reader_squiggly_underline.svg',
  45. selectImagePath:
  46. 'assets/images/ic_reader_squiggly_underline_select.svg',
  47. attributeBean: AnnotAttributeBean(color: Colors.pink)),
  48. AnnotBean(
  49. annotType: AnnotationType.ink,
  50. normalImagePath: 'assets/images/ic_reader_ink.svg',
  51. selectImagePath: 'assets/images/ic_reader_ink_select.svg',
  52. attributeBean: AnnotAttributeBean(color: Colors.yellow)),
  53. AnnotBean(
  54. annotType: AnnotationType.shape,
  55. normalImagePath: 'assets/images/ic_reader_shape.svg',
  56. selectImagePath: 'assets/images/ic_reader_shape_select.svg',
  57. attributeBean: AnnotAttributeBean(color: Colors.transparent)),
  58. AnnotBean(
  59. annotType: AnnotationType.freetext,
  60. normalImagePath: 'assets/images/ic_reader_freetext.svg',
  61. selectImagePath: 'assets/images/ic_reader_freetext_select.svg',
  62. attributeBean: AnnotAttributeBean(color: Colors.transparent)),
  63. AnnotBean(
  64. annotType: AnnotationType.signature,
  65. normalImagePath: 'assets/images/ic_reader_signature.svg',
  66. selectImagePath: 'assets/images/ic_reader_signature_select.svg',
  67. attributeBean: AnnotAttributeBean(color: Colors.transparent)),
  68. AnnotBean(
  69. annotType: AnnotationType.stamp,
  70. normalImagePath: 'assets/images/ic_reader_stamp.svg',
  71. selectImagePath: 'assets/images/ic_reader_stamp_select.svg',
  72. attributeBean: AnnotAttributeBean(color: Colors.transparent)),
  73. ];
  74. @override
  75. void initState() {
  76. super.initState();
  77. initAnnotAttr();
  78. setReaderViewContextMenuHelperListener((annotBean) {
  79. _attributeOptionsModalBottomSheet(context, annotBean,
  80. (AnnotAttributeBean bean) {
  81. modifyAnnotationAttribute(
  82. annotationType: annotBean.annotType, bean: bean);
  83. }, () {
  84. dismissModifyAnnotationAttribute();
  85. });
  86. });
  87. }
  88. @override
  89. Widget build(BuildContext context) {
  90. return annotFunctionMenuListWidget(selectAnnot, (annotBean) {
  91. var annot = selectAnnot == annotBean.annotType
  92. ? AnnotationType.unknown
  93. : annotBean.attributeBean.shapeType;
  94. setCPDFReaderViewFocusType(annot);
  95. setState(() {
  96. selectAnnot = selectAnnot == annotBean.annotType
  97. ? AnnotationType.unknown
  98. : annotBean.annotType;
  99. });
  100. if (selectAnnot == AnnotationType.signature) {
  101. showAttributeModalBottomSheet(context, annotBean);
  102. }
  103. }, (annotBean) {
  104. setCPDFReaderViewFocusType(annotBean.attributeBean.shapeType);
  105. setState(() {
  106. selectAnnot = annotBean.annotType;
  107. });
  108. showAttributeModalBottomSheet(context, annotBean);
  109. });
  110. }
  111. Widget annotFunctionMenuListWidget(
  112. AnnotationType selectAnnotType,
  113. Function(AnnotBean annotBean) onTap,
  114. Function(AnnotBean annotBean) onLongPress) {
  115. return Container(
  116. color: const Color(0xFFEFF4FD),
  117. height: 60,
  118. alignment: Alignment.centerLeft,
  119. child: ListView.builder(
  120. itemCount: annotFunList.length,
  121. scrollDirection: Axis.horizontal,
  122. primary: true,
  123. shrinkWrap: true,
  124. itemBuilder: (context, index) {
  125. AnnotBean bean = annotFunList[index];
  126. return Container(
  127. width: 60,
  128. padding: const EdgeInsets.all(16),
  129. height: 60,
  130. color: selectAnnotType == bean.annotType
  131. ? const Color(0xFFD5E3FE)
  132. : Colors.transparent,
  133. child: InkWell(
  134. onTap: () {
  135. onTap(bean);
  136. },
  137. onLongPress: () {
  138. onLongPress(bean);
  139. },
  140. child: Stack(
  141. children: [
  142. Positioned(
  143. top: 0,
  144. bottom: 0,
  145. left: 0,
  146. right: 0,
  147. child: Container(
  148. margin: const EdgeInsets.all(2),
  149. color: bean.attributeBean.color,
  150. width: double.infinity,
  151. height: double.infinity,
  152. ),
  153. ),
  154. Positioned(
  155. top: 0,
  156. bottom: 0,
  157. left: 0,
  158. right: 0,
  159. child: SvgPicture.asset(
  160. selectAnnotType == bean.annotType
  161. ? bean.selectImagePath!
  162. : bean.normalImagePath!,
  163. semanticsLabel: bean.annotType.name,
  164. ))
  165. ],
  166. ),
  167. ),
  168. );
  169. }));
  170. }
  171. void showAttributeModalBottomSheet(
  172. BuildContext context, AnnotBean annotBean) {
  173. _attributeOptionsModalBottomSheet(context, annotBean,
  174. (AnnotAttributeBean attributeBean) async {
  175. setAnnotAttribute(
  176. annotationType: annotBean.annotType, bean: attributeBean);
  177. AnnotBean bean = annotFunList
  178. .firstWhere((element) => element.annotType == annotBean.annotType);
  179. setState(() {
  180. bean.attributeBean = attributeBean;
  181. });
  182. }, () {
  183. setState(() {
  184. if (selectAnnot == AnnotationType.signature) {
  185. selectAnnot = AnnotationType.unknown;
  186. }
  187. });
  188. });
  189. }
  190. void _attributeOptionsModalBottomSheet(context, AnnotBean annotBean,
  191. AnnotAttributeOptionsCallback callback, VoidCallback? closeCallback) {
  192. Future<void> close = showModalBottomSheet<int>(
  193. context: context,
  194. barrierColor: Colors.transparent,
  195. isScrollControlled: true,
  196. useSafeArea: true,
  197. builder: (BuildContext context) {
  198. return AnnotAttributeOptionsWidget(
  199. attributeOptionsCallback: callback,
  200. annotBean: annotBean,
  201. );
  202. });
  203. close.then((value) {
  204. if (closeCallback != null) {
  205. closeCallback();
  206. }
  207. });
  208. }
  209. void initAnnotAttr() async {
  210. AnnotAttributeBean highlight =
  211. await getAnnotAttribute(AnnotationType.highlight);
  212. AnnotAttributeBean underline =
  213. await getAnnotAttribute(AnnotationType.underline);
  214. AnnotAttributeBean strikeout =
  215. await getAnnotAttribute(AnnotationType.strikeout);
  216. AnnotAttributeBean squiggly =
  217. await getAnnotAttribute(AnnotationType.squiggly);
  218. AnnotAttributeBean ink = await getAnnotAttribute(AnnotationType.ink);
  219. AnnotAttributeBean shape = await getAnnotAttribute(AnnotationType.shape);
  220. AnnotAttributeBean freeText =
  221. await getAnnotAttribute(AnnotationType.freetext);
  222. AnnotAttributeBean signature =
  223. await getAnnotAttribute(AnnotationType.signature);
  224. setState(() {
  225. for (var element in annotFunList) {
  226. switch (element.annotType) {
  227. case AnnotationType.highlight:
  228. element.attributeBean = highlight;
  229. break;
  230. case AnnotationType.underline:
  231. element.attributeBean = underline;
  232. break;
  233. case AnnotationType.strikeout:
  234. element.attributeBean = strikeout;
  235. break;
  236. case AnnotationType.squiggly:
  237. element.attributeBean = squiggly;
  238. break;
  239. case AnnotationType.unknown:
  240. break;
  241. case AnnotationType.ink:
  242. element.attributeBean = ink;
  243. break;
  244. case AnnotationType.shape:
  245. element.attributeBean = shape;
  246. break;
  247. case AnnotationType.freetext:
  248. element.attributeBean = freeText;
  249. break;
  250. case AnnotationType.signature:
  251. element.attributeBean = signature;
  252. break;
  253. default:
  254. break;
  255. }
  256. }
  257. });
  258. }
  259. }