Add autocorrect and enableSuggestions to SearchDelegate (#154932)

Add `autocorrect` and `enableSuggestions` to `SearchDelegate`, so that autocompletion can be disabled in search. 

*List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.*

* https://github.com/flutter/flutter/issues/98241

*If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
This commit is contained in:
Thomas Hareau 2024-10-03 20:14:51 +02:00 committed by GitHub
parent c8c2f4651a
commit 91eb8d226d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 2 deletions

View File

@ -155,6 +155,8 @@ abstract class SearchDelegate<T> {
this.searchFieldDecorationTheme,
this.keyboardType,
this.textInputAction = TextInputAction.search,
this.autocorrect = true,
this.enableSuggestions = true,
}) : assert(searchFieldStyle == null || searchFieldDecorationTheme == null);
/// Suggestions shown in the body of the search page while the user types a
@ -365,6 +367,12 @@ abstract class SearchDelegate<T> {
/// Defaults to the default value specified in [TextField].
final TextInputType? keyboardType;
/// {@macro flutter.widgets.editableText.autocorrect}
final bool autocorrect;
/// {@macro flutter.services.TextInputConfiguration.enableSuggestions}
final bool enableSuggestions;
/// The text input action configuring the soft keyboard to a particular action
/// button.
///
@ -619,6 +627,8 @@ class _SearchPageState<T> extends State<_SearchPage<T>> {
focusNode: focusNode,
style: widget.delegate.searchFieldStyle ?? theme.textTheme.titleLarge,
textInputAction: widget.delegate.textInputAction,
autocorrect: widget.delegate.autocorrect,
enableSuggestions: widget.delegate.enableSuggestions,
keyboardType: widget.delegate.keyboardType,
onSubmitted: (String _) => widget.delegate.showResults(context),
decoration: InputDecoration(hintText: searchFieldLabel),

View File

@ -579,10 +579,50 @@ void main() {
expect(hintText.style?.fontSize, delegate.searchFieldStyle?.fontSize);
expect(textField.style?.color, delegate.searchFieldStyle?.color);
expect(textField.style?.fontSize, delegate.searchFieldStyle?.fontSize);
});
testWidgets('keyboard show search button by default', (WidgetTester tester) async {
testWidgets('Default autocorrect and enableSuggestions value', (WidgetTester tester) async {
final _TestSearchDelegate delegate = _TestSearchDelegate();
addTearDown(() => delegate.dispose());
await tester.pumpWidget(TestHomePage(delegate: delegate));
await tester.tap(find.byTooltip('Search'));
await tester.pumpAndSettle();
final TextField textField = tester.widget<TextField>(find.byType(TextField));
expect(textField.autocorrect, isTrue);
expect(textField.enableSuggestions, isTrue);
});
testWidgets('Custom autocorrect value', (WidgetTester tester) async {
final _TestSearchDelegate delegate = _TestSearchDelegate(autocorrect: false);
addTearDown(() => delegate.dispose());
await tester.pumpWidget(TestHomePage(delegate: delegate));
await tester.tap(find.byTooltip('Search'));
await tester.pumpAndSettle();
final TextField textField = tester.widget<TextField>(find.byType(TextField));
expect(textField.autocorrect, isFalse);
});
testWidgets('Custom enableSuggestions value', (WidgetTester tester) async {
final _TestSearchDelegate delegate = _TestSearchDelegate(enableSuggestions: false);
addTearDown(() => delegate.dispose());
await tester.pumpWidget(TestHomePage(delegate: delegate));
await tester.tap(find.byTooltip('Search'));
await tester.pumpAndSettle();
final TextField textField = tester.widget<TextField>(find.byType(TextField));
expect(textField.enableSuggestions, isFalse);
});
testWidgets('keyboard show search button by default',
(WidgetTester tester) async {
final _TestSearchDelegate delegate = _TestSearchDelegate();
addTearDown(() => delegate.dispose());
@ -1299,6 +1339,8 @@ class _TestSearchDelegate extends SearchDelegate<String> {
super.searchFieldStyle,
String? searchHint,
super.textInputAction,
super.autocorrect,
super.enableSuggestions,
}) : super(
searchFieldLabel: searchHint,
);