Flutter 循环创建输入框 TextField
需求:
根据接口返回的数据生成列表,列表内含有可编辑的字段
先初始化一个 TextEditingController 集合
Map<Object, TextEditingController> _numberControllers = Map();
拿到接口数据时进行绑定
listData.forEach((element) { _numberControllers[element] = TextEditingController(); });
在 TextField 绑定 controller
controller: _numberControllers[listData[index]],
TextField 完整代码

TextField( onSubmitted: (value) { listData[index]['actualQuantity'] = _numberControllers[listData[index]].text; }, onChanged: (value) { }, cursorColor: Color(0xff999999), keyboardType: TextInputType.number, textInputAction: TextInputAction.done, autofocus: false, controller: _numberControllers[listData[index]], onEditingComplete: () { FocusScope.of(context).requestFocus(FocusNode()); }, style: TextStyle( color: Color(0xff333333), fontSize: ScreenUtil().setSp(28) ), decoration: InputDecoration( border: InputBorder.none, contentPadding: const EdgeInsets.symmetric(vertical: 10.0), hintText: "请输入数量", hintStyle: new TextStyle( fontSize: ScreenUtil().setSp(28), color: Color(0xff666666) ), ), ),
设置值
listData.forEach((item) { if (_numberControllers[item].text.isNotEmpty) { item['actualQuantity'] = _numberControllers[item].text; } });
放不了图了,自行试一下好了。
The most terrible person in the world is yourself
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· Windows 提权-UAC 绕过