Sliver Layout
Loading preview
Starting the Flutter app
CustomScrollView with collapsing SliverAppBar and tabs
import 'package:flutter/material.dart';
class SliverLayout extends StatefulWidget {
const SliverLayout({super.key});
@override
State<SliverLayout> createState() => _SliverLayoutState();
}
class _SliverLayoutState extends State<SliverLayout>
with SingleTickerProviderStateMixin {
late TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) {
return [
SliverAppBar(
expandedHeight: 200,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: const Text('Sliver Layout'),
background: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Theme.of(context).primaryColor,
Theme.of(context).primaryColor.withValues(alpha: 0.7),
],
),
),
),
),
),
SliverPersistentHeader(
pinned: true,
delegate: _SliverTabDelegate(
TabBar(
controller: _tabController,
tabs: const [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
],
),
),
),
];
},
body: TabBarView(
controller: _tabController,
children: [
_TabContent(items: ['Item A1', 'Item A2', 'Item A3', 'Item A4']),
_TabContent(items: ['Item B1', 'Item B2', 'Item B3']),
_TabContent(items: ['Item C1', 'Item C2', 'Item C3', 'Item C4', 'Item C5']),
],
),
),
);
}
}
class _SliverTabDelegate extends SliverPersistentHeaderDelegate {
final TabBar tabBar;
_SliverTabDelegate(this.tabBar);
@override
double get minExtent => tabBar.preferredSize.height;
@override
double get maxExtent => tabBar.preferredSize.height;
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return Container(
color: Theme.of(context).colorScheme.surface,
child: tabBar,
);
}
@override
bool shouldRebuild(covariant _SliverTabDelegate oldDelegate) =>
tabBar != oldDelegate.tabBar;
}
class _TabContent extends StatelessWidget {
final List<String> items;
const _TabContent({required this.items});
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) => ListTile(
title: Text(items[index]),
leading: CircleAvatar(child: Text('${index + 1}')),
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
NestedScrollView hosts a pinned, collapsing SliverAppBar and a pinned tab strip. _SliverTabDelegate adapts TabBar to SliverPersistentHeader, using its preferred height for both extents and painting a surface background so content doesn’t show through. The three _TabContent lists are inline placeholders, driven by a manually created _tabController.
Adapting it
- Replace the
Item A1style lists passed to_TabContentwith real data, keepinglength: 3in sync with the tab count. - Put an image in
FlexibleSpaceBar.backgroundin place of the gradient. - Add
SliverOverlapAbsorberinheaderSliverBuilderif tab bodies becomeCustomScrollViews.