Rendering checkboxes through a loop
You need to store all the checkvalue
s in a List
:
class FilterList extends StatefulWidget { @override _FilterListState createState() => _FilterListState();} //creating a state for checkboxesclass _FilterListState extends State<FilterList> { int i; List<bool> checkvalue = new List<bool>.filled(12, false); // this is new Widget _element(String id, int index) { // index added return Row( children: <Widget>[ Checkbox( value: checkvalue[index], // [index] added onChanged: (value) { setState( () { checkvalue[index] = value; // [index] added }, ); }, ), Text('$id') ], ); } // A new Widget where I get to combine the checkboxes with their respective texts @override Widget build(BuildContext context) { return ListView( children: <Widget>[ for (i = 10; i <= 120; i = i + 10) _element('$i HP', i ~/ 10 - 1) // index added ],// for loop for iterating the widget rendering//HP stands for horsepower... ); }}