【问题标题】:How to ignore newlines in TextField?如何忽略 TextField 中的换行符?
【发布时间】:2020-06-18 01:02:48
【问题描述】:

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

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

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

【问题讨论】:

    标签: flutter textfield


    【解决方案1】:

    您应该尝试在inputFormatters 中使用BlacklistingTextInputFormatter

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

    【讨论】:

      【解决方案2】:

      如果你使用以下参数会怎样:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-17
        • 2017-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        • 2011-07-07
        相关资源
        最近更新 更多