jeudi 23 janvier 2020

How to select only one checkbox from Checkbox Listtile in Flutter

Here I've one service page where I've displayed all the services, and there I can select multiple services. but I want to select only one service. I've used the checkbox Listitle for the service selection. I want that user can select only one service not multiple services at a time.

Here is code i've tried :

Widget _renderServices(AddWalkinServiceViewModel awsVm) {
    List lovCountryServices = searchList.length != 0 ? searchList : finalList;
    if (lovCountryServices == null || lovCountryServices.length == 0) {
      return Container(
        child: Center(
          child: Text("No Services available for this combination"),
        ),
      );
    }
    // print(lovCountryServices);
    return ListView.builder(
      shrinkWrap: true,
      padding: const EdgeInsets.all(8.0),
      itemCount: lovCountryServices.length,
      itemBuilder: (BuildContext context, int index) {
        var item =
            lovCountryServices[index]; // should be outside build function
        List items = item['services'];
        return ExpansionTile(
          title: Text(item['name']),
          children: List.generate(items.length, (i) {
            _selection[items[i]['name']] =
                _selection[items[i]['name']] ?? items[i]['isSelected'];
            return CheckboxListTile(
              title: Text(items[i]['name']),
              value: _selection[items[i]['name']] == null
                  ? false
                  : _selection[items[i]['name']],
              onChanged: (val) {
                setState(() {
                  _selection[items[i]['name']] = val;
                  if (val) {
                    servicesIds.add(items[i]['id']);
                    List<BspServices.Service> services =
                        selectedServices.where((service) {
                      return service.mainCategory == item['name'];
                    }).toList();
                    SubCategory subService = new SubCategory(
                      id: items[i]['id'],
                      name: items[i]['name'],
                    );
                    List<SubCategory> subCategories = [];
                    if (services.length == 0) {
                      subCategories.add(subService);
                      selectedServices.add(
                        new BspServices.Service(
                          mainCategory: item['name'],
                          mainCategoryId: item['id'],
                          subCategory: subCategories,
                        ),
                      );
                    } else {
                      print('services in else');
                      print(services[0].subCategory);
                      subCategories = services[0].subCategory;
                      subCategories.add(subService);
                    }
                  } else {
                    servicesIds.removeWhere((service) {
                      return service == items[i]['id'];
                    });
                    List<BspServices.Service> services =
                        selectedServices.where((service) {
                      return service.mainCategory == item['name'];
                    }).toList();
                    services[0].subCategory.removeWhere((subService) {
                      return subService.id == items[i]['id'];
                    });
                  }
                });
                print('servicesIds after set state');
                print(servicesIds);
              },
            );
          }),
        );
      },
    );
  }



Aucun commentaire:

Enregistrer un commentaire