skip to Main Content

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.

3

Answers


  1. You probably have this on

    FocusScope.of(context).unfocus();
    

    Or final FocusNode textFieldFocusNode = FocusNode();

    TextFormField(
                 focusNode: textFieldFocusNode,
                  onTap: () async {
                   textFieldFocusNode.requestFocus();
                     },
    )
    
    Login or Signup to reply.
  2. 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)
    }
    
    Login or Signup to reply.
  3. 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().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search