/// /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved. /// /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. /// This notice may not be removed from this file. import 'package:compdfkit_flutter/common/util/Strings.dart'; import 'package:compdfkit_flutter/core/document/cpdf_bookmark.dart'; import 'package:compdfkit_flutter/core/document/cpdf_document.dart'; import 'package:compdfkit_flutter/widgets/common/dialog/cpd_input_dialog_widget.dart'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import '../../../common/dialog/cpdf_base_input_dialog_widget.dart'; class CPDFBookmarkPage extends StatefulWidget { final int currentPageIndex; final bool isDark; final CPDFDocument document; const CPDFBookmarkPage( {Key? key, this.isDark = false, required this.document, required this.currentPageIndex}) : super(key: key); @override State createState() => _CPDFBookmarkPageState(); } class _CPDFBookmarkPageState extends State with AutomaticKeepAliveClientMixin{ late Future> _bookmarks; @override void initState() { super.initState(); _bookmarks = widget.document.getBookmarks(); } @override Widget build(BuildContext context) { return Stack( children: [ // _item(CPDFBookmark('ComPDFKit SDK', 0)), Positioned( child: FutureBuilder( future: _bookmarks, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) { List bookmarks = snapshot.data!; return ListView.builder( itemCount: bookmarks.length, itemBuilder: (context, index) { return _item(bookmarks[index]); }); } else { return const Padding(padding: EdgeInsets.zero); } }, )), Positioned( bottom: 16, right: 16, child: FloatingActionButton( onPressed: () { addBookmark(context); }, child: const Icon( Icons.add, color: Colors.white, ), )) ], ); } Widget _item(CPDFBookmark bookmark) { return Ink( height: 46.5, child: InkWell( onTap: () { Navigator.pop(context, bookmark.pageIndex); }, child: Column( children: [ SizedBox( height: 46, child: Row( children: [ Expanded( child: Padding( padding: const EdgeInsets.only(left: 16), child: Text(bookmark.title, maxLines: 1, overflow: TextOverflow.ellipsis, style: Theme.of(context).textTheme.bodyMedium))), Row( mainAxisAlignment: MainAxisAlignment.end, children: [ Text( '${Strings.Page} ${(bookmark.pageIndex + 1)}', style: Theme.of(context).textTheme.bodyMedium, ), const Padding(padding: EdgeInsets.only(right: 6)), PopupMenuButton( elevation: 2, offset: const Offset(-20, 45), icon: Image.asset( 'assets/images/ic_vertical_more.png', package: 'compdfkit_flutter', width: 24, height: 24, color: Theme.of(context).colorScheme.onPrimary, ), itemBuilder: (context) => [ const PopupMenuItem( value: 1, child: Text(Strings.Edit)), const PopupMenuItem( value: 2, child: Text(Strings.Delete)), ], onSelected: (value) async { if (value == 1) { updateBookmark(context, bookmark); } else { removeBookmark(bookmark); } }, ), const Padding(padding: EdgeInsets.only(right: 16)) ], ) ], )), const Divider( height: 0.5, ) ], ), ), ); } Future addBookmark(BuildContext context) async { bool hasBookmark = await widget.document.hasBookmark(widget.currentPageIndex); if (hasBookmark) { Fluttertoast.showToast( msg: Strings.HasBookmarkTips, toastLength: Toast.LENGTH_LONG); return; } if (!context.mounted) { return; } String? value = await showCPDFInputDialog(context, title: Strings.AddBookmarks, isDark: widget.isDark, hintText: Strings.BookmarkTitle, cancelText: Strings.Cancel, positiveText: Strings.OK, onCancel: () { Navigator.pop(context); }, onPositive: (value) { Navigator.pop(context, value); }); if (value != null) { widget.document.addBookmark(CPDFBookmark(value, widget.currentPageIndex)); setState(() { _bookmarks = widget.document.getBookmarks(); }); } } Future removeBookmark(CPDFBookmark bookmark) async { bool result = await widget.document.removeBookmark(bookmark.pageIndex); if (result) { setState(() { _bookmarks = widget.document.getBookmarks(); }); } } Future updateBookmark( BuildContext context, CPDFBookmark bookmark) async { String? value = await showCPDFInputDialog(context, title: Strings.EditBookmarks, text: bookmark.title, isDark: widget.isDark, hintText: Strings.BookmarkTitle, cancelText: Strings.Cancel, positiveText: Strings.OK, onCancel: () { Navigator.pop(context); }, onPositive: (value) { Navigator.pop(context, value); }); if (value != null) { widget.document.updateBookmark(bookmark.pageIndex, value); setState(() { _bookmarks = widget.document.getBookmarks(); }); } } @override bool get wantKeepAlive => true; }