Skip to content

Commit 91eb8d2

Browse files
authored
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.* * flutter/flutter#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].*
1 parent c8c2f46 commit 91eb8d2

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/flutter/lib/src/material/search.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ abstract class SearchDelegate<T> {
155155
this.searchFieldDecorationTheme,
156156
this.keyboardType,
157157
this.textInputAction = TextInputAction.search,
158+
this.autocorrect = true,
159+
this.enableSuggestions = true,
158160
}) : assert(searchFieldStyle == null || searchFieldDecorationTheme == null);
159161

160162
/// Suggestions shown in the body of the search page while the user types a
@@ -365,6 +367,12 @@ abstract class SearchDelegate<T> {
365367
/// Defaults to the default value specified in [TextField].
366368
final TextInputType? keyboardType;
367369

370+
/// {@macro flutter.widgets.editableText.autocorrect}
371+
final bool autocorrect;
372+
373+
/// {@macro flutter.services.TextInputConfiguration.enableSuggestions}
374+
final bool enableSuggestions;
375+
368376
/// The text input action configuring the soft keyboard to a particular action
369377
/// button.
370378
///
@@ -619,6 +627,8 @@ class _SearchPageState<T> extends State<_SearchPage<T>> {
619627
focusNode: focusNode,
620628
style: widget.delegate.searchFieldStyle ?? theme.textTheme.titleLarge,
621629
textInputAction: widget.delegate.textInputAction,
630+
autocorrect: widget.delegate.autocorrect,
631+
enableSuggestions: widget.delegate.enableSuggestions,
622632
keyboardType: widget.delegate.keyboardType,
623633
onSubmitted: (String _) => widget.delegate.showResults(context),
624634
decoration: InputDecoration(hintText: searchFieldLabel),

packages/flutter/test/material/search_test.dart

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,50 @@ void main() {
579579
expect(hintText.style?.fontSize, delegate.searchFieldStyle?.fontSize);
580580
expect(textField.style?.color, delegate.searchFieldStyle?.color);
581581
expect(textField.style?.fontSize, delegate.searchFieldStyle?.fontSize);
582+
});
583+
584+
testWidgets('Default autocorrect and enableSuggestions value', (WidgetTester tester) async {
585+
final _TestSearchDelegate delegate = _TestSearchDelegate();
586+
addTearDown(() => delegate.dispose());
587+
588+
await tester.pumpWidget(TestHomePage(delegate: delegate));
589+
await tester.tap(find.byTooltip('Search'));
590+
await tester.pumpAndSettle();
591+
592+
final TextField textField = tester.widget<TextField>(find.byType(TextField));
593+
594+
expect(textField.autocorrect, isTrue);
595+
expect(textField.enableSuggestions, isTrue);
596+
});
597+
598+
testWidgets('Custom autocorrect value', (WidgetTester tester) async {
599+
final _TestSearchDelegate delegate = _TestSearchDelegate(autocorrect: false);
600+
addTearDown(() => delegate.dispose());
601+
602+
await tester.pumpWidget(TestHomePage(delegate: delegate));
603+
await tester.tap(find.byTooltip('Search'));
604+
await tester.pumpAndSettle();
605+
606+
final TextField textField = tester.widget<TextField>(find.byType(TextField));
607+
608+
expect(textField.autocorrect, isFalse);
609+
});
610+
611+
testWidgets('Custom enableSuggestions value', (WidgetTester tester) async {
612+
final _TestSearchDelegate delegate = _TestSearchDelegate(enableSuggestions: false);
613+
addTearDown(() => delegate.dispose());
614+
615+
await tester.pumpWidget(TestHomePage(delegate: delegate));
616+
await tester.tap(find.byTooltip('Search'));
617+
await tester.pumpAndSettle();
618+
619+
final TextField textField = tester.widget<TextField>(find.byType(TextField));
582620

621+
expect(textField.enableSuggestions, isFalse);
583622
});
584623

585-
testWidgets('keyboard show search button by default', (WidgetTester tester) async {
624+
testWidgets('keyboard show search button by default',
625+
(WidgetTester tester) async {
586626
final _TestSearchDelegate delegate = _TestSearchDelegate();
587627
addTearDown(() => delegate.dispose());
588628

@@ -1299,6 +1339,8 @@ class _TestSearchDelegate extends SearchDelegate<String> {
12991339
super.searchFieldStyle,
13001340
String? searchHint,
13011341
super.textInputAction,
1342+
super.autocorrect,
1343+
super.enableSuggestions,
13021344
}) : super(
13031345
searchFieldLabel: searchHint,
13041346
);

0 commit comments

Comments
 (0)