Sub topic 7
Quick grids with GridView.count and GridView.extent
Assume you know basic Dart lists and closures, Flutter build methods, and layout widgets such as Column and Expanded. This chapter focuses on choosing a grid layout, not on loading data from a server.
A grid is a scrollable collection arranged in rows and columns. GridView provides the scrolling viewport. Its grid delegate decides how wide each tile is and how many tiles fit across the available space.
Choose a fixed count or a maximum extent
Use GridView.count when you want an exact number of columns. Use GridView.extent when you want to set a maximum tile width and let Flutter calculate the column count from the available width.
| Constructor | Main decision | Useful when |
|---|---|---|
| `GridView.count` | Exact column count | The design specifies two, three, or four columns |
| `GridView.extent` | Maximum tile width | The layout should adapt to different screen widths |
Both constructors accept an explicit children list. This is convenient for a small, known collection. For a large or dynamic collection, use GridView.builder, which creates items lazily.
Build a fixed-column grid
This example creates three columns. mainAxisSpacing adds space between rows, and crossAxisSpacing adds space between columns. For a vertical grid, the main axis is vertical and the cross axis is horizontal.
GridView.count(
padding: const EdgeInsets.all(16),
crossAxisCount: 3,
mainAxisSpacing: 12,
crossAxisSpacing: 12,
childAspectRatio: 1,
children: const [
Card(child: Center(child: Text('One'))),
Card(child: Center(child: Text('Two'))),
Card(child: Center(child: Text('Three'))),
Card(child: Center(child: Text('Four'))),
Card(child: Center(child: Text('Five'))),
Card(child: Center(child: Text('Six'))),
],
)childAspectRatio is the width divided by the height of each tile. A value of 1 makes square tiles. A value of 2 makes tiles twice as wide as they are tall.
The children list is laid out in order. In a vertical grid, Flutter fills the first row from left to right, then continues on the next row.
Let the screen width choose the columns
GridView.extent uses a maximum cross-axis extent. The following grid tries to keep each tile at or below 160 logical pixels wide:
GridView.extent(
padding: const EdgeInsets.all(8),
maxCrossAxisExtent: 160,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
childAspectRatio: 1,
children: List.generate(
12,
(index) => Card(
child: Center(child: Text('Tile $index')),
),
),
)On a narrow phone, this may produce two columns. On a wider tablet, it may produce four or more. This avoids hard-coding a separate column count for every screen size.
Common layout mistakes
- Setting
crossAxisCountto zero or a negative value causes an error. - Large spacing values reduce the space available for tiles.
- A long
childrenlist builds every child immediately. PreferGridView.builderfor large collections. - A grid nested inside another vertical scrollable can produce constraint or scrolling problems. Avoid nesting, or use slivers when one scroll view must contain several sections.
Exercise
You need three columns on phones but want the number of columns to adapt on tablets. Which constructor should you start with, and what trade-off does it involve?
Show answer
Start with GridView.extent and choose a maximum tile width. The exact column count will vary with screen width, so the design must tolerate that change.