Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically focusing on InputField and opening up keyboard

Tags:

flutter

dart

I have a situation where I need to programmatically focus on a InputField(such as in response to a button press).

I'm using the Focus.moveTo function; however, even though the InputField is focused (blinking cursor appears) the keyboard is not brought up.

It seems like the best solution would be to call the RequestKeyboard() function in _InputFieldState, but this is private.

What would be the best way to accomplish this?

Here is a code sample showing the workflow:

class InputFieldWrapper extends StatefulWidget {

  @override
  _InputFieldWrapperState createState() => new _InputFieldWrapperState();
}


class _InputFieldWrapperState extends State<InputFieldWrapper> {

  InputValue _currentInput = new InputValue(text: 'hello');

  // GlobalKey for the InputField so we can focus on it
  GlobalKey<EditableTextState> _inputKey = new GlobalKey<EditableTextState>();

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: [
        // Button that should focus on the InputField when pressed
        new IconButton(
          icon: new Icon(Icons.message),
          onPressed: () {
            Focus.moveTo(_inputKey);
          },
        ),
        // InputField that should be focused when pressing the Button
        new InputField(
          value: _currentInput,
          key: _inputKey,
          onChanged: (InputValue input) {
            setState(() {
              _currentInput = input;
            });
          }
        ),
      ],
    );
  }
}
like image 913
Dvdwasibi Avatar asked Nov 09 '22 02:11

Dvdwasibi


1 Answers

This was decided to be a bug and is being tracked at #7985.

like image 176
Eric Seidel Avatar answered Dec 05 '22 20:12

Eric Seidel