grouped_list icon indicating copy to clipboard operation
grouped_list copied to clipboard

count total item

Open vohoangtuit opened this issue 3 years ago • 1 comments

how to count total items in grouped_list ?

vohoangtuit avatar Aug 03 '22 04:08 vohoangtuit

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

bsolca avatar Sep 19 '22 09:09 bsolca