Sub topic 16
Mixing lists and grids with CustomScrollView and slivers
This chapter assumes basic Dart syntax, Flutter widgets, Column, Expanded, and ListView or GridView. It also assumes you can run a Flutter app and use hot reload.
Why use slivers?
A ListView is a scrollable list, and a GridView is a scrollable grid. Each widget normally owns its own scrollable area. If a screen needs an app bar, a list, and a grid in one continuous scroll, use CustomScrollView.
A sliver is a scrollable section that works inside a viewport. CustomScrollView provides the viewport and combines several slivers into one scroll position.
Common conversions are:
| Regular widget | Sliver equivalent |
|---|---|
| `ListView.builder` | `SliverList` with `SliverChildBuilderDelegate` |
| `GridView.builder` | `SliverGrid` with a grid delegate |
| `Padding` around scroll content | `SliverPadding` |
Combine a list and a grid
The following widget creates one scrollable page. The app bar can float into view, three featured rows come next, and twenty products appear in a two-column grid.
import 'package:flutter/material.dart';
class ShopPage extends StatelessWidget {
const ShopPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
const SliverAppBar(
title: Text('Shop'),
floating: true,
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return ListTile(
leading: const Icon(Icons.star),
title: Text('Featured item $index'),
);
},
childCount: 3,
),
),
SliverPadding(
padding: const EdgeInsets.all(8),
sliver: SliverGrid(
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
childAspectRatio: 1,
),
delegate: SliverChildBuilderDelegate(
(context, index) {
return Card(
child: Center(child: Text('Product $index')),
);
},
childCount: 20,
),
),
),
],
),
);
}
}SliverChildBuilderDelegate builds children lazily. Flutter creates only the children needed near the visible area, which keeps long collections more efficient than constructing every child at once.
A SliverGrid still needs a grid delegate. SliverGridDelegateWithFixedCrossAxisCount fixes the number of columns. SliverGridDelegateWithMaxCrossAxisExtent instead sets the largest tile width and lets Flutter choose how many columns fit.
Constraints and nesting
Do not place a vertical CustomScrollView inside another vertical scrollable unless you have a specific reason and understand the constraints. Two scrollables competing for the same gesture and height often produce poor scrolling or layout errors.
If a CustomScrollView is inside a Column, give it a bounded height with Expanded, just as you would with ListView:
Column(
children: [
const Text('Catalog'),
Expanded(child: ShopPage()),
],
)Use SliverToBoxAdapter when a normal, non-sliver widget must appear among slivers. Keep such sections small; repeated rows belong in SliverList.
Exercise
Which sliver should replace GridView.builder when combining a grid with a list in one CustomScrollView?
Show answer
Use SliverGrid with a grid delegate and a SliverChildBuilderDelegate.