Remove focus from TextField when the keyboard is dismissed

Viewed 585

It seems that a TextField does not lose the focus when the keyboard is dismissed. That makes the cursor continually blink, and more seriously, when the main window gets focus again, for example, after showing a dialogue box, the keyboard pops up again. Can I make the TextField lose focus when the keyboard is dismissed?

return Scaffold(
  body: Padding(
    padding: EdgeInsets.all(16),
      child: Center(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        TextField(decoration: InputDecoration(hintText: "Example"),),
        RaisedButton(
            child: Text("Test"), onPressed: () => onButtonClick(context))
      ],
    ),
  )),
);

enter image description here

2 Answers

Try using this when clicking on the button:

FocusScope.of(context).unfocus();

and if you have a textEditingController (which I recommend using) you can call to clear the field

_textEditingController.clear();

but you have to create the controller first:

final _textEditingController = TextEditingController();

and when you create your TextField:

TextField(
    decoration: InputDecoration(hintText: "Example"),
    controller: _textEditingController,
),

Use TextEditingController:

First Initialise :

TextEditingController _textController = new TextEditingController();

code:

TextField(controller: _textController),

Related