Dismissing the Keyboard on Flutter

Renan Alvarenga
1 min readJun 24, 2021

Flutter uses the default OS keyboard when typing, which can be an issue when trying to perform certain actions or getting rid of that keyboard.

After doing some research flutter doesn’t provide the behavior of dismissing a keyboard by tapping outside of it, a solution to that problem that you may have seen on Stack Overflow is to add

FocusScope.of(context).requestFocus(new FocusNode());

to the onTap method of a GestureDetector. Even though this will work it‘s not recommended as it will create a ChangeNotifier that needs to be disposed of properly. The best solution to this problem is to wrap your entire app in a Gesture Detector to make sure that every time we click outside of the keyboard it will disappear.

As you can see below we’re getting the Node that currently holds the primary focus (The Text Field) and removing that focus, which hides the keyboard from the screen.

Pretty easy fix and not error prone like many of the suggestions going around.

If you have any question or suggestions, please comment below!

--

--