Add a flag that controls autocorrect on text fields (#11180)
See https://github.com/flutter/flutter/issues/11168
This commit is contained in:
@@ -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<TextField> {
|
||||
textAlign: widget.textAlign,
|
||||
autofocus: widget.autofocus,
|
||||
obscureText: widget.obscureText,
|
||||
autocorrect: widget.autocorrect,
|
||||
maxLines: widget.maxLines,
|
||||
cursorColor: themeData.textSelectionColor,
|
||||
selectionColor: themeData.textSelectionColor,
|
||||
|
||||
@@ -41,6 +41,7 @@ class TextFormField extends FormField<String> {
|
||||
TextStyle style,
|
||||
bool autofocus: false,
|
||||
bool obscureText: false,
|
||||
bool autocorrect: true,
|
||||
int maxLines: 1,
|
||||
FormFieldSetter<String> onSaved,
|
||||
FormFieldValidator<String> validator,
|
||||
@@ -48,6 +49,7 @@ class TextFormField extends FormField<String> {
|
||||
}) : 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<String> {
|
||||
style: style,
|
||||
autofocus: autofocus,
|
||||
obscureText: obscureText,
|
||||
autocorrect: autocorrect,
|
||||
maxLines: maxLines,
|
||||
onChanged: field.onChanged,
|
||||
inputFormatters: inputFormatters,
|
||||
|
||||
@@ -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 <String, dynamic>{
|
||||
'inputType': inputType.toString(),
|
||||
'obscureText': obscureText,
|
||||
'autocorrect': autocorrect,
|
||||
'actionLabel': actionLabel,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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<EditableText> 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<EditableText> 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;
|
||||
|
||||
@@ -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<String, dynamic> json = configuration.toJSON();
|
||||
expect(json['inputType'], 'TextInputType.number');
|
||||
expect(json['obscureText'], true);
|
||||
expect(json['autocorrect'], false);
|
||||
expect(json['actionLabel'], 'xyzzy');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user