How to automatically deselect all items below the selected one flutter

317 Views Asked by At

I have a custom list view with selectable items. And I am trying to deselect all items automatically present below the one I selected. For Ex: Suppose there is 10 items in the list view and I deselected 5th then it should deselect all the items available below 5th. i.e(5,6,7,8,9)

1

There are 1 best solutions below

0
On

You can do something like this!

int selectedIndex = 0;  //default 0 means we should display all  checkbox selected

ListView.builder(
         itemCount: 10,
         itemBuilder: (context, index) {
             return Checkbox(
              value: selectedIndex == 0
                              ? true //first loading all are checked
                   : index > selectedIndex  //selected index check
                   ? false //if 5 selected then 6,7,8,9,10 are blank
                   : true, //rest of them are checked

              onChanged:(val){
                 setState((){
                    selectedIndex = index;
                  });
               },
            );
           },
      ),