Leonidas Yopan
Leonidas Yopan

Reputation: 139

How can I make "select all" and "unselect all" buttons using Flutter?

I have a "AlertDialog" with a Checklist (CheckboxListTile) and I wanted to add two buttons at the top. One with the option "Select All" and the other "Unselect All". How can I implement these two buttons that handles the state of all the items in the list?

Upvotes: 0

Views: 2884

Answers (1)

w461
w461

Reputation: 2678

let's assume that you have loads of checkboxes and you assign a value to each of them

List<bool> checkBoxValues;

...

CheckBox(
  value: checkBoxValues[0] // or i if you automate this
)

Then you can easily set all values by

CupertinoButton(
  child: Text("check all"),
  onPressed: () {
    setState (() {
      for (var i = 0; I checkBoxValues.length < ; i++)
        checkBoxValues[i] = true;
    });
  },
)

Upvotes: 2

Related Questions