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)
                      ),
                    ),
                  ),    
View Code
复制代码

 

设置值

listData.forEach((item) {
    if (_numberControllers[item].text.isNotEmpty) {
          item['actualQuantity'] = _numberControllers[item].text;
     }
 });

 

 

 

放不了图了,自行试一下好了。


posted @   _whys  阅读(370)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· Windows 提权-UAC 绕过
点击右上角即可分享
微信分享提示