skip to Main Content

Flutter TextField: how to lose focus on tap outside?

TextField()

It loses focus only when tap another widget that can take focus such as TextField.

What if the TextField is the only one widget in the page?

2

Answers


  1. wrap your Scaffold with GestureDetector
    and when ontap unfocus

    like this

     GestureDetector(
          onTap: () {
            FocusScopeNode currentFocus = FocusScope.of(context);
            if (!currentFocus.hasPrimaryFocus) {
              currentFocus.unfocus();
            }
          },
          child: Scaffold(
            // your scaffold code here
          ),
        )
    
    Login or Signup to reply.
  2. You can use this code to unfocus keyboard

        @override
      Widget build(BuildContext context) {
        return GestureDetector(
          onTap: () {
            FocusScope.of(context).requestFocus(FocusNode());
          },
          child: Scaffold()
          },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search