Open the keyboard without explicit tap for autofocused Inputs. (#7129)
Fixes https://github.com/flutter/flutter/issues/7035
This commit is contained in:
@@ -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<InputField> {
|
||||
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<Input> {
|
||||
style: config.style,
|
||||
hideText: config.hideText,
|
||||
maxLines: config.maxLines,
|
||||
autofocus: config.autofocus,
|
||||
keyboardType: config.keyboardType,
|
||||
onChanged: config.onChanged,
|
||||
onSubmitted: config.onSubmitted,
|
||||
|
||||
@@ -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<RawInput> 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))
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user