diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index 53e9bacf90..2c749989ff 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -68,7 +68,8 @@ class TextField extends StatefulWidget { /// the number of lines. By default, it is 1, meaning this is a single-line /// text field. If it is not null, it must be greater than zero. /// - /// The [keyboardType], [autofocus], and [obscureText] arguments must not be null. + /// The [keyboardType], [autofocus], [obscureText], and [autocorrect] arguments + /// must not be null. const TextField({ Key key, this.controller, @@ -79,6 +80,7 @@ class TextField extends StatefulWidget { this.textAlign, this.autofocus: false, this.obscureText: false, + this.autocorrect: true, this.maxLines: 1, this.onChanged, this.onSubmitted, @@ -86,6 +88,7 @@ class TextField extends StatefulWidget { }) : assert(keyboardType != null), assert(autofocus != null), assert(obscureText != null), + assert(autocorrect != null), assert(maxLines == null || maxLines > 0), super(key: key); @@ -142,6 +145,11 @@ class TextField extends StatefulWidget { /// Defaults to false. Cannot be null. final bool obscureText; + /// Whether to enable autocorrection. + /// + /// Defaults to true. Cannot be null. + final bool autocorrect; + /// The maximum number of lines for the text to span, wrapping if necessary. /// /// If this is 1 (the default), the text will not wrap, but will scroll @@ -181,6 +189,8 @@ class TextField extends StatefulWidget { description.add('autofocus: $autofocus'); if (obscureText) description.add('obscureText: $obscureText'); + if (autocorrect) + description.add('autocorrect: $autocorrect'); if (maxLines != 1) description.add('maxLines: $maxLines'); } @@ -243,6 +253,7 @@ class _TextFieldState extends State { textAlign: widget.textAlign, autofocus: widget.autofocus, obscureText: widget.obscureText, + autocorrect: widget.autocorrect, maxLines: widget.maxLines, cursorColor: themeData.textSelectionColor, selectionColor: themeData.textSelectionColor, diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index 49726958c6..d28af15da0 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -41,6 +41,7 @@ class TextFormField extends FormField { TextStyle style, bool autofocus: false, bool obscureText: false, + bool autocorrect: true, int maxLines: 1, FormFieldSetter onSaved, FormFieldValidator validator, @@ -48,6 +49,7 @@ class TextFormField extends FormField { }) : assert(keyboardType != null), assert(autofocus != null), assert(obscureText != null), + assert(autocorrect != null), assert(maxLines == null || maxLines > 0), super( key: key, @@ -63,6 +65,7 @@ class TextFormField extends FormField { style: style, autofocus: autofocus, obscureText: obscureText, + autocorrect: autocorrect, maxLines: maxLines, onChanged: field.onChanged, inputFormatters: inputFormatters, diff --git a/packages/flutter/lib/src/services/text_input.dart b/packages/flutter/lib/src/services/text_input.dart index a982d9bd80..b36eb9f2df 100644 --- a/packages/flutter/lib/src/services/text_input.dart +++ b/packages/flutter/lib/src/services/text_input.dart @@ -67,13 +67,15 @@ enum TextInputAction { class TextInputConfiguration { /// Creates configuration information for a text input control. /// - /// The [inputType] and [obscureText] arguments must not be null. + /// The [inputType], [obscureText], and [autocorrect] arguments must not be null. const TextInputConfiguration({ this.inputType: TextInputType.text, this.obscureText: false, + this.autocorrect: true, this.actionLabel, }) : assert(inputType != null), - assert(obscureText != null); + assert(obscureText != null), + assert(autocorrect != null); /// The type of information for which to optimize the text input control. final TextInputType inputType; @@ -83,6 +85,11 @@ class TextInputConfiguration { /// Defaults to false. final bool obscureText; + /// Whether to enable autocorrection. + /// + /// Defaults to true. + final bool autocorrect; + /// What text to display in the text input control's action button. final String actionLabel; @@ -91,6 +98,7 @@ class TextInputConfiguration { return { 'inputType': inputType.toString(), 'obscureText': obscureText, + 'autocorrect': autocorrect, 'actionLabel': actionLabel, }; } diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 4f5ed7d08d..1e5f13b1c1 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -147,6 +147,7 @@ class EditableText extends StatefulWidget { @required this.controller, @required this.focusNode, this.obscureText: false, + this.autocorrect: true, @required this.style, @required this.cursorColor, this.textAlign, @@ -163,6 +164,7 @@ class EditableText extends StatefulWidget { }) : assert(controller != null), assert(focusNode != null), assert(obscureText != null), + assert(autocorrect != null), assert(style != null), assert(cursorColor != null), assert(maxLines == null || maxLines > 0), @@ -186,6 +188,11 @@ class EditableText extends StatefulWidget { /// Defaults to false. final bool obscureText; + /// Whether to enable autocorrection. + /// + /// Defaults to true. + final bool autocorrect; + /// The text style to use for the editable text. final TextStyle style; @@ -253,6 +260,8 @@ class EditableText extends StatefulWidget { description.add('focusNode: $focusNode'); if (obscureText != false) description.add('obscureText: $obscureText'); + if (autocorrect != true) + description.add('autocorrect: $autocorrect'); description.add('${style.toString().split("\n").join(", ")}'); if (textAlign != null) description.add('$textAlign'); @@ -388,7 +397,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien if (!_hasInputConnection) { final TextEditingValue localValue = _value; _lastKnownRemoteTextEditingValue = localValue; - _textInputConnection = TextInput.attach(this, new TextInputConfiguration(inputType: widget.keyboardType, obscureText: widget.obscureText)) + _textInputConnection = TextInput.attach(this, new TextInputConfiguration(inputType: widget.keyboardType, obscureText: widget.obscureText, autocorrect: widget.autocorrect)) ..setEditingState(localValue); } _textInputConnection.show(); @@ -578,6 +587,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien textScaleFactor: widget.textScaleFactor ?? MediaQuery.of(context, nullOk: true)?.textScaleFactor ?? 1.0, textAlign: widget.textAlign, obscureText: widget.obscureText, + autocorrect: widget.autocorrect, offset: offset, onSelectionChanged: _handleSelectionChanged, onCaretChanged: _handleCaretChanged, @@ -600,6 +610,7 @@ class _Editable extends LeafRenderObjectWidget { this.textScaleFactor, this.textAlign, this.obscureText, + this.autocorrect, this.offset, this.onSelectionChanged, this.onCaretChanged, @@ -614,6 +625,7 @@ class _Editable extends LeafRenderObjectWidget { final double textScaleFactor; final TextAlign textAlign; final bool obscureText; + final bool autocorrect; final ViewportOffset offset; final SelectionChangedHandler onSelectionChanged; final CaretChangedHandler onCaretChanged; diff --git a/packages/flutter/test/services/text_input_test.dart b/packages/flutter/test/services/text_input_test.dart index 6903a26c82..838475dfb3 100644 --- a/packages/flutter/test/services/text_input_test.dart +++ b/packages/flutter/test/services/text_input_test.dart @@ -11,6 +11,7 @@ void main() { final TextInputConfiguration configuration = const TextInputConfiguration(); expect(configuration.inputType, TextInputType.text); expect(configuration.obscureText, false); + expect(configuration.autocorrect, true); expect(configuration.actionLabel, null); }); @@ -18,11 +19,13 @@ void main() { final TextInputConfiguration configuration = const TextInputConfiguration( inputType: TextInputType.number, obscureText: true, + autocorrect: false, actionLabel: 'xyzzy' ); final Map json = configuration.toJSON(); expect(json['inputType'], 'TextInputType.number'); expect(json['obscureText'], true); + expect(json['autocorrect'], false); expect(json['actionLabel'], 'xyzzy'); }); });