From a66ea0a628ef3bb31ba0b6c1cdf6ad657f0448a6 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 19 Jul 2018 18:06:44 -0700 Subject: [PATCH] add textCapitalization property (#19367) --- .../demo/material/text_form_field_demo.dart | 1 + .../flutter/lib/src/material/text_field.dart | 15 +++++++ .../lib/src/material/text_form_field.dart | 2 + .../flutter/lib/src/services/text_input.dart | 43 ++++++++++++++++++- .../lib/src/widgets/editable_text.dart | 15 +++++++ .../test/services/text_input_test.dart | 1 + 6 files changed, 76 insertions(+), 1 deletion(-) diff --git a/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart b/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart index 4262bfb627..90bd50eeef 100644 --- a/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart @@ -180,6 +180,7 @@ class TextFormFieldDemoState extends State { children: [ const SizedBox(height: 24.0), new TextFormField( + textCapitalization: TextCapitalization.words, decoration: const InputDecoration( border: const UnderlineInputBorder(), filled: true, diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index ea506ee02d..bfd3373547 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -103,6 +103,7 @@ class TextField extends StatefulWidget { this.decoration = const InputDecoration(), TextInputType keyboardType = TextInputType.text, this.textInputAction = TextInputAction.done, + this.textCapitalization = TextCapitalization.none, this.style, this.textAlign = TextAlign.start, this.autofocus = false, @@ -160,6 +161,19 @@ class TextField extends StatefulWidget { /// Defaults to [TextInputAction.done]. Must not be null. final TextInputAction textInputAction; + /// Configures how the platform keyboard will select an uppercase or + /// lowercase keyboard. + /// + /// Only supports text keyboards, other keyboard types will ignore this + /// configuration. Capitalization is locale-aware. + /// + /// Defaults to [TextCapitalization.none]. Must not be null. + /// + /// See also: + /// + /// * [TextCapitalization], for a description of each capitalization behavior. + final TextCapitalization textCapitalization; + /// The style to use for the text being edited. /// /// This text style is also used as the base style for the [decoration]. @@ -509,6 +523,7 @@ class _TextFieldState extends State with AutomaticKeepAliveClientMixi focusNode: focusNode, keyboardType: widget.keyboardType, textInputAction: widget.textInputAction, + textCapitalization: widget.textCapitalization, style: style, textAlign: widget.textAlign, autofocus: widget.autofocus, diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index 8b5774c00a..8b9168d958 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -55,6 +55,7 @@ class TextFormField extends FormField { FocusNode focusNode, InputDecoration decoration = const InputDecoration(), TextInputType keyboardType = TextInputType.text, + TextCapitalization textCapitalization = TextCapitalization.none, TextInputAction textInputAction = TextInputAction.done, TextStyle style, TextAlign textAlign = TextAlign.start, @@ -101,6 +102,7 @@ class TextFormField extends FormField { textInputAction: textInputAction, style: style, textAlign: textAlign, + textCapitalization: textCapitalization, autofocus: autofocus, obscureText: obscureText, autocorrect: autocorrect, diff --git a/packages/flutter/lib/src/services/text_input.dart b/packages/flutter/lib/src/services/text_input.dart index 20e38c8868..3b82326ef6 100644 --- a/packages/flutter/lib/src/services/text_input.dart +++ b/packages/flutter/lib/src/services/text_input.dart @@ -314,6 +314,34 @@ enum TextInputAction { newline, } +/// Configures how the platform keyboard will select an uppercase or +/// lowercase keyboard. +/// +/// Only supports text keyboards, other keyboard types will ignore this +/// configuration. Capitalization is locale-aware. +enum TextCapitalization { + /// Defaults to an uppercase keyboard for the first letter of each word. + /// + /// Corresponds to `InputType.TYPE_TEXT_FLAG_CAP_WORDS` on Android, and + /// `UITextAutocapitalizationTypeWords` on iOS. + words, + + /// Defaults to an uppercase keyboard for the first letter of each sentence. + /// + /// Corresponds to `InputType.TYPE_TEXT_FLAG_CAP_SENTENCES` on Android, and + /// `UITextAutocapitalizationTypeSentences` on iOS. + sentences, + + /// Defaults to an uppercase keyboard for each character. + /// + /// Corresponds to `InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS` on Android, and + /// `UITextAutocapitalizationTypeAllCharacters` on iOS. + characters, + + /// Defaults to a lowercase keyboard. + none, +} + /// Controls the visual appearance of the text input control. /// /// Many [TextInputAction]s are common between Android and iOS. However, if an @@ -343,11 +371,13 @@ class TextInputConfiguration { this.actionLabel, this.inputAction = TextInputAction.done, this.keyboardAppearance = Brightness.light, + this.textCapitalization = TextCapitalization.none, }) : assert(inputType != null), assert(obscureText != null), assert(autocorrect != null), assert(keyboardAppearance != null), - assert(inputAction != null); + assert(inputAction != null), + assert(textCapitalization != null); /// The type of information for which to optimize the text input control. final TextInputType inputType; @@ -368,6 +398,16 @@ class TextInputConfiguration { /// What kind of action to request for the action button on the IME. final TextInputAction inputAction; + /// Specifies how platforms may automatically capitialize text entered by the + /// user. + /// + /// Defaults to [TextCapitalization.none]. + /// + /// See also: + /// + /// * [TextCapitalization], for a description of each capitalization behavior. + final TextCapitalization textCapitalization; + /// The appearance of the keyboard. /// /// This setting is only honored on iOS devices. @@ -383,6 +423,7 @@ class TextInputConfiguration { 'autocorrect': autocorrect, 'actionLabel': actionLabel, 'inputAction': inputAction.toString(), + 'textCapitalization': textCapitalization.toString(), 'keyboardAppearance': keyboardAppearance.toString(), }; } diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index ed2262e3b2..67301425aa 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -202,6 +202,7 @@ class EditableText extends StatefulWidget { this.selectionControls, TextInputType keyboardType, this.textInputAction = TextInputAction.done, + this.textCapitalization = TextCapitalization.none, this.onChanged, this.onEditingComplete, this.onSubmitted, @@ -269,6 +270,19 @@ class EditableText extends StatefulWidget { /// Defaults to the ambient [Directionality], if any. final TextDirection textDirection; + /// Configures how the platform keyboard will select an uppercase or + /// lowercase keyboard. + /// + /// Only supports text keyboards, other keyboard types will ignore this + /// configuration. Capitalization is locale-aware. + /// + /// Defaults to [TextCapitalization.none]. Must not be null. + /// + /// See also: + /// + /// * [TextCapitalization], for a description of each capitalization behavior. + final TextCapitalization textCapitalization; + /// Used to select a font when the same Unicode character can /// be rendered differently, depending on the locale. /// @@ -569,6 +583,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien inputAction: widget.keyboardType == TextInputType.multiline ? TextInputAction.newline : widget.textInputAction, + textCapitalization: widget.textCapitalization, ) )..setEditingState(localValue); } diff --git a/packages/flutter/test/services/text_input_test.dart b/packages/flutter/test/services/text_input_test.dart index 159176d053..8f4969e86f 100644 --- a/packages/flutter/test/services/text_input_test.dart +++ b/packages/flutter/test/services/text_input_test.dart @@ -13,6 +13,7 @@ void main() { expect(configuration.obscureText, false); expect(configuration.autocorrect, true); expect(configuration.actionLabel, null); + expect(configuration.textCapitalization, TextCapitalization.none); expect(configuration.keyboardAppearance, Brightness.light); });