如何防止在按下Flutter中的Submit键时键盘消失? [英] How to prevent keyboard from dismissing on pressing submit key in flutter?
问题描述
我正在开发一个Flutter应用程序,用户可以在其中键入消息,然后按键盘上的发送按钮以发送消息.问题是,当我按下发送按钮时,消息被发送但键盘自动被关闭.如何防止这种情况发生? 预先感谢.
I'm making a flutter application where user can type a message and hit send button IN THE KEYBOARD to send the message. The problem is when I press the send button the message gets send but the keyboard gets automatically dismissed. How can I prevent that from happening? Thanks in advance.
TextField(
autofocus: true,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: new InputDecoration.collapsed(
hintText: "Let's talk",
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(1),
),
),
textInputAction: TextInputAction.send,
onSubmitted: null,
)
推荐答案
这对我有用:-
首先创建一个FocusNode
并将其分配给您的文本字段,请执行以下操作:-
First Create a FocusNode
and assign it to your textfield, do the following :-
FocusNode
是一个寿命很长的组件,因此请使用initState
方法对其进行初始化:-
The FocusNode
is a long lived component so initialize it in the initState
method:-
FocusNode inputFieldNode;
@override
void initState() {
super.initState();
inputFieldNode = FocusNode();
}
不要忘记在处理窗体后用dispose
方法清理FocusNode
:-
Do not forget to cleanup the FocusNode
in dispose
method after the Form is disposed:-
@override
void dispose() {
inputFieldNode.dispose();
super.dispose();
}
将FocusNode
分配给文本字段,并在onSubmitted:
中编写以下代码:-
Assign the FocusNode
to the textfield and write the following code in onSubmitted:
:-
TextField(
focusNode: inputFieldNode,
onSubmitted: (String) => FocusScope.of(context).requestFocus(inputFieldNode),
)
现在,即使按下提交按钮,文本字段也不会失去焦点.
Now the textfield will not lose focus even after pressing the submit button.
这篇关于如何防止在按下Flutter中的Submit键时键盘消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!