Home > Enterprise >  How to stop keyboard from getting dismissed?
How to stop keyboard from getting dismissed?

Time:01-18

I am using textformfield in flutter. Whenever user types something I give him suggestion. 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.

CodePudding user response:

Give FocusNode to Textformfield and focus that particular node on tapping of suggestion like this:

//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)
}

CodePudding user response:

You probably have this on

FocusScope.of(context).unfocus();

Or final FocusNode textFieldFocusNode = FocusNode();

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

CodePudding user response:

From the FocusNode documentation:

FocusNodes are ChangeNotifiers, so a listener can be registered to receive a notification when the focus changes.

 final _focusNode = FocusNode();

So you can addListener to it to track changes that happens to it when you assign that FocusNode to the TextFormField:

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:

_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().

  • Related