-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Add searchFieldLabel to SearchDelegate in order to show a custom hint #36409
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
Conversation
shihaohong
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a few code formatting and API doc suggestions, but otherwise, LGTM
| assert(debugCheckHasMaterialLocalizations(context)); | ||
| final ThemeData theme = widget.delegate.appBarTheme(context); | ||
| final String searchFieldLabel = MaterialLocalizations.of(context).searchFieldLabel; | ||
| final String searchFieldLabel = widget.delegate.searchFieldLabel ?? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is more readable:
final String searchFieldLabel = widget.delegate.searchFieldLabel
?? MaterialLocalizations.of(context).searchFieldLabel;
| _queryTextController.text = value; | ||
| } | ||
|
|
||
| /// The hint text to be shown when the search field is empty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// The hint text to be shown when the search field is empty. | |
| /// The hint text that is shown in the search field when it is empty. |
Add some documentation to describe what the hint text defaults to if this value is null in another line:
If this value is set to null, the value of MaterialLocalizations.of(context).searchFieldLabel will be used instead.
Also, maybe a code sample for how to use this (as you did with the _TestSearchDelegate in the tests) for other developers.
| expect(selectedResults, <String>['Result Foo']); | ||
| }); | ||
|
|
||
| testWidgets('Showing custom search hint', (WidgetTester tester) async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| testWidgets('Showing custom search hint', (WidgetTester tester) async { | |
| testWidgets('Custom searchFieldLabel value', (WidgetTester tester) async { |
| testWidgets('Showing custom search hint', (WidgetTester tester) async { | ||
| const String searchHint = 'custom search hint'; | ||
| final String defaultSearchHint = | ||
| const DefaultMaterialLocalizations().searchFieldLabel; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be in the same line as the previous line
| const DefaultMaterialLocalizations().searchFieldLabel; | ||
|
|
||
| final _TestSearchDelegate delegate = | ||
| _TestSearchDelegate(searchHint: searchHint); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be in the same line as the previous line
| expect(find.text(defaultSearchHint), findsNothing); | ||
| }); | ||
|
|
||
| testWidgets('Showing default search label if no custom label is given', (WidgetTester tester) async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| testWidgets('Showing default search label if no custom label is given', (WidgetTester tester) async { | |
| testWidgets('Default searchFieldLabel is used when it is set to null', (WidgetTester tester) async { |
|
|
||
| testWidgets('Showing default search label if no custom label is given', (WidgetTester tester) async { | ||
| final String searchHint = | ||
| const DefaultMaterialLocalizations().searchFieldLabel; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be in the same line as the previous line
|
Thank you for the review, I applied your suggestions. |
|
You missed one of the suggestions ( |
| /// @override | ||
| /// String get searchFieldLabel => this.searchHint; | ||
| /// | ||
| /// @override |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// @override | |
| /// @override |
| /// | ||
| /// final String searchHint; | ||
| /// | ||
| /// CustomSearchHintDelegate({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constructor should typically be above every other member/method, so just move this above searchHint
| /// | ||
| /// ```dart | ||
| /// class CustomSearchHintDelegate extends SearchDelegate { | ||
| /// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for newline here
|
Yeah, I don't see why my changes would have caused problems specifically on windows. I merged with master and applied the rest of your suggestions. |
|
It was brought to my attention that this change is related to #28807, except they have some additional functionality, such as keyboard type and the input action. While I don't necessarily think this should stop your PR if you don't want to include those features, it might be worth looking over some of their comments/discussion and seeing if there is something there that you can apply here, notably suggestions like the one in this comment. |
|
I don't mind adding support for textInputAction and keyboardType. I hope it's not considered rude to "steal" features from another PR, but it seems @somarkoe isn't very responsive in his PR at the moment. I made searchFieldLabel, keyboardType, and textInputAction final fields the can be set in the constructor. I also find this approach cleaner, albeit maybe little bit less discoverable. I tried to compensate for this by moving the code sample to the constructor. |
shihaohong
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Thanks for doing the additional work of adding the new properties in.
Just a few minor styling suggestions. LGTM
One thing I would definitely do is give @somarkoe credit in the first comment of this PR, something along the lines of this PR. |
Co-Authored-By: Shi-Hao Hong <shihaohong@google.com>
Not rude at all! I actually quite appreciate it. I made the initial PR when I had a endless amounts of free time back in March, but once it actually was pulled into the review process I was swamped in both personal life events as well as work responsibilities and that hasn't really changed! |
|
Applied all suggestions. |
…flutter#36409) * Add searchFieldLabel to SearchDelegate in order to show a custom hint label. * Add support for specifying textInputAction and keyboardType in SearchDelegate
Description
This fixes issue #18831. A new, optional member "searchFieldLabel" of SearchDelegate was added to specify the label of the search field. If it's specified, it it displayed instead of the default value from DefaultMaterialLocalizations.
Credits to @somarkoe for preparing the addition of some of the added features in #28807
Related Issues
#18831
Tests
I added the following tests:
"Showing custom search hint": a widget test that verifies that the custom search field label is indeed displayed
"Showing default search label if no custom label is given": a widget test that verifies if no searchFieldLabel is specified, the current behaviour of looking up the value from DefaultMaterialLocalizations is unchanged.
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]). This will ensure a smooth and quick review process.///).flutter analyze --flutter-repo) does not report any problems on my PR.Breaking Change
Does your PR require Flutter developers to manually update their apps to accommodate your change?