Ask the assistant
0/400
Your question is processed by an AI service and kept to improve answers. Do not enter personal data.

    Slivers and CustomScrollView

    CustomScrollView is a scroller that builds a page out of slivers : pieces of a scrollable area, each with its own scrolling behaviour — a header that collapses or sticks, a lazy list, a grid, a column of limited width, space "to the end of the screen". Use it when one scrollable page needs more than a single list: a header above a catalog, several sections, a sticky side column.

    A sliver is an element that can only live directly inside a scroller ( CustomScrollView , a sliver group or a sliver wrapper). Every element on this page except CustomScrollView and FlexibleSpaceBar is a sliver.

    SizedBox height:400
      CustomScrollView
        SliverAppBar pinned:true backgroundColor:#4f46e5 foregroundColor:#ffffff
          Text id:title "Catalog"
        SliverList.builder itemCount:30
          @Macros id:itemBuilder index:0
            Padding
              #padding all 12
              Text $"Item ${index}"

    How slivers nest

    • A regular element ( Text , Padding , Container …) placed directly in CustomScrollView , SliverMainAxisGroup or SliverCrossAxisGroup is wrapped in SliverToBoxAdapter automatically.
    • Sliver wrappers — SliverPadding , SliverConstrainedBox , SliverSafeArea , SliverVisibility , SliverOffstage , SliverOpacity , SliverLayoutBuilder — wrap nothing themselves: their child must be a sliver ( SliverList , SliverToBoxAdapter , SliverMainAxisGroup …). A regular element under them is a layout error in the app.
    • Don't put regular wrappers around a sliver. Padding , Visibility , Opacity , Center around a SliverList is a box with a sliver inside it: a layout error. Each such wrapper has a sliver twin: SliverPadding , SliverVisibility , SliverOpacity , SliverConstrainedBox .
    • Inside a sliver container ( SliverList , SliverGrid , SliverToBoxAdapter , SliverFillRemaining , headers) the children are regular elements.

    Live example: the header sticks to the top, with a grid and a list below.

    SizedBox height:420
      CustomScrollView
        SliverAppBar pinned:true backgroundColor:#4f46e5 foregroundColor:#ffffff
          Text id:title "Catalog"
        SliverPadding
          #padding all 12
          SliverGrid crossAxisCount:3 mainAxisSpacing:8 crossAxisSpacing:8
            ColoredBox color:#7c3aed
              Center
                Text "1"
                  #style color:#ffffff
            ColoredBox color:#059669
              Center
                Text "2"
                  #style color:#ffffff
            ColoredBox color:#dc2626
              Center
                Text "3"
                  #style color:#ffffff
        SliverList.builder itemCount:20
          @Macros id:itemBuilder index:0
            Padding
              #padding symmetric horizontal:16 vertical:10
              Text $"Row ${index + 1}"
          @Macros id:separatorBuilder index:0
            Divider height:1

    CustomScrollView

    A scroller made of slivers. Children follow one another along the scroll axis; a regular element among them is wrapped in SliverToBoxAdapter automatically.

    Type: multi-child

    SizedBox height:300
      CustomScrollView
        SizedBox height:200
          ColoredBox color:#4f46e5
        SizedBox height:200
          ColoredBox color:#7c3aed
    Parameter Type Default Description
    scrollDirection string vertical vertical , horizontal
    reverse bool false Start from the end: the first sliver is at the bottom (right)
    shrinkWrap bool false Take only as much space as the content needs, not all that is available
    physics string — Response to gestures: never , bouncing , clamp , always . never — the page does not move at all: this is how you lock it under a full-screen overlay (splash, menu, modal) so the user can't scroll the content beneath it
    clipBehavior string hardEdge Clipping at the bounds
    keyboardDismissBehavior string manual onDrag — hide the on-screen keyboard as soon as the content is dragged

    Like any scroller, it needs a bounded height (width for horizontal scrolling): inside a Column — via Expanded , otherwise via SizedBox .

    CustomScrollView is always attached to the primary scroll controller: Scrollbar with an always-visible thumb and ScrollData both work over it.

    SliverAppBar

    The page header inside a CustomScrollView : it can be taller than a regular one when expanded, collapse on scroll, stick to the top or come back when scrolling backwards.

    Type: multi-child (children by id: )

    SizedBox height:400
      CustomScrollView
        SliverAppBar pinned:true expandedHeight:160 backgroundColor:#4f46e5 foregroundColor:#ffffff
          Text id:title "Profile"
        SliverList
          Text "Content"

    Children are placed by id: : id:title — the title, id:leading — the element on the left, id:action — buttons on the right (any number), id:flexibleSpace — the background of the expanded header (usually `FlexibleSpaceBar` ).

    Parameter Type Default Description
    expandedHeight double — Height of the expanded header
    collapsedHeight double — Height of the collapsed header; no less than toolbarHeight
    toolbarHeight double 56 Height of the row with the title and buttons
    pinned bool false The collapsed header stays at the top edge
    floating bool false The header returns as soon as the content is pulled back, without waiting for the top of the page
    snap bool false A returning header slides in fully instead of following the finger. Only together with floating:true
    elevation double — Shadow under the header
    backgroundColor color — Background (from the theme by default)
    foregroundColor color — Color of the title and icons
    centerTitle bool — Center the title
    automaticallyImplyLeading bool true Show a "back" button automatically if id:leading is not set and there is somewhere to go back to
    // A header that comes back when scrolling backwards
    @n:0
    SizedBox height:400
      CustomScrollView
        SliverAppBar floating:true snap:true backgroundColor:#4f46e5 foregroundColor:#ffffff
          Text id:title "Feed"
          IconButton id:action tooltip:"Refresh" onPressed:(){n = n + 1}
            Icon refresh
        SliverList.builder itemCount:40
          @Macros id:itemBuilder index:0
            Text $"Post ${index}"

    FlexibleSpaceBar

    The content of an expanded SliverAppBar : a background and a title that shrink together with the header. Placed in the header with id:flexibleSpace .

    Type: multi-child (children by id: )

    SizedBox height:400
      CustomScrollView
        SliverAppBar pinned:true expandedHeight:200 backgroundColor:#4f46e5
          FlexibleSpaceBar id:flexibleSpace centerTitle:true
            ColoredBox id:background color:#7c3aed
            Text id:title "Title"
              #style color:#ffffff
        SliverList
          Text "Content"

    Children: id:background — a background filling the whole header, id:title — a title that shrinks and moves into the header row as it collapses.

    Parameter Type Default Description
    centerTitle bool — Center the title
    collapseMode string parallax How the background leaves when collapsing: parallax — slower than the content, pin — stays in place until the header covers it, none — moves away together with it

    SliverToBoxAdapter

    A regular element acting as a sliver. In CustomScrollView , SliverMainAxisGroup and SliverCrossAxisGroup it is added automatically; write it explicitly where the child must be a sliver (under SliverPadding , SliverVisibility …) and as a column of SliverCrossAxisGroup .

    Type: single-child

    SizedBox height:300
      CustomScrollView
        SliverPadding
          #padding all 16
          SliverToBoxAdapter
            Text "Regular text with padding"

    No parameters (other than id: and those common to all elements).

    SliverList

    A list whose children are listed in the markup. All children are built at once; for long lists and data, use `SliverList.builder` .

    Type: multi-child

    SizedBox height:300
      CustomScrollView
        SliverList
          Text "First"
          Text "Second"
          Text "Third"

    No parameters: rows take the width of the scroller and their own height.

    SliverList.builder

    A lazy list inside a CustomScrollView : rows are built from the @Macros id:itemBuilder template as scrolling reaches them. It replaces a ListView.builder shrinkWrap:true nested in a scroller: shrinkWrap measures all rows at once, and a long catalog stops being lazy.

    Type: multi-child with a template macro

    SizedBox height:300
      CustomScrollView
        SliverList.builder itemCount:50
          @Macros id:itemBuilder index:0
            Padding
              #padding all 12
              Text $"Row ${index}"
    Parameter Type Default Description
    itemCount int — How many rows to build. Required : without it the list is not built at all — a scroller has no end, and without a number the list would never finish
    itemExtent double — The same height for every row: the list is laid out without measuring rows. Ignored together with id:separatorBuilder

    The template is @Macros id:itemBuilder index:0 ; the index parameter receives the row number. A second macro, id:separatorBuilder , is placed between rows ( itemCount - 1 times). Rows from data work the same way as in ListView.builder : the macro receives only index , and fields are read at the call site, with ?? .

    JsonData src:"data/products.json" data:@products count:@count
      Center id:loading
        CircularProgressIndicator
      SizedBox id:ready height:400
        CustomScrollView
          SliverAppBar pinned:true
            Text id:title "Products"
          SliverList.builder itemCount:$count
            @Macros id:itemBuilder index:0
              Padding
                #padding all 12
                Text {products[index]['title'] ?? ""}
            @Macros id:separatorBuilder index:0
              Divider height:1

    SliverGrid

    A grid whose cells are listed in the markup: crossAxisCount equal columns, cell height from childAspectRatio .

    Type: multi-child

    SizedBox height:300
      CustomScrollView
        SliverGrid crossAxisCount:2 mainAxisSpacing:8 crossAxisSpacing:8
          ColoredBox color:#4f46e5
          ColoredBox color:#7c3aed
          ColoredBox color:#059669
    Parameter Type Default Description
    crossAxisCount int 2 Number of columns (at least 1)
    mainAxisSpacing double 0 Gap between rows
    crossAxisSpacing double 0 Gap between columns
    childAspectRatio double 1.0 Cell width ÷ height

    SliverGrid.builder

    A lazy grid: cells are built from the @Macros id:itemBuilder template as you scroll. Columns are set by a count ( crossAxisCount ) or by the maximum cell width ( maxCrossAxisExtent ) — then the number of columns follows the window width.

    Type: multi-child with a template macro

    SizedBox height:400
      CustomScrollView
        SliverGrid.builder itemCount:12 maxCrossAxisExtent:240 mainAxisExtent:120 mainAxisSpacing:12 crossAxisSpacing:12
          @Macros id:itemBuilder index:0
            ColoredBox color:#4f46e5
              Center
                Text $"Card ${index}"
                  #style color:#ffffff
    Parameter Type Default Description
    itemCount int — How many cells to build. Required , as with SliverList.builder
    crossAxisCount int 2 Number of columns; ignored if maxCrossAxisExtent is set
    maxCrossAxisExtent double — Maximum cell width; the number of columns is computed from the width
    mainAxisExtent double — Cell height in pixels; replaces childAspectRatio
    mainAxisSpacing double 0 Gap between rows
    crossAxisSpacing double 0 Gap between columns
    childAspectRatio double 1.0 Cell width ÷ height

    For a grid of cards , set mainAxisExtent , not childAspectRatio : with maxCrossAxisExtent the column width changes almost twofold as the window resizes, and with an aspect ratio the height changes with it — a card that used to fit gets clipped. A height in pixels stays put while the number of columns follows the window.

    SliverPadding

    Padding around a sliver — the sliver twin of Padding .

    Type: wrapper (the child is a sliver)

    SizedBox height:300
      CustomScrollView
        SliverPadding
          #padding symmetric horizontal:24 vertical:12
          SliverList
            Text "Row 1"
            Text "Row 2"
    Parameter Type Default Description
    #padding EdgeInsets 0 Padding: all , symmetric , only — as in Padding

    SliverConstrainedBox

    A content column of limited width inside a scroller: it limits the sliver's width and places it in the middle (or at an edge). The whole width of the window still scrolls — the wheel and the scrollbar work on the margins too.

    Type: wrapper (the child is a sliver)

    SizedBox height:300
      CustomScrollView
        SliverConstrainedBox
          #constraints maxWidth:640
          SliverList
            Text "Text in a column no wider than 640"
    Parameter Type Default Description
    #constraints BoxConstraints — Only maxWidth is read (with horizontal scrolling — the extent across the axis). maxHeight , minWidth and the rest have no effect: a sliver's length is the length of its content
    alignment string center Where the column sits when the window is wider: center , start , end

    A Center > ConstrainedBox wrapper around the whole CustomScrollView looks the same but narrows the scroller itself: on a wide window the margins no longer scroll with the wheel. SliverConstrainedBox narrows only the content.

    SliverFillRemaining

    A box that takes all the space left in the scroll viewport after the slivers before it. Useful for an empty state or a loading indicator centered in the remaining space under a short list.

    Type: single-child

    SizedBox height:400
      CustomScrollView
        SliverToBoxAdapter
          Text "Nothing found"
        SliverFillRemaining
          Center
            Text "Try a different query"
    Parameter Type Default Description
    hasScrollBody bool false Whether the child scrolls by itself. Defaults to false — the opposite of Flutter: the child gets exactly the remaining space
    fillOverscroll bool false Also stretch into the overscroll area beyond the edge

    SliverMainAxisGroup

    Several slivers in a row acting as one sliver — what Column is for boxes. It lets you treat a section (a heading and its list) as a single whole: hide it with one SliverVisibility , make it a column of SliverCrossAxisGroup .

    Type: multi-child (children are slivers; a regular element is wrapped automatically)

    @open:true
    SizedBox height:300
      CustomScrollView
        SliverVisibility visible:$open
          SliverMainAxisGroup
            Text "Section"
            SliverList
              Text "Row 1"
              Text "Row 2"

    No parameters.

    SliverCrossAxisGroup

    Several slivers side by side across the scroll axis, under one shared page scroll. This is how you make a sticky side column next to a scrolling body: in Flutter only a header sliver can stick, and it sticks across the full width — next to another column only inside this group.

    Type: multi-child (children are columns)

    SizedBox height:400
      CustomScrollView
        SliverCrossAxisGroup
          SliverMainAxisGroup id:"column" crossAxisExtent:200
            PinnedHeaderSliver
              ColoredBox color:#eef2ff
                Padding
                  #padding all 12
                  Text "Menu"
          SliverMainAxisGroup id:"column" crossAxisFlex:1
            SliverList.builder itemCount:40
              @Macros id:itemBuilder index:0
                Text $"Paragraph ${index}"

    The column sets its own width — with a parameter on the child with id:"column" :

    Child parameter Type Default Description
    crossAxisExtent double — Fixed column width
    crossAxisFlex int 1 Share of the remaining width if crossAxisExtent is not set

    SliverPersistentHeader

    A header that shrinks from maxExtent to minExtent as you scroll; with pinned:true the collapsed header stays at the edge.

    Type: single-child

    SizedBox height:400
      CustomScrollView
        SliverPersistentHeader pinned:true minExtent:48 maxExtent:140
          ColoredBox color:#4f46e5
            Center
              Text "Header"
                #style color:#ffffff
        SliverList.builder itemCount:40
          @Macros id:itemBuilder index:0
            Text $"Row ${index}"
    Parameter Type Default Description
    maxExtent double 56 Height of the expanded header
    minExtent double = maxExtent Height of the collapsed header; never greater than maxExtent
    pinned bool false The collapsed header stays at the edge
    floating bool false The header returns as soon as the content is pulled back

    The child is the same at any degree of collapse — it simply receives the current height. There is no separate "collapsed" look. A header that only needs to stick needs no numbers — that is `PinnedHeaderSliver` .

    PinnedHeaderSliver

    A header that stays at the top edge while the content scrolls under it. Takes its height from the child.

    Type: single-child

    SizedBox height:400
      CustomScrollView
        PinnedHeaderSliver
          ColoredBox color:#ffffff
            Padding
              #padding symmetric horizontal:16 vertical:12
              Text "Search"
        SliverList.builder itemCount:50
          @Macros id:itemBuilder index:0
            Text $"Row ${index}"

    No parameters.

    • Give the child an opaque background : the header is drawn over the content, and the content shows through a transparent one.
    • The child gets a tight width across the whole viewport, so a ConstrainedBox maxWidth: directly under the header does nothing — put a Center first, and the ConstrainedBox inside it.

    SliverSafeArea

    Keeps a sliver clear of screen cutouts: the notch, the home indicator, the status bar — the sliver twin of SafeArea .

    Type: wrapper (the child is a sliver)

    SizedBox height:300
      CustomScrollView
        SliverSafeArea top:false
          #minimum all 8
          SliverList
            Text "Not under the system bar"
    Parameter Type Default Description
    top bool true Avoid the cutout at the top
    bottom bool true Avoid at the bottom
    left bool true Avoid on the left
    right bool true Avoid on the right
    #minimum EdgeInsets 0 The minimum padding — applies even where there is no cutout and on a disabled side

    SliverVisibility

    Shows or hides a sliver — the sliver twin of Visibility . A regular Visibility around a sliver is a layout error (see How slivers nest ).

    Type: wrapper (the child is a sliver)

    @show:true
    Column
      ElevatedButton onPressed:{show = !show}
        Text "Show/hide"
      Expanded
        CustomScrollView
          SliverVisibility visible:$show
            SliverList
              Text "A section that can be hidden"
          SliverList
            Text "Always visible"
    Parameter Type Default Description
    visible bool true Whether the sliver is shown
    maintainSize bool false A hidden sliver keeps its length (empty space remains)
    maintainAnimation bool = maintainSize Animations of the hidden subtree keep running
    maintainState bool = maintainSize The state of the hidden subtree is kept
    maintainInteractivity bool false A hidden sliver keeps receiving taps (only with maintainSize )
    maintainSemantics bool false A hidden sliver stays visible to the screen reader (only with maintainSize )

    SliverOffstage

    Lays out a sliver but neither paints it nor gives it space.

    Type: wrapper (the child is a sliver)

    SizedBox height:200
      CustomScrollView
        SliverOffstage offstage:true
          SliverList
            Text "Not visible"
        SliverList
          Text "Visible"
    Parameter Type Default Description
    offstage bool true true — the sliver is removed from the page

    SliverOpacity

    Sliver opacity without changing the layout — the sliver twin of Opacity .

    Type: wrapper (the child is a sliver)

    SizedBox height:200
      CustomScrollView
        SliverOpacity opacity:0.5
          SliverList
            Text "Semi-transparent section"
    Parameter Type Default Description
    opacity double 1.0 From 0 (invisible) to 1 (opaque); values outside the range are clamped to it
    alwaysIncludeSemantics bool false Keep the section visible to the screen reader even at opacity:0

    SliverLayoutBuilder

    The sliver twin of LayoutBuilder : publishes into @ variables the size of the scroll viewport and how far scrolling has gone into this sliver. The subtree uses them to choose its look — for example, a narrow column on a phone and a side column on a wide window.

    Type: wrapper (the child is a sliver)

    SizedBox height:400
      CustomScrollView
        SliverLayoutBuilder crossAxisExtent:@w
          SliverMainAxisGroup
            SliverToBoxAdapter if:{w < 700}
              Text "Narrow window: one column"
            SliverCrossAxisGroup if:{w >= 700}
              SliverToBoxAdapter id:"column" crossAxisExtent:240
                Text "Side column"
              SliverToBoxAdapter id:"column" crossAxisFlex:1
                Text "Main column"
    Parameter Type Default Description
    crossAxisExtent @ variable — Receives the width of the scroll viewport (across the axis)
    viewportMainAxisExtent @ variable — Receives the height of the scroll viewport (along the axis)
    scrollOffset @ variable — Receives how many pixels scrolling has gone into this sliver

    An if: condition on a published variable is re-evaluated when the size changes, unlike an if: on $theme.sizeClass , which is evaluated once at build time.

    Differences between the app and the site

    On the site, CustomScrollView is a scrollable block, and slivers are blocks inside it. There is no "on scroll" behaviour (collapsing, header return, lazy building): the page is delivered whole, fully expanded.

    Element On the site
    CustomScrollView scrollDirection and reverse work. physics:never locks scrolling of the whole document. shrinkWrap , clipBehavior , keyboardDismissBehavior and the other physics values are ignored
    SliverAppBar A row of id:leading / id:title / id:action with the id:flexibleSpace background behind it. pinned — sticks to the top. expandedHeight — the header height (no collapsing); without it — 56. Without backgroundColor the background is #6200ee , without foregroundColor the text is white. floating , snap , elevation , centerTitle , toolbarHeight , collapsedHeight , automaticallyImplyLeading are ignored
    FlexibleSpaceBar id:background fills the whole header, id:title sits at the bottom with a 16 padding; centerTitle works, collapseMode is ignored
    SliverToBoxAdapter Only the child is drawn
    SliverList , SliverList.builder A column of rows; id:separatorBuilder separators work; all .builder rows go into the page at once; itemExtent is ignored
    SliverGrid A grid: crossAxisCount , both gaps and childAspectRatio work
    SliverGrid.builder Like SliverGrid . maxCrossAxisExtent and mainAxisExtent are ignored : the grid comes out with crossAxisCount columns (2 by default) and square cells
    SliverPadding , SliverSafeArea Work; screen cutouts are reported by the browser (there are none on a computer), #minimum applies
    SliverConstrainedBox maxWidth and alignment work
    SliverFillRemaining Grows into the remaining space; hasScrollBody and fillOverscroll are ignored
    SliverMainAxisGroup Children go straight into the common flow; the group has no box of its own
    SliverCrossAxisGroup Columns side by side; crossAxisExtent and crossAxisFlex work, a sticky header in a column sticks
    PinnedHeaderSliver Sticks to the leading edge of the scroll
    SliverPersistentHeader Always maxExtent tall (does not shrink); pinned — sticks; floating is ignored
    SliverVisibility visible works, including from a variable; maintain… are ignored
    SliverOffstage offstage:true — the block is hidden
    SliverOpacity opacity works; alwaysIncludeSemantics is ignored
    SliverLayoutBuilder An if: on crossAxisExtent is re-evaluated live as the width changes; viewportMainAxisExtent and scrollOffset are updated too. On the site crossAxisExtent is always the width — even with horizontal scrolling

    Bad practices

    A regular wrapper around a sliver

    // BAD — Padding is a box with a sliver inside: a layout error in the app
    SizedBox height:300
      CustomScrollView
        Padding
          #padding all 16
          SliverList
            Text "Row"
    // GOOD — the sliver twin
    SizedBox height:300
      CustomScrollView
        SliverPadding
          #padding all 16
          SliverList
            Text "Row"

    A nested ListView instead of SliverList.builder

    // BAD — shrinkWrap builds all 500 rows at once and on every rebuild
    SizedBox height:400
      CustomScrollView
        SliverAppBar pinned:true
          Text id:title "Catalog"
        ListView.builder itemCount:500 shrinkWrap:true physics:never
          @Macros id:itemBuilder index:0
            Text $"Row ${index}"
    // GOOD — rows are built as you scroll
    SizedBox height:400
      CustomScrollView
        SliverAppBar pinned:true
          Text id:title "Catalog"
        SliverList.builder itemCount:500
          @Macros id:itemBuilder index:0
            Text $"Row ${index}"

    See also