samedi 14 novembre 2020

Uncheck a checkbox when user checking another one (bool of checkbox is in a map)

I have in my app a few checkboxes and I want, when the user checks a checkbox, that the other checkboxes are unchecked. That's normally not difficult at all, but the problem is that I have the bool and the title of the checkbox in a map. How can I uncheck all checkboxes except the one the user checkt?

This is the map:

Map<String, bool> filterOptions = {
  'Test1': false,
  'Test2': false,
};

This is the code where I "build" the checkboxes:

ListView(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
    children: filterOptions.keys.map((String key) {
      return CheckboxListTile(
        title: Text(key),
        value: filterOptions[key],
        onChanged: (bool value) {
          setState(() {
            filterOptions[key] = value;
          });
        },
      );
    }).toList(),
  ),



Aucun commentaire:

Enregistrer un commentaire