首页 > 解决方案 > 获取复选框 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);
                        });
                      }
                  );

                }
            ),
          )

,

标签: flutterdartflutter-layout

解决方案


您需要做的就是在被调用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标志。


推荐阅读