Add clearable search button to home search bar#35
Conversation
There was a problem hiding this comment.
Code Review
This pull request converts the SearchCard widget into a StatefulWidget to introduce a clearable search field functionality. Key changes include adding a TextEditingController listener to track input and dynamically switching the suffix icon between a clear button and a filter icon. Review feedback suggests standardizing the README header, optimizing the text change listener to prevent unnecessary state updates, and adjusting the suffix icon implementation to maintain consistent UI styling when the field is empty.
| ## ✅ Update | ||
| - Added a clearable search field in the home screen search card, making it easier to reset search text quickly. |
There was a problem hiding this comment.
The "Update" section header is non-standard for a project README. It is generally better to list new functionality under a "Features" section or simply update the project description to reflect the current state of the application.
| ## ✅ Update | |
| - Added a clearable search field in the home screen search card, making it easier to reset search text quickly. | |
| ## 🚀 Features | |
| - Added a clearable search field in the home screen search card, making it easier to reset search text quickly. |
| void _onTextChanged() { | ||
| setState(() { | ||
| _hasText = _searchControl.text.isNotEmpty; | ||
| }); | ||
| } |
There was a problem hiding this comment.
Calling setState on every keystroke is slightly inefficient when the _hasText state hasn't actually changed (for example, when typing the second or third character). You can optimize this by checking if the state needs to be updated before calling setState.
| void _onTextChanged() { | |
| setState(() { | |
| _hasText = _searchControl.text.isNotEmpty; | |
| }); | |
| } | |
| void _onTextChanged() { | |
| if (_hasText != _searchControl.text.isNotEmpty) { | |
| setState(() { | |
| _hasText = _searchControl.text.isNotEmpty; | |
| }); | |
| } | |
| } |
| suffixIcon: IconButton( | ||
| icon: Icon( | ||
| _hasText ? Icons.clear : Icons.filter_list, | ||
| color: Colors.black, | ||
| ), | ||
| onPressed: _hasText | ||
| ? () { | ||
| _searchControl.clear(); | ||
| } | ||
| : null, | ||
| ), |
There was a problem hiding this comment.
Using an IconButton with onPressed: null when there is no text causes the filter_list icon to appear disabled (usually greyed out) and adds extra padding compared to a standard Icon. Consider conditionally returning a plain Icon when the field is empty to maintain the original UI appearance and avoid the disabled styling.
| suffixIcon: IconButton( | |
| icon: Icon( | |
| _hasText ? Icons.clear : Icons.filter_list, | |
| color: Colors.black, | |
| ), | |
| onPressed: _hasText | |
| ? () { | |
| _searchControl.clear(); | |
| } | |
| : null, | |
| ), | |
| suffixIcon: _hasText | |
| ? IconButton( | |
| icon: Icon( | |
| Icons.clear, | |
| color: Colors.black, | |
| ), | |
| onPressed: () { | |
| _searchControl.clear(); | |
| }, | |
| ) | |
| : Icon( | |
| Icons.filter_list, | |
| color: Colors.black, | |
| ), |
Updated [search_card.dart]
Converted SearchCard to a StatefulWidget
Added an interactive clear button that appears when search text is entered
Clears text when the button is tapped
Updated [README.md]
with a note describing the new feature