Home > Net >  How to rename every ListTile differently in flutter app
How to rename every ListTile differently in flutter app

Time:10-10

I have a list title that I want to have 100 tiles I want to have a separate name for every tile. I want to know how can I implement Named arrays from ListView.builder to the Tiles.

My code is something like this:

Code:

final items = List<String>.generate(114, (i) => "index $i");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Chapters Index'),
      ),
      body: ListView.builder(
        padding: const EdgeInsets.all(8),
        itemCount: items.length,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            title: Text(items[index]),
          );
        },
      ),
    );

CodePudding user response:

Declare list like this

List<Item>items=[Item(name:"name1"),tem(name:"name2"),tem(name:"name2")];

Use it like this

ListView.builder(
    padding: const EdgeInsets.all(8),
    itemCount: items.length,
    itemBuilder: (BuildContext context, int index) {
      return ListTile(
        title: Text(items[index].name),
      );
    },
  )

Your Item modal

class Item{
String? name;
Item({required this.name};
}
  • Related