Question #348HardWidgets & UI

What are Slivers?

#widget#slivers#scrolling

Answer

Overview

Slivers are special scrollable components in Flutter that can be combined inside a

text
CustomScrollView
to create complex, coordinated scrolling effects — like collapsing app bars, mixed scroll content, and sticky headers.

The word "sliver" literally means "a thin piece" — each sliver is a piece of the scrollable area.


Why Slivers?

Normal scrolling with

text
ListView
is rigid — it's one scrollable area. Slivers let you:

  • Combine multiple scrollable sections
  • Create collapsible app bars
  • Create sticky headers
  • Mix grids and lists in one scroll view

CustomScrollView + Slivers

dart
CustomScrollView(
  slivers: [
    // Collapsing app bar
    SliverAppBar(
      title: Text('My App'),
      expandedHeight: 200,
      pinned: true,
      flexibleSpace: FlexibleSpaceBar(
        background: Image.network(url, fit: BoxFit.cover),
      ),
    ),

    // Grid section
    SliverGrid(
      delegate: SliverChildBuilderDelegate(
        (context, index) => Card(child: Center(child: Text('Grid $index'))),
        childCount: 6,
      ),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
    ),

    // List section
    SliverList(
      delegate: SliverChildBuilderDelegate(
        (context, index) => ListTile(title: Text('List Item $index')),
        childCount: 20,
      ),
    ),
  ],
)

Common Sliver Widgets

WidgetDescription
text
SliverAppBar
Collapsible/floating app bar
text
SliverList
Lazy list (replaces ListView)
text
SliverGrid
Lazy grid (replaces GridView)
text
SliverPersistentHeader
Custom sticky header
text
SliverToBoxAdapter
Wrap any normal widget in a sliver
text
SliverFillRemaining
Fill remaining scroll area
text
SliverPadding
Add padding around a sliver
text
SliverFixedExtentList
Performance list with fixed item height
text
SliverAnimatedList
Animated sliver list

SliverToBoxAdapter (Wrap Any Widget)

dart
CustomScrollView(
  slivers: [
    SliverAppBar(title: Text('App')),
    SliverToBoxAdapter(
      child: Padding(
        padding: EdgeInsets.all(16),
        child: Text('Featured Section', style: TextStyle(fontSize: 20)),
      ),
    ),
    SliverList(
      delegate: SliverChildBuilderDelegate(
        (context, index) => ListTile(title: Text('Item $index')),
        childCount: 10,
      ),
    ),
    SliverFillRemaining(
      child: Center(child: Text('End of list')),
    ),
  ],
)

Normal Widgets vs Sliver Equivalents

Normal WidgetSliver Equivalent
text
ListView
text
SliverList
text
GridView
text
SliverGrid
text
AppBar
text
SliverAppBar
Any widget
text
SliverToBoxAdapter

Key Rule: Slivers only work inside

text
CustomScrollView
. Outside of it, use normal widgets. Slivers are powerful but have more complexity — use them only when you need coordinated scrolling effects.