Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add clear button to search box #190

Merged
merged 5 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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