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

Flutter动态TextField无法编辑文本问题求助

Fixing TextField Sync & Editability Issue in Flutter

Hey there! Let's break down why your shipping name TextField isn't updating properly and is stuck in a non-editable state (even though the cursor moves):

What's Causing the Problem?

Looking at your code, the main issue is in the onChanged callback of your shipping TextField:

onChanged: (String val) {
  enteredValue(field,val);
  setState(() {
    shippingNameController.text = val; // This is redundant & problematic
    sShppingfieldList[p].fieldvalue=val;
  });
},

When you update shippingNameController.text inside onChanged, you're creating a conflict. The TextField already syncs its value with the controller automatically—manually setting the controller's text here disrupts the internal state management, leading to weird cursor behavior and blocked editing.

Step-by-Step Fixes

1. Clean Up the Shipping TextField's onChanged

First, remove the redundant shippingNameController.text = val line from the setState. The TextField will handle updating the controller's value on its own:

TextField(
  style: TextStyle(fontFamily: 'semibold', color: MyColors.colorPrimary),
  keyboardType: field.type == 'STR' || field.type == 'EMAIL' ? TextInputType.text : TextInputType.number,
  textInputAction: TextInputAction.done,
  decoration: InputDecoration(
    contentPadding: EdgeInsets.all(8.0),
    hintStyle: TextStyle(fontFamily: 'semibold', color: MyColors.colorPrimary),
    border: InputBorder.none,
  ),
  controller: shippingNameController,
  onChanged: (String val) {
    enteredValue(field, val);
    setState(() {
      sShppingfieldList[p].fieldvalue = val; // Only update your data list here
    });
  },
)

2. Sync Billing Name to Shipping Name Correctly

To sync the billing name's value to the shipping TextField, you have two reliable options:

Option A: Update from Billing TextField's onChanged

If you have a controller for the billing name (let's call it billingNameController), add this to its onChanged callback:

onChanged: (val) {
  // Update your billing data first
  setState(() {
    // ... your existing billing field updates
    shippingNameController.text = val; // Sync to shipping controller
  });
}
Option B: Listen to Billing Controller's Changes

For a more reactive approach, add a listener to the billing controller when your widget initializes (e.g., in initState):

@override
void initState() {
  super.initState();
  // Listen for changes in billing controller
  billingNameController.addListener(() {
    setState(() {
      shippingNameController.text = billingNameController.text;
    });
  });
}

// Don't forget to dispose the listener when the widget is disposed
@override
void dispose() {
  billingNameController.removeListener(() {});
  billingNameController.dispose();
  shippingNameController.dispose();
  super.dispose();
}

Why This Works

By letting the TextField manage its controller's value naturally, you avoid state conflicts. Syncing from the billing side directly updates the shipping controller's text, which the TextField will reflect immediately without blocking editing.

内容的提问来源于stack exchange,提问作者Farhana Naaz Ansari

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:19:52