cpdf_bookmark_page.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ///
  2. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  3. ///
  4. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. /// This notice may not be removed from this file.
  8. import 'package:compdfkit_flutter/common/util/Strings.dart';
  9. import 'package:compdfkit_flutter/core/document/cpdf_bookmark.dart';
  10. import 'package:compdfkit_flutter/core/document/cpdf_document.dart';
  11. import 'package:compdfkit_flutter/widgets/common/dialog/cpd_input_dialog_widget.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:fluttertoast/fluttertoast.dart';
  14. import '../../../common/dialog/cpdf_base_input_dialog_widget.dart';
  15. class CPDFBookmarkPage extends StatefulWidget {
  16. final int currentPageIndex;
  17. final bool isDark;
  18. final CPDFDocument document;
  19. const CPDFBookmarkPage(
  20. {Key? key,
  21. this.isDark = false,
  22. required this.document,
  23. required this.currentPageIndex})
  24. : super(key: key);
  25. @override
  26. State<CPDFBookmarkPage> createState() => _CPDFBookmarkPageState();
  27. }
  28. class _CPDFBookmarkPageState extends State<CPDFBookmarkPage> with AutomaticKeepAliveClientMixin{
  29. late Future<List<CPDFBookmark>> _bookmarks;
  30. @override
  31. void initState() {
  32. super.initState();
  33. _bookmarks = widget.document.getBookmarks();
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. return Stack(
  38. children: [
  39. // _item(CPDFBookmark('ComPDFKit SDK', 0)),
  40. Positioned(
  41. child: FutureBuilder(
  42. future: _bookmarks,
  43. builder: (context, snapshot) {
  44. if (snapshot.connectionState == ConnectionState.done &&
  45. snapshot.hasData) {
  46. List<CPDFBookmark> bookmarks = snapshot.data!;
  47. return ListView.builder(
  48. itemCount: bookmarks.length,
  49. itemBuilder: (context, index) {
  50. return _item(bookmarks[index]);
  51. });
  52. } else {
  53. return const Padding(padding: EdgeInsets.zero);
  54. }
  55. },
  56. )),
  57. Positioned(
  58. bottom: 16,
  59. right: 16,
  60. child: FloatingActionButton(
  61. onPressed: () {
  62. addBookmark(context);
  63. },
  64. child: const Icon(
  65. Icons.add,
  66. color: Colors.white,
  67. ),
  68. ))
  69. ],
  70. );
  71. }
  72. Widget _item(CPDFBookmark bookmark) {
  73. return Ink(
  74. height: 46.5,
  75. child: InkWell(
  76. onTap: () {
  77. Navigator.pop(context, bookmark.pageIndex);
  78. },
  79. child: Column(
  80. children: [
  81. SizedBox(
  82. height: 46,
  83. child: Row(
  84. children: [
  85. Expanded(
  86. child: Padding(
  87. padding: const EdgeInsets.only(left: 16),
  88. child: Text(bookmark.title,
  89. maxLines: 1,
  90. overflow: TextOverflow.ellipsis,
  91. style:
  92. Theme.of(context).textTheme.bodyMedium))),
  93. Row(
  94. mainAxisAlignment: MainAxisAlignment.end,
  95. children: [
  96. Text(
  97. '${Strings.Page} ${(bookmark.pageIndex + 1)}',
  98. style: Theme.of(context).textTheme.bodyMedium,
  99. ),
  100. const Padding(padding: EdgeInsets.only(right: 6)),
  101. PopupMenuButton(
  102. elevation: 2,
  103. offset: const Offset(-20, 45),
  104. icon: Image.asset(
  105. 'assets/images/ic_vertical_more.png',
  106. package: 'compdfkit_flutter',
  107. width: 24,
  108. height: 24,
  109. color: Theme.of(context).colorScheme.onPrimary,
  110. ),
  111. itemBuilder: (context) => [
  112. const PopupMenuItem(
  113. value: 1, child: Text(Strings.Edit)),
  114. const PopupMenuItem(
  115. value: 2, child: Text(Strings.Delete)),
  116. ],
  117. onSelected: (value) async {
  118. if (value == 1) {
  119. updateBookmark(context, bookmark);
  120. } else {
  121. removeBookmark(bookmark);
  122. }
  123. },
  124. ),
  125. const Padding(padding: EdgeInsets.only(right: 16))
  126. ],
  127. )
  128. ],
  129. )),
  130. const Divider(
  131. height: 0.5,
  132. )
  133. ],
  134. ),
  135. ),
  136. );
  137. }
  138. Future<void> addBookmark(BuildContext context) async {
  139. bool hasBookmark =
  140. await widget.document.hasBookmark(widget.currentPageIndex);
  141. if (hasBookmark) {
  142. Fluttertoast.showToast(
  143. msg: Strings.HasBookmarkTips, toastLength: Toast.LENGTH_LONG);
  144. return;
  145. }
  146. if (!context.mounted) {
  147. return;
  148. }
  149. String? value = await showCPDFInputDialog(context,
  150. title: Strings.AddBookmarks,
  151. isDark: widget.isDark,
  152. hintText: Strings.BookmarkTitle,
  153. cancelText: Strings.Cancel,
  154. positiveText: Strings.OK, onCancel: () {
  155. Navigator.pop(context);
  156. }, onPositive: (value) {
  157. Navigator.pop(context, value);
  158. });
  159. if (value != null) {
  160. widget.document.addBookmark(CPDFBookmark(value, widget.currentPageIndex));
  161. setState(() {
  162. _bookmarks = widget.document.getBookmarks();
  163. });
  164. }
  165. }
  166. Future<void> removeBookmark(CPDFBookmark bookmark) async {
  167. bool result = await widget.document.removeBookmark(bookmark.pageIndex);
  168. if (result) {
  169. setState(() {
  170. _bookmarks = widget.document.getBookmarks();
  171. });
  172. }
  173. }
  174. Future<void> updateBookmark(
  175. BuildContext context, CPDFBookmark bookmark) async {
  176. String? value = await showCPDFInputDialog(context,
  177. title: Strings.EditBookmarks,
  178. text: bookmark.title,
  179. isDark: widget.isDark,
  180. hintText: Strings.BookmarkTitle,
  181. cancelText: Strings.Cancel,
  182. positiveText: Strings.OK, onCancel: () {
  183. Navigator.pop(context);
  184. }, onPositive: (value) {
  185. Navigator.pop(context, value);
  186. });
  187. if (value != null) {
  188. widget.document.updateBookmark(bookmark.pageIndex, value);
  189. setState(() {
  190. _bookmarks = widget.document.getBookmarks();
  191. });
  192. }
  193. }
  194. @override
  195. bool get wantKeepAlive => true;
  196. }