cpdf_search_toolbar.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/core/cpdf_view_ctrl.dart';
  9. import 'package:compdfkit_flutter/core/document/textsearcher/cpdf_search_data.dart';
  10. import 'package:compdfkit_flutter/core/document/textsearcher/cpdf_text_searcher.dart';
  11. import 'package:compdfkit_flutter/widgets/common/views/cpdf_action_widget.dart';
  12. import 'package:compdfkit_flutter/widgets/viewer/pdfsearch/cpdf_search_result_list_page.dart';
  13. import 'package:flutter/material.dart';
  14. import '../../../common/util/Strings.dart';
  15. import '../../../common/util/cpdf_keyboard_util.dart';
  16. import '../../../theme/themes.dart';
  17. class CPDFSearchToolbar extends StatefulWidget {
  18. final bool isDark;
  19. final CPDFViewCtrl ctrl;
  20. final VoidCallback? onBack;
  21. const CPDFSearchToolbar(
  22. {Key? key, this.isDark = false, required this.ctrl, this.onBack})
  23. : super(key: key);
  24. @override
  25. State<CPDFSearchToolbar> createState() => _CPDFSearchToolbarState();
  26. }
  27. class _CPDFSearchToolbarState extends State<CPDFSearchToolbar> {
  28. final TextEditingController _textEditingController = TextEditingController();
  29. bool _hasSearchResult = false;
  30. String _keyword = '';
  31. @override
  32. void dispose() {
  33. super.dispose();
  34. _textEditingController.dispose();
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. return Theme(
  39. data: widget.isDark
  40. ? comPDFKitDarkTheme
  41. : comPDFKitLightTheme.copyWith(
  42. colorScheme: comPDFKitLightTheme.colorScheme
  43. .copyWith(primary: const Color(0xFFFAFCFF))),
  44. child: Builder(builder: (context) {
  45. return AppBar(
  46. leading: IconButton(
  47. onPressed: widget.onBack,
  48. icon: Icon(Icons.arrow_back,
  49. color: Theme.of(context).colorScheme.onBackground),
  50. ),
  51. title: TextField(
  52. controller: _textEditingController,
  53. style: Theme.of(context).textTheme.titleMedium,
  54. decoration: const InputDecoration(
  55. focusedBorder: UnderlineInputBorder(
  56. borderSide: BorderSide(color: Colors.transparent, width: 0)
  57. ),
  58. enabledBorder: UnderlineInputBorder(
  59. borderSide: BorderSide(color: Colors.transparent, width: 0)),
  60. hintText: Strings.Search,
  61. hintStyle: TextStyle(color: Color(0xFFCCCCCC), fontSize: 18),
  62. ),
  63. maxLines: 1,
  64. textInputAction: TextInputAction.search,
  65. autofocus: false,
  66. onEditingComplete: () async {
  67. _keyword = _textEditingController.text;
  68. if (_textEditingController.text.isNotEmpty) {
  69. CPDFKeyboardUtils.hideKeyShowUnfocused();
  70. CPDFTextSearcher textSearcher =
  71. widget.ctrl.document.textSearcher;
  72. var data = await textSearcher.searchFirst(
  73. keywords: _textEditingController.text);
  74. if (data != null) {
  75. textSearcher.searchBegin(
  76. pageIndex: data.pageIndex,
  77. textRangeIndex: data.textRangeIndex);
  78. }
  79. setState(() {
  80. _hasSearchResult = data != null;
  81. });
  82. }
  83. },
  84. ),
  85. titleSpacing: 10,
  86. elevation: 2,
  87. actions: [
  88. if (_hasSearchResult) ...{
  89. CPDFActionWidget(
  90. image: 'assets/images/ic_arrow_search_backward.png',
  91. onPressed: (context) {
  92. widget.ctrl.document.textSearcher.searchBackward();
  93. },
  94. ),
  95. CPDFActionWidget(
  96. image: 'assets/images/ic_arrow_search_forward.png',
  97. onPressed: (context) {
  98. widget.ctrl.document.textSearcher.searchForward();
  99. },
  100. ),
  101. CPDFActionWidget(
  102. image: 'assets/images/ic_searchlist.png',
  103. onPressed: (context) async {
  104. CPDFSearchData? data = await Navigator.push(context,
  105. MaterialPageRoute(builder: (_) {
  106. return CPDFSearchResultListPage(
  107. isDark: widget.isDark,
  108. document: widget.ctrl.document,
  109. keywords: _keyword);
  110. }));
  111. if (data != null) {
  112. widget.ctrl.setDisplayPageIndex(data.pageIndex);
  113. widget.ctrl.document.textSearcher.searchBegin(
  114. pageIndex: data.pageIndex,
  115. textRangeIndex: data.textRangeIndex);
  116. }
  117. },
  118. )
  119. }
  120. ],
  121. );
  122. }));
  123. }
  124. }