Keyboard pops up when tapping on the TextField.
It won't go away unless you press close/enter button on the keyboard.
But! There is another way to remove the keyboard.
Unfocusing the TextField will make the keyboard go away.
Then how to take the focus away from the TextField is what we want to know.
GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child : Scaffold(
...
body : TextField(
...
)
)
)
The FocusManager does that.
Calling unfocus() will remove the focus from current node.
GestureDetector is used to detect if you've tapped a widget/background other than the TextField.
It's important to wrap the GestureDetector around the widget that you'll tap when you want your keyboard to go away.
In this case, just wrapping entire Scaffold will do the work.
Tapping anyplace on the screen will unfocus and close the keyboard.
https://api.flutter.dev/flutter/widgets/FocusNode/unfocus.html
https://api.flutter.dev/flutter/widgets/GestureDetector-class.html