且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

移开键盘上的多个按钮的焦点

更新时间:2023-11-24 16:18:22

还需要在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