【问题标题】:How to generate list of items from checkbox selection in flutter?如何从颤动中的复选框选择中生成项目列表?
【发布时间】:2019-04-19 13:08:43
【问题描述】:

我正在尝试从复选框选择中创建一个 id 和名称列表,我想在 navigator.pop 中传递该数组列表,但不知何故我无法做到。?

我尝试创建模型并将其放在列表对象中,它可以在列表或数组中为我提供我选择的值,但我得到了最后一个额外的(逗号(,))

我的复选框和代码用于创建列表并将其传递给 navigator.pop。

THIS IS MAIN PAGE WHERE I WANT TO GET AND REDIRECT TO SECOND LIST PAGE.

var tempRoomFace;

getRoomFaceData() async {
    tempRoomFace = await Navigator.push(
        context, MaterialPageRoute(builder: (context) => RoomFacilities()));
        for (int i = 0; i < areaDataResult.length; i++) {
      _selectedroom = _selectedroom + tempRoomFace[i].name + ", ";
      _selectedroomID = _selectedroomID + tempRoomFace[i].id + ", ";
      print("selected rooms : $_selectedroomID");
    }
  }


THIS IS LIST PAGE... TO CREATE LIST

------------------ code for passing value  ---------------------

List<RoomFacilityModel.Message> tempRoomData =
      List<RoomFacilityModel.Message>();
  //
  List<RoomFacilityModel.Message> roomListOBJ =
      List<RoomFacilityModel.Message>();

onPressed: () {
                  Navigator.pop(context, tempRoomData);
                  for (int i = 0; i < tempRoomData.length; i++) {
                    print(
                        "City List : ${tempRoomData[i].fcid} + ${tempRoomData[i].name} ");
                  }
                },

-------------------   Code for checkbox  ------------------

Container(
                child: CheckboxListTile(
                    title: Row(
                      children: <Widget>[
                        Container(
                          padding: EdgeInsets.all(5),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(3),
                            color: Colors.black,
                          ),
                          height: 26,
                          width: 26,
                          child: Image.network(
                            roomListOBJ[index].icon,
                          ),
                        ),
                        SizedBox(width: 10),
                        Text(roomListOBJ[index].name),
                      ],
                    ),
                    value: roomListOBJ[index].isCheck,
                    onChanged: (bool value) async {
                      //
                      setState(() {
                        roomListOBJ[index].isCheck = value;
                        if (value) {
                          tempRoomData.add(RoomFacilityModel.Message(
                              fcid: roomListOBJ[index].fcid,
                              name: roomListOBJ[index].name));
                        } else {
                          tempRoomData.removeAt(index);
                        }
                      });
                    }),
              );



I want to get resulat on my main page as
tempRoomFace = [TV, Safe box, curtains, iron]
tempRoomFaceID = [3,4,8,1]

【问题讨论】:

    标签: android dart flutter checkboxlist


    【解决方案1】:

    您可以随意修改。

    class MultiCheckBoxField extends StatelessWidget {
      const MultiCheckBoxField({
        Key key,
        this.count = 1,
        this.onSaved,
      }) : super(key: key);
    
      final int count;
      final FormFieldSetter<List<bool>> onSaved;
    
      @override
      Widget build(BuildContext context) {
        return FormField<List<bool>>(
          initialValue: List.filled(count, false),
          onSaved: onSaved,
          builder: (FormFieldState field) {
            return Column(
              mainAxisSize: MainAxisSize.min,
              children: List.generate(
                count,
                (int index) {
                  return Checkbox(
                    onChanged: (bool value) {
                      field.value[index] = value;
                      field.didChange(field.value);
                    },
                    value: field.value[index],
                  );
                },
              ),
            );
          },
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多