diff --git a/packages/flutter/lib/src/material/input.dart b/packages/flutter/lib/src/material/input.dart index f915c534fc..396803e7ee 100644 --- a/packages/flutter/lib/src/material/input.dart +++ b/packages/flutter/lib/src/material/input.dart @@ -45,6 +45,7 @@ class InputField extends StatefulWidget { this.style, this.hideText: false, this.maxLines: 1, + this.autofocus: false, this.onChanged, this.onSubmitted, }) : super(key: key); @@ -75,6 +76,9 @@ class InputField extends StatefulWidget { /// horizontally instead. final int maxLines; + /// Whether this input field should focus itself if nothing else is already focused. + final bool autofocus; + /// Called when the text being edited changes. /// /// The [value] must be updated each time [onChanged] is invoked. @@ -124,6 +128,7 @@ class _InputFieldState extends State { style: textStyle, hideText: config.hideText, maxLines: config.maxLines, + autofocus: config.autofocus, cursorColor: themeData.textSelectionColor, selectionColor: themeData.textSelectionColor, selectionControls: materialTextSelectionControls, @@ -448,6 +453,10 @@ class Input extends StatefulWidget { final bool isDense; /// Whether this input field should focus itself if nothing else is already focused. + /// If true, the keyboard will open as soon as this input obtains focus. Otherwise, + /// the keyboard is only shown after the user taps the text field. + // See https://github.com/flutter/flutter/issues/7035 for the rationale for this + // keyboard behavior. final bool autofocus; /// The maximum number of lines for the text to span, wrapping if necessary. @@ -504,6 +513,7 @@ class _InputState extends State { style: config.style, hideText: config.hideText, maxLines: config.maxLines, + autofocus: config.autofocus, keyboardType: config.keyboardType, onChanged: config.onChanged, onSubmitted: config.onSubmitted, diff --git a/packages/flutter/lib/src/widgets/editable.dart b/packages/flutter/lib/src/widgets/editable.dart index 347f85a6ae..139a46c2da 100644 --- a/packages/flutter/lib/src/widgets/editable.dart +++ b/packages/flutter/lib/src/widgets/editable.dart @@ -135,6 +135,7 @@ class RawInput extends Scrollable { this.cursorColor, this.textScaleFactor, int maxLines: 1, + this.autofocus: false, this.selectionColor, this.selectionControls, @required this.platform, @@ -177,6 +178,11 @@ class RawInput extends Scrollable { /// horizontally instead. final int maxLines; + /// Whether this input field should focus itself if nothing else is already focused. + /// If true, the keyboard will open as soon as this input obtains focus. Otherwise, + /// the keyboard is only shown after the user taps the text field. + final bool autofocus; + /// The color to use when painting the selection. final Color selectionColor; @@ -278,7 +284,7 @@ class RawInputState extends ScrollableState implements TextInputClient bool _requestingFocus = false; void _attachOrDetachKeyboard(bool focused) { - if (focused && !_isAttachedToKeyboard && _requestingFocus) { + if (focused && !_isAttachedToKeyboard && (_requestingFocus || config.autofocus)) { _textInputConnection = TextInput.attach( this, new TextInputConfiguration(inputType: config.keyboardType)) ..setEditingState(_getTextEditingStateFromInputValue(_currentValue)) diff --git a/packages/flutter/test/widgets/input_test.dart b/packages/flutter/test/widgets/input_test.dart index 43c8ed44a0..6df133e328 100644 --- a/packages/flutter/test/widgets/input_test.dart +++ b/packages/flutter/test/widgets/input_test.dart @@ -91,6 +91,7 @@ void main() { return new Center( child: new Material( child: new Input( + autofocus: true, value: inputValue, key: inputKey, hintText: 'Placeholder', @@ -101,7 +102,6 @@ void main() { } await tester.pumpWidget(builder()); - await showKeyboard(tester); RenderBox findInputBox() => tester.renderObject(find.byKey(inputKey));