Flutter Slivers
Creating advanced scrollable effects
📜 What are Slivers?
Slivers are scrollable areas in Flutter that can change appearance while scrolling. They enable advanced effects like collapsing headers, floating app bars, and custom scroll behaviors for beautiful, dynamic user interfaces.
// Simple Sliver example
CustomScrollView(
slivers: [
SliverAppBar(title: Text('Header')),
SliverList(delegate: SliverChildListDelegate([])),
],
)
Output:
Key Sliver Concepts
SliverAppBar
Collapsing app bar header
SliverAppBar(
expandedHeight: 200,
floating: true,
)
SliverList
Scrollable list of items
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(),
),
)
SliverGrid
Scrollable grid layout
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
)
CustomScrollView
Container for slivers
CustomScrollView(
slivers: [
// Add slivers here
],
)
🔹 Basic SliverAppBar
Create a collapsing app bar with SliverAppBar:
import 'package:flutter/material.dart';
class SliverExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text('Sliver AppBar'),
background: Image.network(
'https://picsum.photos/400/200',
fit: BoxFit.cover,
),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(
title: Text('Item $index'),
),
childCount: 20,
),
),
],
),
);
}
}
Output:
🔹 SliverList Example
Create a scrollable list using SliverList:
CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
height: 60,
color: index.isOdd ? Colors.white : Colors.grey[200],
child: Center(
child: Text('Item $index'),
),
);
},
childCount: 10,
),
),
],
)
Output:
🔹 SliverGrid Example
Create a scrollable grid using SliverGrid:
CustomScrollView(
slivers: [
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
childAspectRatio: 1.0,
),
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
color: Colors.primaries[index % Colors.primaries.length],
child: Center(
child: Text(
'Grid $index',
style: TextStyle(color: Colors.white),
),
),
);
},
childCount: 12,
),
),
],
)
Output:
🔹 Floating SliverAppBar
Create a floating app bar that appears when scrolling up:
CustomScrollView(
slivers: [
SliverAppBar(
floating: true,
snap: true,
title: Text('Floating AppBar'),
backgroundColor: Colors.teal,
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(
leading: Icon(Icons.star),
title: Text('Item $index'),
),
childCount: 30,
),
),
],
)
Output:
🔹 Multiple Slivers Combined
Combine different sliver types in one scroll view:
CustomScrollView(
slivers: [
SliverAppBar(
title: Text('My App'),
pinned: true,
),
SliverToBoxAdapter(
child: Container(
height: 100,
color: Colors.amber,
child: Center(child: Text('Fixed Header')),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(title: Text('List Item $index')),
childCount: 5,
),
),
],
)