Skip to content

Commit

Permalink
feat: add clear button to search box (#190)
Browse files Browse the repository at this point in the history
* feat: add clear button to search box

* Refactor search functionality

* Revert "feat: add clear button to search box"

This reverts commit f2ad0d1.

* Fix search bar clear button behavior

* Fix search appbar behavior
  • Loading branch information
MiaoMint authored Jan 18, 2024
1 parent 0c07809 commit 9102034
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
25 changes: 23 additions & 2 deletions lib/views/pages/search/extension_searcher_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class _ExtensionSearcherPageState extends fluent.State<ExtensionSearcherPage> {
Map<String, ExtensionFilter>? _filters;
// 初始化一开始选择的选项
Map<String, List<String>> _selectedFilters = {};
// 缓存的选项

late final _textEditingController = TextEditingController(text: _keyWord);

@override
void initState() {
Expand All @@ -52,6 +53,12 @@ class _ExtensionSearcherPageState extends fluent.State<ExtensionSearcherPage> {
});
}

@override
void dispose() {
_textEditingController.dispose();
super.dispose();
}

_initFilters() async {
_filters = await _runtime.createFilter();
_filters!.forEach((key, value) {
Expand Down Expand Up @@ -200,7 +207,7 @@ class _ExtensionSearcherPageState extends fluent.State<ExtensionSearcherPage> {
return Scaffold(
appBar: SearchAppBar(
title: _runtime.extension.name,
textEditingController: TextEditingController(text: _keyWord),
textEditingController: _textEditingController,
onChanged: (value) {
if (value.isEmpty) {
_onSearch(value);
Expand Down Expand Up @@ -247,6 +254,18 @@ class _ExtensionSearcherPageState extends fluent.State<ExtensionSearcherPage> {
}

Widget _buildDesktop(BuildContext context) {
final suffix = Row(mainAxisSize: MainAxisSize.min, children: [
Padding(
padding: const EdgeInsetsDirectional.only(start: 2.0),
child: fluent.IconButton(
icon: const Icon(fluent.FluentIcons.chrome_close, size: 9.0),
onPressed: () {
_textEditingController.clear();
_onSearch("");
},
),
),
]);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expand Down Expand Up @@ -287,6 +306,8 @@ class _ExtensionSearcherPageState extends fluent.State<ExtensionSearcherPage> {
_onSearch(value);
}
},
suffix: suffix,
suffixMode: fluent.OverlayVisibilityMode.editing,
onSubmitted: _onSearch,
placeholder: 'search.hint-text'.i18n,
),
Expand Down
14 changes: 14 additions & 0 deletions lib/views/pages/search/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ class _SearchPageState extends State<SearchPage> {
}

Widget _buildDesktopSearch(BuildContext context) {
final suffix = Row(mainAxisSize: MainAxisSize.min, children: [
Padding(
padding: const EdgeInsetsDirectional.only(start: 2.0),
child: fluent.IconButton(
icon: const Icon(fluent.FluentIcons.chrome_close, size: 9.0),
onPressed: () {
c.search.value = '';
setState(() {});
},
),
),
]);
return Obx(
() => Column(
children: [
Expand Down Expand Up @@ -210,6 +222,8 @@ class _SearchPageState extends State<SearchPage> {
controller:
TextEditingController(text: c.search.value),
placeholder: "search.hint-text".i18n,
suffix: suffix,
suffixMode: fluent.OverlayVisibilityMode.editing,
onChanged: (value) {
if (value.isEmpty) {
c.search.value = '';
Expand Down
47 changes: 38 additions & 9 deletions lib/views/widgets/search_appbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,53 @@ class _SearchAppBarState extends State<SearchAppBar> {
@override
Widget build(BuildContext context) {
return AppBar(
leading: _showSearch
? IconButton(
onPressed: () {
setState(() {
widget.textEditingController.clear();
widget.onSubmitted?.call('');
_showSearch = false;
});
},
icon: const Icon(Icons.arrow_back),
)
: null,
title: _showSearch
? TextField(
controller: widget.textEditingController,
decoration: InputDecoration(
hintText: widget.hintText ?? widget.title,
border: InputBorder.none,
? PopScope(
canPop: false,
onPopInvoked: (_) async {
if (_showSearch) {
setState(() {
widget.textEditingController.clear();
widget.onSubmitted?.call('');
_showSearch = false;
});
return;
}
},
child: TextField(
controller: widget.textEditingController,
decoration: InputDecoration(
hintText: widget.hintText ?? widget.title,
border: InputBorder.none,
),
autofocus: true,
onChanged: widget.onChanged,
onSubmitted: widget.onSubmitted,
),
autofocus: true,
onChanged: widget.onChanged,
onSubmitted: widget.onSubmitted,
)
: Text(widget.title),
actions: [
IconButton(
onPressed: () {
setState(() {
if (_showSearch) {
widget.textEditingController.clear();
widget.onSubmitted?.call('');
return;
}
_showSearch = !_showSearch;
if (!_showSearch) widget.onSubmitted?.call("");
});
},
icon: Icon(_showSearch ? Icons.close : Icons.search),
Expand Down

0 comments on commit 9102034

Please sign in to comment.