Fayakon
Fayakon

Reputation: 1523

How to pass parameters in Flutter ListView

i want to pass list of title and description in my list view, i can pass in list view builder, but i don't how to pass in list view.

i want to pass entries array in ListView. without typing each entry i want to show everything in array on cards.

 Widget build(BuildContext context) {
 final List<String> entries= <String> ['Entry one','Entry Two','Entry Three','Entry one','Entry Two','Entry Three','Entry one','Entry Two','Entry Three','Entry one','Entry Two','Entry Three'];

    return Scaffold(
      appBar: AppBar(
        title: Text('title'),
      ),

      body: ListView(

  children: const <Widget>[
  Card(
      child: ListTile(
        leading: FlutterLogo(size: 40.0),
        title: Text('all entries one by one'),
        subtitle: Text(
          'subtitle'
        ),
        trailing: Icon(Icons.favorite),
        isThreeLine: true,
      ),
    ),
  ],
)
    );

  }

Upvotes: 0

Views: 3682

Answers (2)

furkeen
furkeen

Reputation: 421

You can use ListView.builder() for this. Here is an example.

class TestPage extends StatelessWidget {
  final List<String> entries = <String>[
    'Entry one',
    'Entry Two',
    'Entry Three',
    'Entry one',
    'Entry Two',
    'Entry Three',
    'Entry one',
    'Entry Two',
    'Entry Three',
    'Entry one',
    'Entry Two',
    'Entry Three'
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('title'),
      ),
      body: Container(
        height: 500,
        child: ListView.builder(
          itemCount: entries.length,
          itemBuilder: (BuildContext context, int index) {
            return Card(
              child: ListTile(
                leading: FlutterLogo(size: 40.0),
                title: Text(entries[index]),
                subtitle: Text('subtitle'),
                trailing: Icon(Icons.favorite),
                isThreeLine: true,
              ),
            );
          },
        ),
      ),
    );
  }
}

Upvotes: 2

Hussam
Hussam

Reputation: 1686

If you already have array of title and description using the ListView.builder() is the best option here. First make a class for your title and description:

class Info{
  String title;
  String description;
  Info(this.title, this.description);
}

Now create an array of this info class List<Info> _myInfo and populate it with your data. Now you can create your list view like this:

ListView.builder(
  itemCount: _myInfo.length,
  builder: (context, index) {
    return _createCard(index);
  }
);

Now you can separate your card creation here like this:

Widget _createCard(int index){
  return Card(
    child: ListTile(
      leading: FlutterLogo(size: 40.0),
      //here your title will be create from array for each new item
      title: Text(_myInfo[index].title),
      subtitle: Text(
        'subtitle'
      ),
      trailing: Icon(Icons.favorite),
      isThreeLine: true,
    ),
  ),
}

Code might contain syntax error feel free to ask if you face any problem

Upvotes: 1

Related Questions