GridView.builder Widget and Attributes

GridView.builder Widget and Attributes

ยท

2 min read

The GridView.builder widget in Flutter is an efficient way to create a grid of items lazily, on-demand. It's particularly useful when dealing with a large number of items, as it only creates widgets for items that are currently in view. In this section, we'll explore the attributes of the GridView.builder widget and provide an example for your blog.

1. Attributes of GridView.builder Widget:

1.1 gridDelegate:

  • Description: Defines the layout of the grid, such as the number of columns and their spacing.

  • Example:

      GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 16.0,
          mainAxisSpacing: 16.0,
        ),
        itemBuilder: (context, index) {
          return /* your widget for item at index */;
        },
        itemCount: /* total number of items */,
      ),
    

1.2 itemBuilder:

  • Description: A callback that returns the widget for a specific index in the grid.

  • Example:

      GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 16.0,
          mainAxisSpacing: 16.0,
        ),
        itemBuilder: (context, index) {
          return Image.network('https://example.com/image$index.jpg');
        },
        itemCount: 10,
      ),
    

1.3 itemCount:

  • Description: The total number of items in the grid.

  • Example:

      GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 16.0,
          mainAxisSpacing: 16.0,
        ),
        itemBuilder: (context, index) {
          return /* your widget for item at index */;
        },
        itemCount: 20,
      ),
    

1.4 scrollDirection:

  • Description: Specifies the scrolling direction of the grid, either horizontal or vertical.

  • Example:

      GridView.builder(
        scrollDirection: Axis.horizontal,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 16.0,
          mainAxisSpacing: 16.0,
        ),
        itemBuilder: (context, index) {
          return /* your widget for item at index */;
        },
        itemCount: /* total number of items */,
      ),
    

2. Example of GridView.builder Widget:

Let's create a Flutter application that uses the GridView.builder widget to dynamically create a grid of images.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('GridView.builder Example'),
        ),
        body: GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            crossAxisSpacing: 16.0,
            mainAxisSpacing: 16.0,
          ),
          itemBuilder: (context, index) {
            return Image.network('https://example.com/image$index.jpg');
          },
          itemCount: 10,
        ),
      ),
    );
  }
}

In this example, we've created a Flutter app with a GridView.builder containing a grid of images. The gridDelegate is set to have two columns with spacing between them. The itemBuilder callback dynamically creates Image widgets based on the index, allowing for efficient creation of a large number of items.

Did you find this article valuable?

Support Vinit Mepani by becoming a sponsor. Any amount is appreciated!

ย