When I tap on a TextField
it gains focus. The keyboard opens and typing works. Now I'm done typing and I tap anywhere except theTextField
, I expected the focus to change and the keyboard to go away, it doesn't.
How to I make tapping anywhere outside of the TextField
make it loose focus?
TextField is a very common widget in Flutter. When you click on the TextField it opens up the on-screen keyboard. To hide/dismiss the keyboard you have to press the back button in Android and the done button (inside the onscreen keyboard) in iOS.
You can wrap the whole screen in GestureDetector
so that when you touch anywhere on GestureDetector
, onTap
method is called which will hide the soft keyboard.
new Scaffold(
body: new GestureDetector(
onTap: () {
/*This method here will hide the soft keyboard.*/
FocusScope.of(context).requestFocus(new FocusNode());
},
child: new Container(
//More work here
)
)
Try this function,
final _emailFocus = FocusNode();
final _passwordFocus = FocusNode();
final _emailController = TextEditingController();
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.search,
focusNode: _emailFocus,
onFieldSubmitted: (term) {
_emailFocus.unfocus();
FocusScope.of(context).requestFocus(_passwordFocus);
},
controller: _emailController,
decoration: InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
),
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With