问题描述
目前,我知道使用此代码隐藏软键盘的方法,通过任何小部件的 onTap
方法.
Currently, I know the method of hiding the soft keyboard using this code, by onTap
methods of any widget.
FocusScope.of(context).requestFocus(new FocusNode());
但我想通过单击 TextField 外部或屏幕上的任何位置来隐藏软键盘.flutter
中是否有任何方法可以做到这一点?
But I want to hide the soft keyboard by clicking outside of TextField or anywhere on the screen. Is there any method in flutter
to do this?
推荐答案
你做错了,试试这个简单的方法来隐藏软键盘.您只需将整个屏幕包裹在 GestureDetector
方法和 onTap
方法中编写此代码.
You are doing it in the wrong way, just try this simple method to hide the soft keyboard. you just need to wrap your whole screen in the GestureDetector
method and onTap
method write this code.
FocusScope.of(context).requestFocus(new FocusNode());
这是完整的例子:
new Scaffold(
body: new GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: new Container(
//rest of your code write here
)
)
更新(2021 年 5 月)
return GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: Scaffold(
appBar: AppBar(
title: Text('Login'),
),
body: Body(),
),
);
即使您触摸 AppBar,这也会起作用,new
是 Dart 2 中的可选.FocusManager.instance.primaryFocus
将返回当前在小部件树中具有主要焦点的节点.
This will work even when you touch the AppBar, new
is optional in Dart 2. FocusManager.instance.primaryFocus
will return the node that currently has the primary focus in the widget tree.
这篇关于单击屏幕上的文本字段/任何位置后,如何在颤动上隐藏软输入键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!