Sub topic 12
Collapsing app bars with SliverAppBar
This chapter assumes that you know basic Dart syntax, StatelessWidget, and the build method. It also assumes that you can create and run a Flutter project. You should already understand that Scaffold provides screen layout slots such as body and appBar.
A regular AppBar stays in the Scaffold.appBar slot. A SliverAppBar is different: it is a sliver, which is a scrollable piece of a larger scroll view. Place it inside a CustomScrollView to make the app bar react to scrolling.
Put SliverAppBar inside CustomScrollView
The Scaffold still provides the screen structure, but its body contains the scroll view. The CustomScrollView.slivers list can contain a SliverAppBar followed by scrollable content such as SliverList.
import 'package:flutter/material.dart';
void main() {
runApp(const DemoApp());
}
class DemoApp extends StatelessWidget {
const DemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sliver App Bar Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
useMaterial3: true,
),
home: const FeedPage(),
);
}
}
class FeedPage extends StatelessWidget {
const FeedPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
title: const Text('My Feed'),
expandedHeight: 220,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
background: ColoredBox(
color: Theme.of(context).colorScheme.primaryContainer,
child: const Center(
child: Icon(Icons.landscape, size: 80),
),
),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return ListTile(
leading: CircleAvatar(child: Text('${index + 1}')),
title: Text('Feed item ${index + 1}'),
subtitle: const Text('Scroll to collapse the app bar'),
);
},
childCount: 30,
),
),
],
),
);
}
}At the start, the app bar is 220 logical pixels high. As the list moves upward, the flexible area shrinks. Because pinned is true, the toolbar remains at the top after the flexible area collapses.
The FlexibleSpaceBar manages the title and flexible background. Its title can scale or move during the collapse. A plain flexibleSpace widget gives you more control but requires you to manage its layout yourself.
Choose the scrolling behavior
Several SliverAppBar properties control when the bar is visible:
| Property | Effect |
|---|---|
| `pinned` | Keeps the toolbar visible after the app bar collapses |
| `floating` | Reveals the app bar as soon as the user scrolls back toward the top |
| `snap` | Animates a floating app bar fully open or closed |
| `expandedHeight` | Sets the initial height before the flexible area collapses |
floating is useful for a feed where users need quick access to search or actions. snap can be used only when floating is also true. pinned and floating can both be true when you want the toolbar to remain available and also reappear quickly.
Understand common layout errors
The most common error is placing a normal box widget directly in CustomScrollView.slivers. Widgets such as Container and ListView are not slivers. Use sliver equivalents such as SliverToBoxAdapter, SliverList, and SliverGrid.
Use SliverToBoxAdapter for one ordinary widget:
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.all(16),
child: Text('A section above the list'),
),
)A SliverAppBar also behaves differently from a normal app bar when the content is short. If the list does not provide enough scroll extent, there may be no meaningful collapse. Add enough content or use SliverFillRemaining for a page that fills the available space.
Visualize the widget structure
When deciding between AppBar and SliverAppBar, use a regular AppBar for a fixed top bar. Use SliverAppBar when the top area should expand, collapse, float, or coordinate with scrolling content.
Exercise
What change makes the toolbar stay visible after the flexible area collapses?
Show answer
Set pinned: true on the SliverAppBar.