如何忽略 TextField 中的换行符?

but*_*oys 0 textfield flutter

我没有找到答案的简单问题——我有一个多行的文本字段,但我不想在文本中允许换行符。例如,如果用户正在键入并按 [Enter],我不希望在文本字段中注册换行符。我怎么做?

我尝试在 onChanged 事件中捕获它,但得到了奇怪的渲染结果:

      onChanged: (value) {
        if (value.endsWith('\n')) {
          _textController.text = value.trim();
        }
      },
Run Code Online (Sandbox Code Playgroud)

KuK*_*uKu 5

您应该尝试使用BlacklistingTextInputFormatterin inputFormatters.

inputFormatters: [
  FilteringTextInputFormatter.deny(new RegExp(r"\n"))
],
    
Run Code Online (Sandbox Code Playgroud)