Dimibe / grouped_list

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

count total item #160

Open vohoangtuit opened 2 years ago

vohoangtuit commented 2 years ago

how to count total items in grouped_list ?

bsolca commented 2 years ago

Hello @vohoangtuit,

Here is my solution to add the number of items in each group.

final List<MapEntry<String, int>> _textList = [
  const MapEntry('Lorem', 0),
  const MapEntry('ipsum', 1),
  const MapEntry('dolor', 2),
  const MapEntry('sit', 3),
  const MapEntry('amet', 4),
  const MapEntry('consectetur', 5),
  const MapEntry('adipiscing', 6),
  const MapEntry('elit', 7),
  const MapEntry('sed', 8),
  const MapEntry('do', 9),
  const MapEntry('eiusmod', 10),
  const MapEntry('tempor', 11),
  const MapEntry('incididunt', 12),
  const MapEntry('ut', 14),
];
String groupBy(MapEntry<String, int> e) => e.value.isEven ? 'Even' : 'Odd';

Now you can use the groupHeaderBuilder and used the groupBy function to count the number of item in the group.

groupHeaderBuilder: (MapEntry<String, int> e) => Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
            '${e.key} with ${_textList.where((test) {
              return groupBy(e) == groupBy(test);
            }).length} elements',
            textAlign: TextAlign.left,
            style: const TextStyle(
              fontSize: 20,
              fontWeight: FontWeight.bold,
              color: Colors.pink,
            ),
          ),
        ),

Result: image