Sub topic 9
GridView.builder for large grids
This chapter assumes you know basic Dart lists and closures, Flutter widgets, and Column, Row, and Expanded. You should also be able to run a Flutter app and use hot reload.
GridView.builder creates grid tiles lazily. Lazy building means Flutter creates only the children that are visible, plus a small area near the viewport. This keeps memory use and startup work lower than creating every tile at once.
Use GridView.builder when the number of items is large, changes at runtime, or comes from a data source.
The basic pattern
A grid needs two important pieces:
gridDelegatecontrols the number and size of columns.itemBuildercreates the tile at a given index.
itemCount tells Flutter how many items exist. Providing it prevents the builder from continuing indefinitely.
import 'package:flutter/material.dart';
class ProductGrid extends StatelessWidget {
const ProductGrid({super.key});
@override
Widget build(BuildContext context) {
final products = List<String>.generate(
1000,
(index) => 'Product ${index + 1}',
);
return Scaffold(
appBar: AppBar(title: const Text('Products')),
body: GridView.builder(
padding: const EdgeInsets.all(8),
itemCount: products.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
childAspectRatio: 1,
),
itemBuilder: (context, index) {
return Card(
child: Center(
child: Text(products[index]),
),
);
},
),
);
}
}crossAxisCount: 2 creates two columns. In a vertical grid, the cross axis is horizontal. mainAxisSpacing adds space between rows, and crossAxisSpacing adds space between columns. childAspectRatio is the tile width divided by its height. A value of 1 creates square tiles.
Choosing a grid delegate
Use SliverGridDelegateWithFixedCrossAxisCount when you want a known number of columns. This is useful when each tile must have a predictable width.
Use SliverGridDelegateWithMaxCrossAxisExtent when tile width matters more than the exact column count. Flutter chooses as many columns as fit without making a tile wider than the maximum.
GridView.builder(
padding: const EdgeInsets.all(8),
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 160,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
childAspectRatio: 1,
),
itemCount: 200,
itemBuilder: (context, index) {
return Card(
child: Center(child: Text('Tile $index')),
);
},
)This second approach adapts to different screen widths. A phone may show two columns, while a tablet may show four or more.
Common errors and performance choices
Do not create expensive work inside itemBuilder for every rebuild. Keep data preparation outside the builder when possible. For network images, use an image-caching strategy and show a placeholder while loading.
If a grid is inside a Column, give it a bounded height with Expanded. Otherwise, Flutter may report an unbounded-height error.
Use stable keys when tiles contain stateful children and can move when the data changes. A key helps Flutter match an existing tile with the same data item instead of reusing state for a different item.
Exercise
Which delegate should you choose when a grid must have exactly three columns on every screen?
Show answer
Use SliverGridDelegateWithFixedCrossAxisCount with crossAxisCount: 3.