Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent keyboard from dismissing on pressing submit key in 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,
)
like image 822
Piyush Avatar asked Apr 26 '19 08:04

Piyush


People also ask

How do I manage my keyboard on Flutter?

Calling or dismissing the keyboard on tap If the application requires filling a form with user data, there might be more text fields on that screen. Some might need only text input, and some only numbers or both. Tapping on these widgets will cause the keyboard to open.

How do you hide soft input keyboard on Flutter after clicking outside TextField anywhere on screen?

First, you need to get the instance of FocusManager . Then, you can get the node that has the primary focus from the primaryFocus property whose type is FocusNode . If the primaryFocus is not null, you can call the unfocus() method of the FocusNode instance.

How do you turn off the behavior bottom navigation bar goes up with keyboard in Flutter?

bottomNavigationBar: isKeyboardOpen ? null : BottomAppBar(); This technique also works with the floating action bottom issue.. Save this answer.


2 Answers

The cleanest approach would be to use onEditingComplete() with TextEditingController instead of onSubmitted(text). Refer below example.

  final _controller = TextEditingController();
  TextField(
       controller: _controller,
       padding: EdgeInsets.all(8),
       textInputAction: TextInputAction.send,
       placeholder: 'Type your message',
       onEditingComplete: (){
           if (_controller.text.isEmpty) return;

           sendMessage(_controller.text);
       },
  ),
like image 112
Sanket Naik Avatar answered Sep 18 '22 04:09

Sanket Naik


TextField widget has a parameter just for this purpose!

While onSubmit callback can be used to handle business logic when the user presses done, there is also a property called onEditingComplete, specially made to handle focus-related logic. So you should use both, for better code readability.

According to the docs:

The default implementation of onEditingComplete executes 2 different behaviors based on the situation:

When a completion action is pressed, such as "done", "go", "send", or "search", the user's content is submitted to the controller and then focus is given up.

When a non-completion action is pressed, such as "next" or "previous", the user's content is submitted to the controller, but focus is not given up because developers may want to immediately move focus to another input widget within onSubmitted.

Therefore, if you don't like this behaviour, for example, "send" is considered a "completion action" here, thus in an Instant Messaging (chatting) app, each time user sends a short message, the keyboard will be collapsed. But if we override the onEditingComplete callback to an empty function, it will stop the default behavior and not hide the keyboard.

Sample code:

TextField(
  controller: _controller,
  onSubmitted: (value) {
    sendMessage(text);
    _controller.clear();
  },
  onEditingComplete: () {}, // this prevents keyboard from closing
  textInputAction: TextInputAction.send,
)

Demo:

demo gif

For complete explanation and comparison between onSubmitted and onEditingComplete callbacks, check out my other answer here.

like image 42
user1032613 Avatar answered Sep 18 '22 04:09

user1032613