buttonsrtoys
buttonsrtoys

Reputation: 2781

How to ignore newlines in TextField?

Simple question that I'm not finding an answer to--I have a TextField that's multi-line but I don't want to allow newlines in the text. E.g., if the user is typing and hits [Enter], I don't want a newline to register in the TextField. How do I do that?

I tried catching it in the onChanged event, but got weird rendering results:

      onChanged: (value) {
        if (value.endsWith('\n')) {
          _textController.text = value.trim();
        }
      },

Upvotes: 0

Views: 1549

Answers (2)

KuKu
KuKu

Reputation: 7512

You should try using BlacklistingTextInputFormatter in inputFormatters.

inputFormatters: [
  FilteringTextInputFormatter.deny(new RegExp(r"\n"))
],
    

Upvotes: 6

Jim
Jim

Reputation: 7601

What if you use following parameters:

TextField(keyboardType: TextInputType.text, maxLines: 3,);

Upvotes: 1

Related Questions