Commit 7034a03c authored by Matt Perry's avatar Matt Perry Committed by GitHub

Open the keyboard without explicit tap for autofocused Inputs. (#7129)

Fixes https://github.com/flutter/flutter/issues/7035
parent 88f12096
...@@ -45,6 +45,7 @@ class InputField extends StatefulWidget { ...@@ -45,6 +45,7 @@ class InputField extends StatefulWidget {
this.style, this.style,
this.hideText: false, this.hideText: false,
this.maxLines: 1, this.maxLines: 1,
this.autofocus: false,
this.onChanged, this.onChanged,
this.onSubmitted, this.onSubmitted,
}) : super(key: key); }) : super(key: key);
...@@ -75,6 +76,9 @@ class InputField extends StatefulWidget { ...@@ -75,6 +76,9 @@ class InputField extends StatefulWidget {
/// horizontally instead. /// horizontally instead.
final int maxLines; 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. /// Called when the text being edited changes.
/// ///
/// The [value] must be updated each time [onChanged] is invoked. /// The [value] must be updated each time [onChanged] is invoked.
...@@ -124,6 +128,7 @@ class _InputFieldState extends State<InputField> { ...@@ -124,6 +128,7 @@ class _InputFieldState extends State<InputField> {
style: textStyle, style: textStyle,
hideText: config.hideText, hideText: config.hideText,
maxLines: config.maxLines, maxLines: config.maxLines,
autofocus: config.autofocus,
cursorColor: themeData.textSelectionColor, cursorColor: themeData.textSelectionColor,
selectionColor: themeData.textSelectionColor, selectionColor: themeData.textSelectionColor,
selectionControls: materialTextSelectionControls, selectionControls: materialTextSelectionControls,
...@@ -448,6 +453,10 @@ class Input extends StatefulWidget { ...@@ -448,6 +453,10 @@ class Input extends StatefulWidget {
final bool isDense; final bool isDense;
/// Whether this input field should focus itself if nothing else is already focused. /// 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; final bool autofocus;
/// The maximum number of lines for the text to span, wrapping if necessary. /// The maximum number of lines for the text to span, wrapping if necessary.
...@@ -504,6 +513,7 @@ class _InputState extends State<Input> { ...@@ -504,6 +513,7 @@ class _InputState extends State<Input> {
style: config.style, style: config.style,
hideText: config.hideText, hideText: config.hideText,
maxLines: config.maxLines, maxLines: config.maxLines,
autofocus: config.autofocus,
keyboardType: config.keyboardType, keyboardType: config.keyboardType,
onChanged: config.onChanged, onChanged: config.onChanged,
onSubmitted: config.onSubmitted, onSubmitted: config.onSubmitted,
......
...@@ -135,6 +135,7 @@ class RawInput extends Scrollable { ...@@ -135,6 +135,7 @@ class RawInput extends Scrollable {
this.cursorColor, this.cursorColor,
this.textScaleFactor, this.textScaleFactor,
int maxLines: 1, int maxLines: 1,
this.autofocus: false,
this.selectionColor, this.selectionColor,
this.selectionControls, this.selectionControls,
@required this.platform, @required this.platform,
...@@ -177,6 +178,11 @@ class RawInput extends Scrollable { ...@@ -177,6 +178,11 @@ class RawInput extends Scrollable {
/// horizontally instead. /// horizontally instead.
final int maxLines; 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. /// The color to use when painting the selection.
final Color selectionColor; final Color selectionColor;
...@@ -278,7 +284,7 @@ class RawInputState extends ScrollableState<RawInput> implements TextInputClient ...@@ -278,7 +284,7 @@ class RawInputState extends ScrollableState<RawInput> implements TextInputClient
bool _requestingFocus = false; bool _requestingFocus = false;
void _attachOrDetachKeyboard(bool focused) { void _attachOrDetachKeyboard(bool focused) {
if (focused && !_isAttachedToKeyboard && _requestingFocus) { if (focused && !_isAttachedToKeyboard && (_requestingFocus || config.autofocus)) {
_textInputConnection = TextInput.attach( _textInputConnection = TextInput.attach(
this, new TextInputConfiguration(inputType: config.keyboardType)) this, new TextInputConfiguration(inputType: config.keyboardType))
..setEditingState(_getTextEditingStateFromInputValue(_currentValue)) ..setEditingState(_getTextEditingStateFromInputValue(_currentValue))
......
...@@ -91,6 +91,7 @@ void main() { ...@@ -91,6 +91,7 @@ void main() {
return new Center( return new Center(
child: new Material( child: new Material(
child: new Input( child: new Input(
autofocus: true,
value: inputValue, value: inputValue,
key: inputKey, key: inputKey,
hintText: 'Placeholder', hintText: 'Placeholder',
...@@ -101,7 +102,6 @@ void main() { ...@@ -101,7 +102,6 @@ void main() {
} }
await tester.pumpWidget(builder()); await tester.pumpWidget(builder());
await showKeyboard(tester);
RenderBox findInputBox() => tester.renderObject(find.byKey(inputKey)); RenderBox findInputBox() => tester.renderObject(find.byKey(inputKey));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment