You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TextField自定义TextInputFormatter失效:添加后无法输入内容

问题解决:自定义TextInputFormatter导致TextField无法输入

你的问题出在LowerCaseFormatter的实现上——你直接返回了oldValue,这会让TextField每次输入新内容时,强制回退到之前的文本状态,自然无法接收任何新输入。

要实现小写转换的正确格式化器,应该基于newValue处理后返回,示例代码如下:

class LowerCaseFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    // 将新输入文本转为小写,同时保留光标位置
    return TextEditingValue(
      text: newValue.text.toLowerCase(),
      selection: newValue.selection,
    );
  }
}

核心原则:自定义TextInputFormatter时,必须根据newValue(用户的新输入)进行逻辑处理后返回新的TextEditingValue,而不是直接返回oldValue,否则会阻断所有输入更新。

内容的提问来源于stack exchange,提问作者Sameer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 02:35:19