移开键盘上的多个按钮的焦点 [英] Removing keyboard focus on multiple buttons pressed
问题描述
当我在textField之外的任何地方点击时,我都试图隐藏键盘.因此,我用GestureDetector包装了Scaffold,并用unfocused()设置了onTap.效果很好,但是当按下按钮时,键盘仍然处于活动状态
I'm trying to hide keyboard when tapped everywhere outside of textField. So I wrapped Scaffold with GestureDetector and set onTap with unfocused(). That works well however when a button is pressed then the keyboard is still active
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Scaffold(
appBar: AppBar(
actions: <Widget>[FlatButton(child: Text('Done'), onPressed: () {})],
),
body: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('something'),
onPressed: () {},
),
TextField(),
],
),
),
);
}
有什么办法可以消除焦点,而不必在所有按钮的onTap中添加未聚焦的焦点.原因是我那里有很多按钮,有些甚至设置了onLogTap,所以会有很多重复的代码
Is there any way to remove the focus without adding that unfocused in onTap of all buttons.. Reason is I've got many buttons there and some has even onLogTap set so there would be a lot of duplicate codes
推荐答案
还需要在FlatButton
FlatButton(
child: Text('something'),
onPressed: () {
FocusScope.of(context).unfocus();
},
),
希望找到一种解决方案,其中我不需要那么多重复的代码即可完成一件事情.
was hoping for some solution where I wouldn't need so many duplicate codes to do one thing.
无法使用AFAIK,因为GestureDetector
小部件的click事件和FlatButton
的click事件不同,
AFAIK that is not possible because the click event of GestureDetector
widget and click event of FlatButton
both are different,
您正在注册FlatButton
的不同/单独的单击事件,这就是为什么当您单击FlatButton
You are registering different/separate click event of FlatButton
that's why your keyboard is not hiding when you click on FlatButton
现在是按下按钮时键盘无法隐藏的原因
Now the reason why your keyboard not hiding when pressed on buttons
因为GestureDetector
小部件的click事件被FlatButton
Because the click event of your GestureDetector
widget if overridden by the click event of your FlatButton
解决方案
您可以做一件事,创建一个隐藏键盘的通用方法,然后通过单击按钮调用该方法.
You can do one thing, create a common method to hide the keyboard, and call that method to from button click
这篇关于移开键盘上的多个按钮的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!