Warm tip: This article is reproduced from stackoverflow.com, please click
flutter textfield

How to ignore newlines in TextField?

发布于 2020-10-10 14:15:09

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();
        }
      },
Questioner
buttonsrtoys
Viewed
2
2,045 2020-08-31 21:30

You should try using BlacklistingTextInputFormatter in inputFormatters.

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