123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- ///
- /// 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<CPDFBookmarkPage> createState() => _CPDFBookmarkPageState();
- }
- class _CPDFBookmarkPageState extends State<CPDFBookmarkPage> with AutomaticKeepAliveClientMixin{
- late Future<List<CPDFBookmark>> _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<CPDFBookmark> 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<void> 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<void> removeBookmark(CPDFBookmark bookmark) async {
- bool result = await widget.document.removeBookmark(bookmark.pageIndex);
- if (result) {
- setState(() {
- _bookmarks = widget.document.getBookmarks();
- });
- }
- }
- Future<void> 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;
- }
|