Flutter GridView
A GridView arranges widgets in a scrollable two-dimensional grid. Photo galleries, product catalogues, app launchers and calendars are all grids. Like ListView, it has several constructors, and the main question is always: how big should each tile be?
GridView.count — a fixed number of columns
GridView.count lets you say "I want N columns". Tiles share the width equally. mainAxisSpacing and crossAxisSpacing add gaps between rows and columns.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ColorGrid()));
class ColorGrid extends StatelessWidget {
const ColorGrid({super.key});
@override
Widget build(BuildContext context) {
const colors = [Colors.red, Colors.orange, Colors.amber,
Colors.green, Colors.blue, Colors.purple];
return Scaffold(
appBar: AppBar(title: const Text('Colors')),
body: GridView.count(
crossAxisCount: 3,
padding: const EdgeInsets.all(8),
mainAxisSpacing: 8,
crossAxisSpacing: 8,
children: [
for (final c in colors)
Container(color: c),
],
),
);
}
}Tiles are square by default. Change the shape with childAspectRatio (width ÷ height): 2 makes tiles twice as wide as they are tall.
GridView.builder — lazy grids
For many items use GridView.builder. It needs a grid delegate that describes the layout, plus an itemBuilder — just like ListView.builder.
class PhotoGrid extends StatelessWidget {
const PhotoGrid({super.key});
@override
Widget build(BuildContext context) {
return GridView.builder(
padding: const EdgeInsets.all(8),
itemCount: 8,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
childAspectRatio: 1.5,
),
itemBuilder: (context, index) => Card(
child: Center(child: Text('Photo ${index + 1}')),
),
);
}
}GridView.extent — a maximum tile width
Instead of a column count, you can give a maximum tile width. Flutter then fits as many columns as possible, so the grid automatically has more columns on a tablet than on a phone. The builder equivalent is SliverGridDelegateWithMaxCrossAxisExtent.
GridView.builder(
itemCount: 12,
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 90, // tiles at most 90 px wide
mainAxisSpacing: 4,
crossAxisSpacing: 4,
),
itemBuilder: (context, i) => Container(
color: Colors.indigo[100],
alignment: Alignment.center,
child: Text('${i + 1}'),
),
)On a 360 px wide phone this gives 4 columns (360 ÷ 90); on a 1024 px tablet it gives 12.
Summary
| Constructor / delegate | You specify | Columns |
|---|---|---|
GridView.count / …FixedCrossAxisCount |
crossAxisCount |
Always the same |
GridView.extent / …MaxCrossAxisExtent |
maxCrossAxisExtent |
Depends on screen width |
GridView.builder |
a delegate + itemBuilder |
Lazy building |
Tip: For uneven "masonry" layouts (Pinterest style) look at the community package
flutter_staggered_grid_view.
Note: "Main axis" is the scroll direction (vertical by default) and "cross axis" is the other one. That is why the column count is called
crossAxisCount.
Test yourself
Which property sets the number of columns in GridView.count?
- columns
- crossAxisCount
- mainAxisCount
- itemCount
childAspectRatio: 2 makes each tile…
- Twice as tall as wide
- Twice as wide as tall
- Square
- Two tiles per row
Which delegate adapts the number of columns to the screen width?
- SliverGridDelegateWithFixedCrossAxisCount
- SliverGridDelegateWithMaxCrossAxisExtent
- GridView.count
- ListView.separated