Sub topic 7
Grids and Lists That Adjust Their Column Count
Assume you know Dart, widget trees, Row, Column, Scaffold, and ListView. This chapter focuses on choosing a grid structure from the width available to the grid, rather than from a fixed device type.
A responsive grid changes its geometry as space changes. An adaptive grid can also change its interaction pattern, such as switching from one column to several columns. In Flutter, both behaviors usually begin with constraints: the parent tells the grid how much space it may use.
Choose a grid delegate
GridView.builder creates grid children lazily. Lazy creation means Flutter builds only the items needed for the visible part of a long grid.
The grid delegate controls tile placement. Use SliverGridDelegateWithMaxCrossAxisExtent when each tile should stay below a preferred maximum width. Flutter then calculates how many columns fit.
| Delegate | Best when |
|---|---|
| `SliverGridDelegateWithMaxCrossAxisExtent` | Tile width should stay below a maximum |
| `SliverGridDelegateWithFixedCrossAxisCount` | The design requires an exact column count |
This example keeps each tile at most 200 logical pixels wide:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: PhotoGrid()));
}
class PhotoGrid extends StatelessWidget {
const PhotoGrid({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Adaptive grid')),
body: GridView.builder(
padding: const EdgeInsets.all(8),
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
childAspectRatio: 1.2,
),
itemCount: 40,
itemBuilder: (context, index) {
return Card(
child: Center(child: Text('Item $index')),
);
},
),
);
}
}On a narrow window, this grid may have one or two columns. On a wider window, it may have several. The tile limit stays the same, so content does not become unnecessarily wide.
Use breakpoints when behavior must change
A maximum tile width is enough when only the number of columns changes. Use a window-size class when the layout itself must change. For example, a compact layout might use a list, while an expanded layout uses a grid.
enum WindowSize { compact, medium, expanded }
WindowSize windowSizeFor(double width) {
if (width < 600) return WindowSize.compact;
if (width < 840) return WindowSize.medium;
return WindowSize.expanded;
}Use LayoutBuilder when the decision belongs to a particular region, such as a card or pane. Use MediaQuery.sizeOf(context) when the decision belongs to the whole application window.
Lists need the same discipline
For a list, prefer ListView.builder instead of creating every child at once. Keep row contents flexible with Expanded, and avoid hard-coded widths that fail when text is scaled or the window narrows.
A list can also switch layouts at a breakpoint:
Widget catalog(double width) {
final size = windowSizeFor(width);
return switch (size) {
WindowSize.compact => ListView.builder(
itemCount: 20,
itemBuilder: (context, index) => ListTile(
title: Text('Product $index'),
),
),
WindowSize.medium || WindowSize.expanded => GridView.builder(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 240,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
),
itemCount: 20,
itemBuilder: (context, index) => Card(
child: Center(child: Text('Product $index')),
),
),
};
}Exercise
Which delegate should you choose when tiles should never exceed 200 logical pixels in width?
Show answer
Use SliverGridDelegateWithMaxCrossAxisExtent with maxCrossAxisExtent: 200.