I am trying to select multiple list tiles but it is selecting all the list tiles when I only select one. Could someone guide me on this?
Here is my code:
bool _selected = false;
body: ListView.builder(
itemCount: list.length,
itemBuilder: (_, i) {
return ListTile(
tileColor: _selected ? Colors.blue : null,
title: Text(list[i]['name']),
onTap: () => setState(() {
if (_selected = !_selected) {
groupList.add(list[i]);
print(groupList);
_selected = _selected;
} else if (_selected = !_selected) {
groupList.remove(list[i]);
print(groupList);
_selected = !_selected;
}
}));
})
Here is the image of the list tile https://i.stack.imgur.com/9EknP.jpg I selected Candice Cook but it selected all the tiles.
You should keep record of
item
s that has been selected in a differentList
orMap
so that when you add or remove items from that list you can update the UI accordingly.