Currently, I know the method of hiding the soft keyboard using this code, by onTap methods of any widget.
FocusScope.of(context).requestFocus(new
try this if you are on a stack
body: GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: Container(
height: double.infinity,
width: double.infinity,
color: Colors.transparent,
child: Stack(children: [
_CustomBody(_),
Positioned(
bottom: 15, right: 20, left: 20, child: _BotonNewList()),
]),
),
),
This is best
Scaffold(
body: GestureDetector(
onTap: () {
if (messageFocusNode.hasFocus) {
messageFocusNode.unfocus();
}
},
child: new Container(
//rest of your code write here
)
)
It is true what maheshmnj said that from version v1.7.8+hotfix.2, you can hide keyboard using unfocus() instead of requestfocus().
FocusScope.of(context).unfocus()
But in my case I was still presented with a lot of layout errors, as the screen I navigated to was not capable of handling the layout.
════════ Exception Caught By rendering library ═════════════════════════════════
The following JsonUnsupportedObjectError was thrown during paint():
Converting object to an encodable object failed: Infinity
When the exception was thrown, this was the stack
#0 _JsonStringifier.writeObject (dart:convert/json.dart:647:7)
#1 _JsonStringifier.writeMap (dart:convert/json.dart:728:7)
#2 _JsonStringifier.writeJsonValue (dart:convert/json.dart:683:21)
#3 _JsonStringifier.writeObject (dart:convert/json.dart:638:9)
#4 _JsonStringifier.writeList (dart:convert/json.dart:698:9)
This was handled by inserting "resizeToAvoidBottomInset: false" in the receiving screens Scaffold()
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false, // HERE
appBar: AppBar(
centerTitle: true,
title: Text("Receiving Screen "),
),
body: Container(...)
),
);
}
As of Flutters latest version v1.7.8+hotfix.2, you can hide keyboard using unfocus() instead of requestfocus()
FocusScope.of(context).unfocus()
so whenever you tap in the body part keyboard gets hidden
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Login"),
),
body: GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: Container(...)
),
);
}
I just developed a small package to give any widget the kind of behavior you are looking for: keyboard_dismisser on pub.dev. You can wrap the whole page with it, so that the keyboard will get dismissed when tapping on any inactive widget.
Starting May 2019, FocusNode now has unfocus method:
Cancels any outstanding requests for focus.
This method is safe to call regardless of whether this node has ever requested focus.
Use unfocus if you have declared a FocusNode for your text fields:
final focusNode = FocusNode();
// ...
focusNode.unfocus();
My original answer suggested detach method - use it only if you need to get rid of your FocusNode completely. If you plan to keep it around - use unfocus instead.
If you have not declared a FocusNode specifically - use unfocus for the FocusScope of your current context:
FocusScope.of(context).unfocus();
See revision history for the original answer.