/// /// 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/core/cpdf_view_ctrl.dart'; import 'package:compdfkit_flutter/core/document/textsearcher/cpdf_search_data.dart'; import 'package:compdfkit_flutter/core/document/textsearcher/cpdf_text_searcher.dart'; import 'package:compdfkit_flutter/widgets/common/views/cpdf_action_widget.dart'; import 'package:compdfkit_flutter/widgets/viewer/pdfsearch/cpdf_search_result_list_page.dart'; import 'package:flutter/material.dart'; import '../../../common/util/Strings.dart'; import '../../../common/util/cpdf_keyboard_util.dart'; import '../../../theme/themes.dart'; class CPDFSearchToolbar extends StatefulWidget { final bool isDark; final CPDFViewCtrl ctrl; final VoidCallback? onBack; const CPDFSearchToolbar( {Key? key, this.isDark = false, required this.ctrl, this.onBack}) : super(key: key); @override State createState() => _CPDFSearchToolbarState(); } class _CPDFSearchToolbarState extends State { final TextEditingController _textEditingController = TextEditingController(); bool _hasSearchResult = false; String _keyword = ''; @override void dispose() { super.dispose(); _textEditingController.dispose(); } @override Widget build(BuildContext context) { return Theme( data: widget.isDark ? comPDFKitDarkTheme : comPDFKitLightTheme.copyWith( colorScheme: comPDFKitLightTheme.colorScheme .copyWith(primary: const Color(0xFFFAFCFF))), child: Builder(builder: (context) { return AppBar( leading: IconButton( onPressed: widget.onBack, icon: Icon(Icons.arrow_back, color: Theme.of(context).colorScheme.onBackground), ), title: TextField( controller: _textEditingController, style: Theme.of(context).textTheme.titleMedium, decoration: const InputDecoration( focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.transparent, width: 0) ), enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.transparent, width: 0)), hintText: Strings.Search, hintStyle: TextStyle(color: Color(0xFFCCCCCC), fontSize: 18), ), maxLines: 1, textInputAction: TextInputAction.search, autofocus: false, onEditingComplete: () async { _keyword = _textEditingController.text; if (_textEditingController.text.isNotEmpty) { CPDFKeyboardUtils.hideKeyShowUnfocused(); CPDFTextSearcher textSearcher = widget.ctrl.document.textSearcher; var data = await textSearcher.searchFirst( keywords: _textEditingController.text); if (data != null) { textSearcher.searchBegin( pageIndex: data.pageIndex, textRangeIndex: data.textRangeIndex); } setState(() { _hasSearchResult = data != null; }); } }, ), titleSpacing: 10, elevation: 2, actions: [ if (_hasSearchResult) ...{ CPDFActionWidget( image: 'assets/images/ic_arrow_search_backward.png', onPressed: (context) { widget.ctrl.document.textSearcher.searchBackward(); }, ), CPDFActionWidget( image: 'assets/images/ic_arrow_search_forward.png', onPressed: (context) { widget.ctrl.document.textSearcher.searchForward(); }, ), CPDFActionWidget( image: 'assets/images/ic_searchlist.png', onPressed: (context) async { CPDFSearchData? data = await Navigator.push(context, MaterialPageRoute(builder: (_) { return CPDFSearchResultListPage( isDark: widget.isDark, document: widget.ctrl.document, keywords: _keyword); })); if (data != null) { widget.ctrl.setDisplayPageIndex(data.pageIndex); widget.ctrl.document.textSearcher.searchBegin( pageIndex: data.pageIndex, textRangeIndex: data.textRangeIndex); } }, ) } ], ); })); } }