DEV Community

CodeTrade India Pvt. Ltd.
CodeTrade India Pvt. Ltd.

Posted on

Use of ListView.builder With groupBy Method In Flutter

Image description

Flutter, a versatile and open-source UI toolkit, provides developers with a comprehensive Software Development Kit (SDK) to build mobile, desktop, and web applications.

Its cross-platform development capabilities allow developers to write code once and deploy it on both iOS and Android platforms. It’s an attractive choice for developers looking to streamline their app development process.

Use of ListView.builder with groupBy Method in Flutter

When building Flutter applications, it’s common to display lists of items grouped by certain criteria. The ‘ListView.builder’ widget provides an efficient way to dynamically render large lists. In this blog post, we will explore how to use ‘ListView.builder’ with the ‘groupBy’ functionality to group items in Flutter.

Step 1: Import Necessary Packages

To start, import the following packages at the top of your Dart file:

import 'package:flutter/material.dart';
import 'package:collection/collection.dart';
Enter fullscreen mode Exit fullscreen mode

Step 2: Define the Data Model

Assume you have a data model representing items with a category field. Build a class to represent an item in your data model.

class Item {
  final String name;
  final String category;

  Item({required this.name, required this.category});
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Create A List Of Items

Next, create a list of ‘Item’ objects, each with a name and category:

List itemList = [
  Item(name: 'Item 1', category: 'Category A'),
  Item(name: 'Item 2', category: 'Category A'),
  Item(name: 'Item 3', category: 'Category B'),
  Item(name: 'Item 4', category: 'Category B'),
  Item(name: 'Item 5', category: 'Category B'),
];
Enter fullscreen mode Exit fullscreen mode

Step 4: Group Items By Category

To group the items based on their categories, define a function that utilizes the ‘groupBy’ function from the ‘collection’ package:

Map> groupItemsByCategory(List items) {
  return groupBy(items, (item) => item.category);
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Implement The Build Method

Now we can use ‘ListView.builder’ to build the grouped list in the ‘build’ method of a Flutter widget. Follow these steps within the ‘build’ method:

**a. **Group the items by category using the ‘groupItemsByCategory’ function:

Map> groupedItems = groupItemsByCategory(itemList);
Enter fullscreen mode Exit fullscreen mode

**b. **Use ‘ListView.builder’ to construct the list view:

return ListView.builder(
  itemCount: groupedItems.length,
  itemBuilder: (BuildContext context, int index) {
    String category = groupedItems.keys.elementAt(index);
    List itemsInCategory = groupedItems[category]!;

    // Return a widget representing the category and its items
    return Column(
      children: [
        Text(category, style: TextStyle(fontWeight: FontWeight.bold)),
        ListView.builder(
          shrinkWrap: true,
          physics: ClampingScrollPhysics(),
          itemCount: itemsInCategory.length,
          itemBuilder: (BuildContext context, int index) {
            Item item = itemsInCategory[index];
            // Return a widget representing the item
            return ListTile(
              title: Text(item.name), );
          }, ),
      ], );
  },
);
Enter fullscreen mode Exit fullscreen mode

In the code snippet above, we access each category and its corresponding items using ‘groupedItems.keys.elementAt(index)’ and ‘groupedItems[category]!’, respectively. We display the category as a ‘Text’ widget and use another ‘ListView.builder’ to display the items within that category as ‘ListTile’ widgets.

Top comments (0)