Sub topic 8
Controlling columns with grid delegates
Assume you know basic Dart lists and closures, Flutter widget composition, and Row, Column, and Expanded. You should also be able to run a Flutter app and use hot reload.
A grid delegate is an object that tells a GridView how to arrange its children. It controls the number or maximum width of columns, spacing, and tile proportions.
Choose fixed columns or adaptive columns
Use SliverGridDelegateWithFixedCrossAxisCount when the number of columns must stay fixed. For example, a two-column product grid remains two columns on every screen.
Use SliverGridDelegateWithMaxCrossAxisExtent when tile width matters more than the exact column count. Flutter chooses as many columns as fit while keeping each tile below the requested maximum width.
| Delegate | Main decision | Good for |
|---|---|---|
| `WithFixedCrossAxisCount` | Exact number of columns | A design requiring two or three columns |
| `WithMaxCrossAxisExtent` | Maximum tile width | Responsive grids across different screen widths |
The cross axis is horizontal when the grid scrolls vertically. Therefore, crossAxisCount: 3 means three columns. The main axis is vertical, so mainAxisSpacing controls the gap between rows and crossAxisSpacing controls the gap between columns.
Fixed column count
GridView.count is a convenience constructor for a fixed-count grid. It creates a SliverGridDelegateWithFixedCrossAxisCount internally.
GridView.count(
crossAxisCount: 2,
mainAxisSpacing: 12,
crossAxisSpacing: 12,
padding: const EdgeInsets.all(16),
childAspectRatio: 0.8,
children: List.generate(
6,
(index) => Card(
child: Center(child: Text('Product $index')),
),
),
)childAspectRatio is the tile width divided by its height. A value of 1 creates square tiles. A value below 1 creates taller tiles. A value above 1 creates wider tiles.
For more control, pass the delegate directly:
GridView(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
mainAxisExtent: 180,
),
children: const [
Card(child: Center(child: Text('A'))),
Card(child: Center(child: Text('B'))),
Card(child: Center(child: Text('C'))),
],
)mainAxisExtent sets an exact tile height for a vertically scrolling grid. When it is provided, it takes precedence over childAspectRatio.
Adaptive maximum width
GridView.extent is a convenience constructor for SliverGridDelegateWithMaxCrossAxisExtent.
GridView.extent(
maxCrossAxisExtent: 180,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
padding: const EdgeInsets.all(8),
children: List.generate(
20,
(index) => Card(
child: Center(child: Text('Tile $index')),
),
),
)On a narrow screen, this might produce two columns. On a wider screen, it might produce four. The exact result also depends on padding and spacing.
Common mistakes
- Use
crossAxisCountfor a fixed column count, not a minimum width. - Remember that spacing consumes available width. A large
crossAxisSpacingcan make tiles unexpectedly narrow. - Choose
mainAxisExtentwhen every tile needs a predictable height, such as a card with fixed content. - Use the max-extent delegate for responsive layouts, but test very narrow and very wide constraints.
- If the grid is inside a
Column, give it a bounded height withExpanded.
Exercise
Which delegate should you choose when each tile should be no wider than 160 logical pixels, but the number of columns may change with screen width?
Show answer
Use SliverGridDelegateWithMaxCrossAxisExtent with maxCrossAxisExtent: 160.