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

Flutter问题:安卓模拟器点击勾选/回车按钮后TextFormField文本消失

Flutter安卓模拟器TextFormField回车后文本消失问题解决

问题原因

你给TextFormField设置的onFieldSubmitted回调里,手动执行first_name_controller.text = value;属于多余操作——TextFormField和绑定的controller会自动同步输入内容,无需手动赋值。在安卓模拟器点击回车/勾选按钮触发该回调时,传入的value可能出现空值或同步异常,直接覆盖清空了原有文本。

解决方案

直接移除onFieldSubmitted回调即可。如果需要在提交时执行其他逻辑(比如聚焦下一个输入框),可以在回调内处理对应操作,但不要手动给controller赋值文本。

修改后的代码示例

final name_label = Text(
  '    First Name:',
  style: TextStyle(color: Colors.black54),
);

final name = TextFormField(
  validator: (val) {
    if (val?.isEmpty ?? true) {
      return 'This Field Cannot Be Empty';
    } else if (val!.length > 20) {
      return "This Field Can't Have more than 20 characters";
    } else {
      return null;
    }
  },
  controller: first_name_controller,
  keyboardType: TextInputType.name,
  autofocus: false,
  decoration: InputDecoration(
    hintText: 'Joe',
    contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
    border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
  ),
);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 19:20:27