How to Dismiss the Keyboard in Flutter?

· 2 min read
Dismiss KeyBoard In Flutter
Dismiss KeyBoard In Flutter

While Submitting a Form in Flutter you may have a problem where a keyboard keeps coming on the screen every time. So in this article, we will learn about How to Dismiss a Keyboard in Flutter?

How to Dismiss a Keyboard in Flutter?

You can dismiss the keyboard by taking away the focus of the TextFormField and giving it to an unused FocusNode:

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

Users can also make use of code snippet like below:

import 'package:flutter/services.dart';
SystemChannels.textInput.invokeMethod('TextInput.hide');

For Flutter 1.17.3 stable channel as of June 2020, use

FocusManager.instance.primaryFocus.unfocus();

Another way to Dismiss a Keyboard is to put your widget inside new GestureDetector() on which tap will hide the Keyboard and onTap use the following code snippet.

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

You can also use approach like a below:

onTap: () {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
       currentFocus.focusedChild.unfocus();
    }
}

We, Will get output like below:

Dismiss KeyBoard In Flutter
Dismiss KeyBoard In Flutter

Conclusion:

In this article, we have learned about How to Dismiss the Keyboard in Flutter.

Thanks for Reading!!!
Keep Fluttering.

Do let us know if you need any assistance with Flutter?

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply