[Solved] How to create long list of a widget with dynamic content without writing the same code again and again? [closed]


Welcome to flutter. In flutter, you can do that very easily using ListView.builder.
Just add an item count of 60 and create a list of roll numbers and bool values. Then add any custom widget for example text widget and use the index of itemBuilder and print that index from the list of roll numbers and bool. Through this, you can do a lot of things using very little code.

Example code:

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  List<int> rollNo = [2016239009, 2016239007, 2016239002, 2016239008];
  List<bool> _value = [false, false, false, false];

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: 4,
      itemBuilder: (context, index) {
        return CheckboxListTile(
          title: Text(rollNo[index].toString()),
          value: _value[index],
          onChanged: (bool value) {
            setState(() {
              _value[index] = !_value[index];
            });
          },
          secondary: const Icon(Icons.hourglass_empty),
        );
      },
    );
  }
}

Output would be:

enter image description here

You can look at the dartpad for full code here.

0

solved How to create long list of a widget with dynamic content without writing the same code again and again? [closed]