flutter - 获取复选框 id 作为数组颤动
问题描述
我有复选框颤动,所以我需要从复选框中选择多个项目,我需要作为数组传递给一个函数我该怎么做,我可以获得一个数据而不是多个选定的项目
Container(
width: double.infinity,
height: 250,
child: new ListView.builder(
itemCount: service_type.length,
itemBuilder: (BuildContext context, int index){
return new CheckboxListTile(
value: inputs[index],
title: new Text(service_type[index].service_type_name),
controlAffinity: ListTileControlAffinity.leading,
onChanged:(bool val){
ItemChange(val, index);
setState(() {
selectedChecboxmenuid=service_type[index].service_type_id;
print(selectedChecboxmenuid);
});
}
);
}
),
)
,
解决方案
您需要做的就是在被调用isSelected=false
的对象内创建一个名为的新字段service_type
(不是列表,它应该在类内)
在单击项目时,将状态更改isSelected=true
为特定对象,
前任:
List<int> list = [];
onChanged:(bool status){
ItemChange(val, index);
setState(() {
service_type[index].isSelected = status;
for(var service in service_type) {
if(service.isSelected) {
list.add(service_type[index].service_type_id);
// now pass this list wherever you want
}
}
});
}
现在我们知道,从列表中选择了哪个项目,所以现在您可以迭代列表并获取service_type_id
基于isSelected
标志。
推荐阅读
- oracle - Oracle XE 和 DBeaver:ORA-12514,TNS:listener 目前不知道连接描述符中请求的服务
- c - 找不到命令分配数组脚本bash
- java - Java递归方法返回错误值
- reactjs - useFirestoreConnect with populates - 使用/实现
- python - 如何向 pytorch 中的一个网络输出通道添加可学习的偏差
- powershell - PowerShell - 检查用户是否通过安全组访问文件夹
- ios - XCode Swift - MultiPeerConnectivity / MCAdvertiserAssistant 不呈现连接的接受/拒绝对话
- python - 旧目录中的 pipenv 搜索 python 不存在
- python - 当我考虑到索引范围时,总是出现此错误“IndexError:字符串索引超出范围”
- python - 更改列表中包含的 Pandas 数据框的列名