Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
In this article, we are going to show how to dismiss the keyboard in Flutter the right way.
Before moving further first, we need to know when and why to dismiss the keyboard.
When we click the textField or textFormField in a flutter app, the on-screen keyboard pops up to the screen and the user wants it to be dismissed once click outside the textField or textFormField or any button. But flutter doesn’t provide this out of the box then how to do it?
textField
textFormField
Check our Flutter app development page.
First, we need to detect the tap or click which is outside the textField or textFormField and we can achieve this by wrapping the MaterialApp widget inside the GestureDetector widget.
MaterialApp
GestureDetector
The reason we are wrapping the MaterialApp widget with the GestureDetector is that we want this behavior throughout the Flutter app and for most of the Flutter apps the MaterialApp widget is the root widget.
To trigger the keyboard dismissal in Flutter after clicking outside the textField or textFormField, we need to remove the current focus from the textField or textFormField. First, get the current FocusNode using FocusScope.of(context):
FocusScope.of(context)
In the above code snippet, after getting the current FocusNode we are checking that if this current FocusNode doesn’t have a hasPrimaryFocus and its focusedChild is not null then we are calling the unfocus()on the currently focused node using Focusmanager to remove the focus.
unfocus()
Thanks for reading.
Happy Coding 🙂
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Webkul is a truly trusted and supported IT Company to develop business ideas for e-commerce. They provide professional support after deploying the solution to production and be responsible to act for fixing the reported issues or system errors. We highly encourage to deal with Webkul for business development.
Hussein Zawia
Co-founder, 24Dokan
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.