Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I make tapping anywhere outside of the TextField make it loose focus?

Tags:

flutter

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?

like image 444
Justin808 Avatar asked Jan 30 '19 23:01

Justin808


People also ask

How do I minimize keyboard flutters?

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.


2 Answers

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
    )
 )
like image 166
Hussam Avatar answered Oct 17 '22 18:10

Hussam


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,
                ),
              ),
like image 44
Neeraj Sharma Avatar answered Oct 17 '22 18:10

Neeraj Sharma