Dimibe / grouped_list

A Flutter ListView in which items can be grouped into sections.
https://pub.dev/packages/grouped_list
MIT License
415 stars 107 forks source link

Unable to group Map #57

Closed shanthosh2910 closed 4 years ago

shanthosh2910 commented 4 years ago

I have a list of map List<SongInfo> songs;

image

This is how I will use the normal ListView

Widget _buildApps(songs) => ListView.builder(
      itemCount: songs.length,
      itemBuilder: (BuildContext context, int index) =>
          _buildRow(songs[index]));

  Widget _buildRow(SongInfo song) {
    final saved = musicModel.getSongs().contains(song.filePath);
    return ListTile( title: Text("${song.title}"));

Now I want to group all the songs by song.album.

Dimibe commented 4 years ago

@shanthosh2910 What have you tried so far and what is not working?

shanthosh2910 commented 4 years ago

I tried this. Getting NoSuchMethodError

Widget _buildApps(songs) => GroupedListView(
      elements: songs,
      groupBy: (song) => song.album,
      itemBuilder: (context, song) => _buildRow(song));
Dimibe commented 4 years ago

@shanthosh2910 As mentioned in the readme, you neeed to specify groupSeparatorBuilder which is a function which returns a widget for the group header (equal to itemBuilder for items). There you can customize the group header as you like.

In your case a very basic solution would be something like this:

groupSeparatorBuilder: (album) => Text('$album'),
shanthosh2910 commented 4 years ago

Thank you!!!