diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index 4c1d28e3f1..ee52e87953 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -197,8 +197,8 @@ class CupertinoTextField extends StatefulWidget { /// /// The [autocorrect], [autofocus], [clearButtonMode], [dragStartBehavior], /// [expands], [maxLengthEnforced], [obscureText], [prefixMode], [readOnly], - /// [scrollPadding], [suffixMode], and [textAlign] properties must not be - /// null. + /// [scrollPadding], [suffixMode], [textAlign], and [enableSuggestions] properties + /// must not be null. /// /// See also: /// @@ -235,6 +235,7 @@ class CupertinoTextField extends StatefulWidget { this.autofocus = false, this.obscureText = false, this.autocorrect = true, + this.enableSuggestions = true, this.maxLines = 1, this.minLines, this.expands = false, @@ -260,6 +261,7 @@ class CupertinoTextField extends StatefulWidget { assert(autofocus != null), assert(obscureText != null), assert(autocorrect != null), + assert(enableSuggestions != null), assert(maxLengthEnforced != null), assert(scrollPadding != null), assert(dragStartBehavior != null), @@ -415,6 +417,9 @@ class CupertinoTextField extends StatefulWidget { /// {@macro flutter.widgets.editableText.autocorrect} final bool autocorrect; + /// {@macro flutter.services.textInput.enableSuggestions} + final bool enableSuggestions; + /// {@macro flutter.widgets.editableText.maxLines} final int maxLines; @@ -561,7 +566,8 @@ class CupertinoTextField extends StatefulWidget { properties.add(DiagnosticsProperty('style', style, defaultValue: null)); properties.add(DiagnosticsProperty('autofocus', autofocus, defaultValue: false)); properties.add(DiagnosticsProperty('obscureText', obscureText, defaultValue: false)); - properties.add(DiagnosticsProperty('autocorrect', autocorrect, defaultValue: false)); + properties.add(DiagnosticsProperty('autocorrect', autocorrect, defaultValue: true)); + properties.add(DiagnosticsProperty('enableSuggestions', enableSuggestions, defaultValue: true)); properties.add(IntProperty('maxLines', maxLines, defaultValue: 1)); properties.add(IntProperty('minLines', minLines, defaultValue: null)); properties.add(DiagnosticsProperty('expands', expands, defaultValue: false)); @@ -877,6 +883,7 @@ class _CupertinoTextFieldState extends State with AutomaticK autofocus: widget.autofocus, obscureText: widget.obscureText, autocorrect: widget.autocorrect, + enableSuggestions: widget.enableSuggestions, maxLines: widget.maxLines, minLines: widget.minLines, expands: widget.expands, diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index 10a6ce984a..efc1cef30d 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -277,8 +277,8 @@ class TextField extends StatefulWidget { /// is null (the default) and [readOnly] is true. /// /// The [textAlign], [autofocus], [obscureText], [readOnly], [autocorrect], - /// [maxLengthEnforced], [scrollPadding], [maxLines], and [maxLength] - /// arguments must not be null. + /// [maxLengthEnforced], [scrollPadding], [maxLines], [maxLength], and + /// [enableSuggestions] arguments must not be null. /// /// See also: /// @@ -303,6 +303,7 @@ class TextField extends StatefulWidget { this.autofocus = false, this.obscureText = false, this.autocorrect = true, + this.enableSuggestions = true, this.maxLines = 1, this.minLines, this.expands = false, @@ -329,6 +330,7 @@ class TextField extends StatefulWidget { assert(autofocus != null), assert(obscureText != null), assert(autocorrect != null), + assert(enableSuggestions != null), assert(enableInteractiveSelection != null), assert(maxLengthEnforced != null), assert(scrollPadding != null), @@ -454,6 +456,9 @@ class TextField extends StatefulWidget { /// {@macro flutter.widgets.editableText.autocorrect} final bool autocorrect; + /// {@macro flutter.services.textInput.enableSuggestions} + final bool enableSuggestions; + /// {@macro flutter.widgets.editableText.maxLines} final int maxLines; @@ -676,6 +681,7 @@ class TextField extends StatefulWidget { properties.add(DiagnosticsProperty('autofocus', autofocus, defaultValue: false)); properties.add(DiagnosticsProperty('obscureText', obscureText, defaultValue: false)); properties.add(DiagnosticsProperty('autocorrect', autocorrect, defaultValue: true)); + properties.add(DiagnosticsProperty('enableSuggestions', enableSuggestions, defaultValue: true)); properties.add(IntProperty('maxLines', maxLines, defaultValue: 1)); properties.add(IntProperty('minLines', minLines, defaultValue: null)); properties.add(DiagnosticsProperty('expands', expands, defaultValue: false)); @@ -955,6 +961,7 @@ class _TextFieldState extends State implements TextSelectionGestureDe autofocus: widget.autofocus, obscureText: widget.obscureText, autocorrect: widget.autocorrect, + enableSuggestions: widget.enableSuggestions, maxLines: widget.maxLines, minLines: widget.minLines, expands: widget.expands, diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index 1868e61109..b8e194ada7 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -98,6 +98,7 @@ class TextFormField extends FormField { bool showCursor, bool obscureText = false, bool autocorrect = true, + bool enableSuggestions = true, bool autovalidate = false, bool maxLengthEnforced = true, int maxLines = 1, @@ -125,6 +126,7 @@ class TextFormField extends FormField { assert(readOnly != null), assert(obscureText != null), assert(autocorrect != null), + assert(enableSuggestions != null), assert(autovalidate != null), assert(maxLengthEnforced != null), assert(scrollPadding != null), @@ -177,6 +179,7 @@ class TextFormField extends FormField { showCursor: showCursor, obscureText: obscureText, autocorrect: autocorrect, + enableSuggestions: enableSuggestions, maxLengthEnforced: maxLengthEnforced, maxLines: maxLines, minLines: minLines, diff --git a/packages/flutter/lib/src/services/text_input.dart b/packages/flutter/lib/src/services/text_input.dart index d015d81e4c..8dc841b3dd 100644 --- a/packages/flutter/lib/src/services/text_input.dart +++ b/packages/flutter/lib/src/services/text_input.dart @@ -386,6 +386,7 @@ class TextInputConfiguration { this.inputType = TextInputType.text, this.obscureText = false, this.autocorrect = true, + this.enableSuggestions = true, this.actionLabel, this.inputAction = TextInputAction.done, this.keyboardAppearance = Brightness.light, @@ -393,6 +394,7 @@ class TextInputConfiguration { }) : assert(inputType != null), assert(obscureText != null), assert(autocorrect != null), + assert(enableSuggestions != null), assert(keyboardAppearance != null), assert(inputAction != null), assert(textCapitalization != null); @@ -410,6 +412,20 @@ class TextInputConfiguration { /// Defaults to true. final bool autocorrect; + /// {@template flutter.services.textInput.enableSuggestions} + /// Whether to show input suggestions as the user types. + /// + /// This flag only affects Android. On iOS, suggestions are tied directly to + /// [autocorrect], so that suggestions are only shown when [autocorrect] is + /// true. On Android autocorrection and suggestion are controlled separately. + /// + /// Defaults to true. Cannot be null. + /// + /// See also: + /// * + /// {@endtemplate} + final bool enableSuggestions; + /// What text to display in the text input control's action button. final String actionLabel; @@ -439,6 +455,7 @@ class TextInputConfiguration { 'inputType': inputType.toJson(), 'obscureText': obscureText, 'autocorrect': autocorrect, + 'enableSuggestions': enableSuggestions, 'actionLabel': actionLabel, 'inputAction': inputAction.toString(), 'textCapitalization': textCapitalization.toString(), diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 0f38f56c16..b1ef6e1973 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -342,9 +342,9 @@ class EditableText extends StatefulWidget { /// The [controller], [focusNode], [obscureText], [autocorrect], [autofocus], /// [showSelectionHandles], [enableInteractiveSelection], [forceLine], /// [style], [cursorColor], [cursorOpacityAnimates],[backgroundCursorColor], - /// [paintCursorAboveText], [textAlign], [dragStartBehavior], [scrollPadding], - /// [dragStartBehavior], [toolbarOptions], [rendererIgnoresPointer], and - /// [readOnly] arguments must not be null. + /// [enableSuggestions], [paintCursorAboveText], [textAlign], [dragStartBehavior], + /// [scrollPadding], [dragStartBehavior], [toolbarOptions], + /// [rendererIgnoresPointer], and [readOnly] arguments must not be null. EditableText({ Key key, @required this.controller, @@ -352,6 +352,7 @@ class EditableText extends StatefulWidget { this.readOnly = false, this.obscureText = false, this.autocorrect = true, + this.enableSuggestions = true, @required this.style, StrutStyle strutStyle, @required this.cursorColor, @@ -401,6 +402,7 @@ class EditableText extends StatefulWidget { assert(focusNode != null), assert(obscureText != null), assert(autocorrect != null), + assert(enableSuggestions != null), assert(showSelectionHandles != null), assert(enableInteractiveSelection != null), assert(readOnly != null), @@ -515,6 +517,9 @@ class EditableText extends StatefulWidget { /// {@endtemplate} final bool autocorrect; + /// {@macro flutter.services.textInput.enableSuggestions} + final bool enableSuggestions; + /// The text style to use for the editable text. final TextStyle style; @@ -1037,6 +1042,7 @@ class EditableText extends StatefulWidget { properties.add(DiagnosticsProperty('focusNode', focusNode)); properties.add(DiagnosticsProperty('obscureText', obscureText, defaultValue: false)); properties.add(DiagnosticsProperty('autocorrect', autocorrect, defaultValue: true)); + properties.add(DiagnosticsProperty('enableSuggestions', enableSuggestions, defaultValue: true)); style?.debugFillProperties(properties); properties.add(EnumProperty('textAlign', textAlign, defaultValue: null)); properties.add(EnumProperty('textDirection', textDirection, defaultValue: null)); @@ -1394,6 +1400,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien inputType: widget.keyboardType, obscureText: widget.obscureText, autocorrect: widget.autocorrect, + enableSuggestions: widget.enableSuggestions, inputAction: widget.textInputAction ?? (widget.keyboardType == TextInputType.multiline ? TextInputAction.newline : TextInputAction.done @@ -1842,6 +1849,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien textWidthBasis: widget.textWidthBasis, obscureText: widget.obscureText, autocorrect: widget.autocorrect, + enableSuggestions: widget.enableSuggestions, offset: offset, onSelectionChanged: _handleSelectionChanged, onCaretChanged: _handleCaretChanged, @@ -1907,6 +1915,7 @@ class _Editable extends LeafRenderObjectWidget { this.locale, this.obscureText, this.autocorrect, + this.enableSuggestions, this.offset, this.onSelectionChanged, this.onCaretChanged, @@ -1944,6 +1953,7 @@ class _Editable extends LeafRenderObjectWidget { final bool obscureText; final TextWidthBasis textWidthBasis; final bool autocorrect; + final bool enableSuggestions; final ViewportOffset offset; final SelectionChangedHandler onSelectionChanged; final CaretChangedHandler onCaretChanged; diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index af4fc84578..932ecb0f77 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -119,6 +119,7 @@ void main() { expect(editableText.maxLines, equals(1)); expect(editableText.obscureText, isFalse); expect(editableText.autocorrect, isTrue); + expect(editableText.enableSuggestions, isTrue); expect(editableText.textAlign, TextAlign.start); expect(editableText.cursorWidth, 2.0); }); @@ -327,6 +328,36 @@ void main() { equals('TextInputAction.done')); }); + testWidgets('enableSuggestions flag is sent to the engine properly', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController(); + const bool enableSuggestions = false; + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: FocusScope( + node: focusScopeNode, + autofocus: true, + child: EditableText( + controller: controller, + backgroundCursorColor: Colors.grey, + focusNode: focusNode, + enableSuggestions: enableSuggestions, + style: textStyle, + cursorColor: cursorColor, + ), + ), + ), + ), + ); + + await tester.tap(find.byType(EditableText)); + await tester.showKeyboard(find.byType(EditableText)); + await tester.idle(); + expect(tester.testTextInput.setClientArgs['enableSuggestions'], enableSuggestions); + }); + testWidgets('selection overlay will update when text grow bigger', (WidgetTester tester) async { final TextEditingController controller = TextEditingController.fromValue( const TextEditingValue(