Search code examples
flutterlistdartlistviewcheckbox

Add checked boxed values from CheckBox widget to a list in flutter


I'm building a dialog which allow user to use multi Regions by checking the checkBox next to the region name.

what I wanna know is how to add the Checkbox checked values to a list of int to be sent later to the API

And how to make multi select which not affect the value of the others check boxes , What I mean is if I checked the box the whole list stays unchecked

please I've been trying for a long time and got nothing

enter image description here

                                        showDialog(
                                          context: context,
                                          builder: (context) {
                                            return AlertDialog(
                                              content: Column(
                                                crossAxisAlignment: CrossAxisAlignment.center,
                                                mainAxisSize: MainAxisSize.min,
                                                children: [
                                                  Text('يرجي اختيار المناطق',
                                                      textAlign: TextAlign.center,
                                                      style: mediumStyle(
                                                          fontSize: 21,
                                                          color: AppColors.blackColor)),
                                                  SizedBox(
                                                    height: 20.h,
                                                  ),
                                                  SizedBox(
                                                    width: 500,
                                                    height: 250,
                                                    child: ListView.builder(
                                                      itemCount: state.selectedCity!.regions.length,
                                                      itemBuilder: (context, index) {
                                                       bool isChecked = false;
                                                        return CheckboxListTile(
                                                          title: Text(
                                                            state.selectedCity!.regions[index].name,
                                                            style: mediumStyle(fontSize: 20.sp, color: AppColors.blackColor),
                                                          ),
                                                          value: isChecked,
                                                          onChanged: (value) {
                                                            setState(() => isChecked = value!);
                                                            List regions = [];

                                                          },
                                                        );
                                                      },
                                                    ),
                                                  )
                                                ],
                                              ),
                                            );

Solution

  • Try this:

    Map<String, bool> selectedRegions = {};
         state.selectedCity!.regions.for(region){
          selectedRegions[region.name]=false;
         }
    
    
    showDialog(
                                          context: context,
                                          builder: (context) {
                                            return AlertDialog(
                                              content: Column(
                                                crossAxisAlignment: CrossAxisAlignment.center,
                                                mainAxisSize: MainAxisSize.min,
                                                children: [
                                                  Text('يرجي اختيار المناطق',
                                                      textAlign: TextAlign.center,
                                                      style: mediumStyle(
                                                          fontSize: 21,
                                                          color: AppColors.blackColor)),
                                                  SizedBox(
                                                    height: 20.h,
                                                  ),
                                                  SizedBox(
                                                    width: 500,
                                                    height: 250,
                                                    child: ListView(
            children: selectedRegions.keys.map((String key) {
              return new CheckboxListTile(
                title: Text(key,
                style: mediumStyle(fontSize: 20.sp, color: AppColors.blackColor),),
                value: selectedRegions[key],
                onChanged: (bool value) {
                  setState(() {
                    selectedRegions[key] = value;
                  });
                },
              );
            }).toList(),
                                            );