简体   繁体   English

如何阻止键盘被解雇?

[英]How to stop keyboard from getting dismissed?

I am using textformfield in flutter. Whenever user types something I give him suggestion.我在 flutter 中使用textformfield 。每当用户键入内容时,我都会给他建议。 When user taps on suggestion, keyboard is dismissed automatically.当用户点击建议时,键盘会自动消失。 I want to stop keyboard from getting dismissed.我想阻止键盘被解雇。 How to do it?怎么做? I want user to add suggestion continuously.我希望用户不断添加建议。 Thanks in advance.提前致谢。

From the FocusNode documentation :来自FocusNode文档

FocusNodes are ChangeNotifiers, so a listener can be registered to receive a notification when the focus changes. FocusNodes 是 ChangeNotifiers,因此可以注册一个侦听器以在焦点更改时接收通知。

 final _focusNode = FocusNode();

So you can addListener to it to track changes that happens to it when you assign that FocusNode to the TextFormField :因此,当您将该addListener分配给TextFormField时,您可以向它添加FocusNode以跟踪它发生的变化:

TextFormField(
 focusNode: _focusNode,
 // ...
 ),

You can add the listener, then check if it has focus, which means that you're checking if the TextFormField field is focused, which also means that if the keyboard is shown:您可以添加侦听器,然后检查它是否具有焦点,这意味着您正在检查TextFormField字段是否获得焦点,这也意味着如果显示键盘:

_focusNode.addListener(() {
  if(!_focusNode.hasFocus) {
    _focusNode.requestFocus();
  }
});

by this, it will remain the TextFormField focused, when you're done with it, you can call the removeListener() .这样,它将保持TextFormField的焦点,当你完成它时,你可以调用removeListener()

You probably have this on你可能有这个

FocusScope.of(context).unfocus();

Or final FocusNode textFieldFocusNode = FocusNode();或者最终的 FocusNode textFieldFocusNode = FocusNode();

TextFormField(
             focusNode: textFieldFocusNode,
              onTap: () async {
               textFieldFocusNode.requestFocus();
                 },
)

Give FocusNode to Textformfield and focus that particular node on tapping of suggestion like this:将 FocusNode 赋予 Textformfield 并将该特定节点集中在点击建议上,如下所示:

//Assign this inputNode to TextFormField
FocusNode inputNode = FocusNode();

//Somewhere on TextFormField
TextFormField(
  focusNode:inputNode
)

// to open keyboard call this function;
void openKeyboard(){
   FocusScope.of(context).requestFocus(inputNode)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM