GridView
A scrolling grid with a fixed number of columns. Like
ListView
, it comes in two forms:
GridView
— the cells are listed in the markup,
GridView.builder
— the cells are built from the
@Macros id:itemBuilder
template.
Type:
multi-child
(
GridView
),
multi-child
with a template macro (
GridView.builder
)
SizedBox height:240
GridView crossAxisCount:2 mainAxisSpacing:8 crossAxisSpacing:8
ColoredBox color:#4f46e5
ColoredBox color:#7c3aed
ColoredBox color:#059669
ColoredBox color:#dc2626
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
crossAxisCount
|
int | 2 | Number of columns (rows when scrolling horizontally) |
mainAxisSpacing
|
double | 0 | Gap between rows |
crossAxisSpacing
|
double | 0 | Gap between columns |
childAspectRatio
|
double | 1.0 | Cell width ÷ height: cells are square by default |
scrollDirection
|
string | vertical |
vertical
,
horizontal
|
reverse
|
bool | false | Start from the end |
shrinkWrap
|
bool | false | Take space according to the content |
physics
|
string | always |
never
,
bouncing
,
clamp
,
always
|
clipBehavior
|
string | hardEdge | Clipping at the bounds |
The cells are the same size: the width is split evenly between columns, the height follows from
childAspectRatio
. This element does not make cells of different heights.
GridView.builder
A grid whose cells are built from the
@Macros id:itemBuilder
template — one per number from
0
to
itemCount - 1
, as you scroll. The form for grids from data.
Type:
multi-child
with a template macro
SizedBox height:300
GridView.builder crossAxisCount:3 itemCount:30 mainAxisSpacing:8 crossAxisSpacing:8
@Macros id:itemBuilder index:0
ColoredBox color:#4f46e5
Center
Text $"No. ${index}"
#style color:#ffffff
The parameters shared with
GridView
—
crossAxisCount
,
mainAxisSpacing
,
crossAxisSpacing
,
childAspectRatio
,
scrollDirection
,
reverse
,
shrinkWrap
,
physics
,
clipBehavior
and
#padding
— are described in the
Parameters
table and work the same way. Only on
GridView.builder
:
| Parameter | Type | Default | Description |
|---|---|---|---|
itemCount
|
int | — | How many cells to build |
The cell template and a grid from data are in the
Children
section; what works on the site is in the
Differences
. This element does not make cells by maximum width (
maxCrossAxisExtent
) or with a fixed height (
mainAxisExtent
) —
SliverGrid.builder
inside
CustomScrollView
has them.
Nested structures
#padding
Padding around the whole grid inside the scroll area.
SizedBox height:200
GridView crossAxisCount:3 mainAxisSpacing:4 crossAxisSpacing:4
#padding all 8
ColoredBox color:#4f46e5
ColoredBox color:#7c3aed
ColoredBox color:#059669
Children
In
GridView.builder
the child is the
@Macros id:itemBuilder
macro; the
index
parameter (
index:0
in the declaration) receives the cell number.
SizedBox height:300
GridView.builder crossAxisCount:3 itemCount:9 mainAxisSpacing:8 crossAxisSpacing:8
@Macros id:itemBuilder index:0
ColoredBox color:#4f46e5
Center
Text $"No. ${index}"
A grid from data
The macro receives only
index
; the fields are fetched by index at the call site, with
??
:
JsonData src:"data/products.json" data:@products count:@count
Center id:loading
CircularProgressIndicator
SizedBox id:ready height:400
GridView.builder crossAxisCount:2 itemCount:$count mainAxisSpacing:8 crossAxisSpacing:8
@Macros id:itemBuilder index:0
DecoratedBox
#decoration color:#36345E
#borderRadius circular 12
Center
Text {products[index]['title'] ?? ""}
Examples
16:9 tiles
SizedBox height:260
GridView crossAxisCount:2 childAspectRatio:1.78 mainAxisSpacing:8 crossAxisSpacing:8
ColoredBox color:#4f46e5
ColoredBox color:#7c3aed
ColoredBox color:#059669
ColoredBox color:#dc2626
Differences between the app and the site
On the site the grid is a CSS grid with
crossAxisCount
equal columns, and it
does not scroll by itself
: it takes space according to its content, and if the parent limits the height, the rest is clipped. So:
-
scrollDirection,reverse,physics,shrinkWrapare not read on the site, nor is#padding; -
mainAxisSpacing,crossAxisSpacing,childAspectRatio,crossAxisCountwork.
For the site, do not give the grid a fixed height if you expect scrolling inside it — let the page scroll.
Bad practices
A grid without a bounded height
// BAD — inside a Column the grid gets an infinite height: in the app this is a layout error
Column
Text "Heading"
GridView crossAxisCount:2
ColoredBox color:#4f46e5
ColoredBox color:#7c3aed
// GOOD
Column
Text "Heading"
Expanded
GridView crossAxisCount:2
ColoredBox color:#4f46e5
ColoredBox color:#7c3aed
See also
- ListView — a one-dimensional list
- AspectRatio — aspect ratio for a single element
- Wrap — wrapping elements of different widths onto a new line