Learn Pain Less

HomeOur TeamContact
Flutter
How to Close Keyboard Using Code in Flutter - 3 Methods
Pawneshwer Gupta
Pawneshwer Gupta
March 09, 2023
2 min

Table Of Contents

01
Method 1: Using the FocusNode class
02
Method 2: Using the TextInputAction.done property
03
Method 3: Using the GestureDetector class
How to Close Keyboard Using Code in Flutter - 3 Methods

At our company, we know that one of the most important things when developing a mobile app is to provide users with a smooth and seamless experience. This includes making sure that the keyboard is easy to use and that it disappears when the user is done with it. In this article, we will show you how to close the keyboard using code in Flutter.

What is Flutter?

Flutter is an open-source framework created by Google that allows developers to create high-performance, high-fidelity apps for iOS and Android using a single codebase. It provides a rich set of pre-built widgets that can be customized to create a unique look and feel for your app.

Closing the Keyboard in Flutter

When a user taps on a text field, the keyboard appears, covering part of the screen. In most cases, you want the keyboard to disappear when the user is done entering text. There are a few ways to do this in Flutter.

Method 1: Using the FocusNode class

The first method is to use the FocusNode class. This class represents a node in the focus traversal order. It can be used to request and release the focus and listen to changes in the focus state.

To close the keyboard when the user is done, you need to call the unfocus method of the FocusNode class. This method removes the focus from the current widget, which causes the keyboard to disappear.

Here’s an example of how to use the FocusNode class:

final _focusNode = FocusNode();
TextField(
focusNode: _focusNode,
// Other properties...
);
// When the user is done, call:
_focusNode.unfocus();

Method 2: Using the TextInputAction.done property

The second method is to use the TextInputAction.done property. This property defines the action that should be taken when the user presses the done button on the keyboard.

To close the keyboard, you can set the TextInputAction.done property to TextInputAction.done and then define a function to be called when the user presses the done button. In this function, you can call the unfocus method of the FocusNode class to close the keyboard.

Here’s an example:

final _focusNode = FocusNode();
TextField(
focusNode: _focusNode,
textInputAction: TextInputAction.done,
onEditingComplete: () => _focusNode.unfocus(),
// Other properties...
);

Method 3: Using the GestureDetector class

The third method is to use the GestureDetector class. This class provides a way to handle gestures, such as taps, swipes, and long presses.

To close the keyboard, you can wrap your text field in a GestureDetector and define a function to be called when the user taps outside the text field. In this function, you can call the unfocus method of the FocusNode class to close the keyboard.

Here’s an example:

final _focusNode = FocusNode();
GestureDetector(
onTap: () => _focusNode.unfocus(),
child: TextField(
focusNode: _focusNode,
// Other properties...
),
);

Conclusion

In this article, we showed you how to close the keyboard using code in Flutter. We presented three methods you can use to ensure the keyboard disappears when the user is done with it. We hope this information will help you create a more user-friendly app.

Learn Flutter in 90 days with Pawneshwer!

Dart Beginners Course in Hindi free of cost for limited period.

Start Learning

250+

LESSONS

30+

COURSES

15+

TUTORS

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

Close KeyboardGuideHow To

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

How to Refresh Specific Widget with setState using StatefulBuilder() in Flutter
How to Refresh Specific Widget with setState using StatefulBuilder() in Flutter
April 20, 2023
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media