Bird
Raised Fist0
Fluttermobile~20 mins

Slivers (SliverList, SliverGrid, SliverAppBar) in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Sliver Demo Screen
This screen demonstrates how to use SliverAppBar, SliverList, and SliverGrid together in a scrollable view.
Target UI
┌───────────────────────────────┐
│ Sliver Demo Screen            │
│ ┌─────────────────────────┐ │
│ │ SliverAppBar with title │ │
│ └─────────────────────────┘ │
│ ┌─────────────────────────┐ │
│ │ SliverList:             │ │
│ │ - Item 1                │ │
│ │ - Item 2                │ │
│ │ - Item 3                │ │
│ │ ...                     │ │
│ └─────────────────────────┘ │
│ ┌─────────────────────────┐ │
│ │ SliverGrid:             │ │
│ │ [Tile 1] [Tile 2]       │ │
│ │ [Tile 3] [Tile 4]       │ │
│ │ ...                     │ │
│ └─────────────────────────┘ │
└───────────────────────────────┘
Use a CustomScrollView as the main scrollable area.
Add a SliverAppBar with a title 'Sliver Demo' that stays visible when scrolled.
Add a SliverList below the app bar with 5 list items labeled 'List Item 1' to 'List Item 5'.
Add a SliverGrid below the list with 6 grid tiles labeled 'Grid Tile 1' to 'Grid Tile 6'.
Grid should have 2 columns with fixed spacing.
Ensure smooth scrolling through all slivers.
Starter Code
Flutter
import 'package:flutter/material.dart';

class SliverDemoScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          // TODO: Add SliverAppBar here
          // TODO: Add SliverList here
          // TODO: Add SliverGrid here
        ],
      ),
    );
  }
}
Task 1
Task 2
Task 3
Solution
Flutter
import 'package:flutter/material.dart';

class SliverDemoScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverAppBar(
            pinned: true,
            expandedHeight: 100,
            flexibleSpace: FlexibleSpaceBar(
              title: Text('Sliver Demo'),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (context, index) => ListTile(
                title: Text('List Item ${index + 1}'),
              ),
              childCount: 5,
            ),
          ),
          SliverGrid(
            delegate: SliverChildBuilderDelegate(
              (context, index) => Container(
                alignment: Alignment.center,
                margin: EdgeInsets.all(8),
                color: Colors.blue[100 * ((index % 8) + 1)],
                child: Text('Grid Tile ${index + 1}'),
              ),
              childCount: 6,
            ),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 8,
              crossAxisSpacing: 8,
              childAspectRatio: 1.5,
            ),
          ),
        ],
      ),
    );
  }
}

We use a CustomScrollView to combine multiple slivers in one scrollable area. The SliverAppBar is pinned so it stays visible at the top when scrolling. The SliverList creates a vertical list of 5 items using SliverChildBuilderDelegate for efficiency. Below that, the SliverGrid shows 6 tiles arranged in 2 columns with spacing. Each tile is a colored container with centered text. This setup demonstrates how slivers can create flexible, performant scrolling layouts.

Final Result
Completed Screen
┌───────────────────────────────┐
│ Sliver Demo                  │
│ ┌─────────────────────────┐ │
│ │ SliverAppBar (pinned)   │ │
│ └─────────────────────────┘ │
│ ┌─────────────────────────┐ │
│ │ List Item 1             │ │
│ │ List Item 2             │ │
│ │ List Item 3             │ │
│ │ List Item 4             │ │
│ │ List Item 5             │ │
│ └─────────────────────────┘ │
│ ┌───────────────┬─────────┐ │
│ │ Grid Tile 1   │ Grid 2  │ │
│ ├───────────────┼─────────┤ │
│ │ Grid Tile 3   │ Grid 4  │ │
│ ├───────────────┼─────────┤ │
│ │ Grid Tile 5   │ Grid 6  │ │
│ └───────────────┴─────────┘ │
└───────────────────────────────┘
User can scroll up and down smoothly through the SliverList and SliverGrid.
SliverAppBar stays pinned at the top when scrolling down.
Grid tiles and list items are clearly visible with spacing.
Stretch Goal
Add a floating action button that scrolls the CustomScrollView back to the top when tapped.
💡 Hint
Use a ScrollController with the CustomScrollView and call animateTo(0) on button tap.

Practice

(1/5)
1. What is the main purpose of using SliverAppBar in a Flutter app?
easy
A. To replace the default app bar with a static title
B. To display a fixed footer at the bottom of the screen
C. To create a grid layout inside a scrollable area
D. To create a header that can expand, collapse, and stay visible when scrolling

Solution

  1. Step 1: Understand SliverAppBar role

    SliverAppBar is designed to create headers that can expand and collapse as the user scrolls.
  2. Step 2: Compare options with SliverAppBar behavior

    Only To create a header that can expand, collapse, and stay visible when scrolling describes the expandable and pinned header behavior of SliverAppBar.
  3. Final Answer:

    To create a header that can expand, collapse, and stay visible when scrolling -> Option D
  4. Quick Check:

    SliverAppBar = expandable header [OK]
Hint: Remember: SliverAppBar controls flexible headers [OK]
Common Mistakes:
  • Confusing SliverAppBar with fixed AppBar
  • Thinking SliverAppBar creates grids or lists
  • Assuming SliverAppBar is a footer widget
2. Which of the following is the correct way to create a SliverList inside a CustomScrollView?
easy
A. SliverList(items: List.generate(5, (index) => Text('Item $index')))
B. SliverList(delegate: SliverChildBuilderDelegate((context, index) => Text('Item $index')))
C. SliverList(itemBuilder: (context, index) => Text('Item $index'))
D. SliverList(children: [Text('Item 1'), Text('Item 2')])

Solution

  1. Step 1: Recall SliverList constructor

    SliverList requires a delegate parameter, usually SliverChildBuilderDelegate or SliverChildListDelegate.
  2. Step 2: Check each option's syntax

    SliverList(delegate: SliverChildBuilderDelegate((context, index) => Text('Item $index'))) correctly uses delegate: SliverChildBuilderDelegate. Options A, B, and C use invalid parameters or properties.
  3. Final Answer:

    SliverList(delegate: SliverChildBuilderDelegate((context, index) => Text('Item $index'))) -> Option B
  4. Quick Check:

    SliverList needs delegate = SliverList(delegate: SliverChildBuilderDelegate((context, index) => Text('Item $index'))) [OK]
Hint: SliverList always needs a delegate parameter [OK]
Common Mistakes:
  • Using children instead of delegate
  • Using itemBuilder directly on SliverList
  • Passing items instead of delegate
3. Given the code snippet below, what will be displayed on the screen?
CustomScrollView(
  slivers: [
    SliverAppBar(
      expandedHeight: 100,
      pinned: true,
      flexibleSpace: FlexibleSpaceBar(title: Text('Hello')),
    ),
    SliverList(
      delegate: SliverChildBuilderDelegate(
        (context, index) => ListTile(title: Text('Item $index')),
        childCount: 3,
      ),
    ),
  ],
)
medium
A. A scrollable view with a pinned app bar titled 'Hello' and 3 list items labeled Item 0 to Item 2
B. A static app bar with title 'Hello' and 3 buttons labeled Item 1 to Item 3
C. An error because SliverAppBar cannot be pinned
D. A scrollable grid with 3 items and no app bar

Solution

  1. Step 1: Analyze SliverAppBar properties

    The app bar is pinned and has a flexible space with title 'Hello', so it stays visible when scrolling.
  2. Step 2: Analyze SliverList delegate

    The list builds 3 items labeled 'Item 0', 'Item 1', and 'Item 2'.
  3. Final Answer:

    A scrollable view with a pinned app bar titled 'Hello' and 3 list items labeled Item 0 to Item 2 -> Option A
  4. Quick Check:

    Pinned SliverAppBar + SliverList with 3 items = A scrollable view with a pinned app bar titled 'Hello' and 3 list items labeled Item 0 to Item 2 [OK]
Hint: Pinned SliverAppBar stays visible; SliverList shows items lazily [OK]
Common Mistakes:
  • Thinking pinned means static, not scrollable
  • Confusing list items count or labels
  • Assuming SliverAppBar cannot be pinned
4. Identify the error in the following Flutter code snippet using slivers:
CustomScrollView(
  slivers: [
    SliverGrid(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
      children: List.generate(4, (index) => Container(color: Colors.blue)),
    ),
  ],
)
medium
A. List.generate cannot be used inside SliverGrid
B. SliverGridDelegateWithFixedCrossAxisCount is not a valid class
C. SliverGrid requires a delegate, not a children parameter
D. CustomScrollView cannot contain SliverGrid

Solution

  1. Step 1: Check SliverGrid constructor requirements

    SliverGrid requires a delegate parameter, not children.
  2. Step 2: Validate other parts

    SliverGridDelegateWithFixedCrossAxisCount is valid, and List.generate can be used inside a delegate.
  3. Final Answer:

    SliverGrid requires a delegate, not a children parameter -> Option C
  4. Quick Check:

    SliverGrid needs delegate, not children [OK]
Hint: SliverGrid uses delegate, not children, for lazy loading [OK]
Common Mistakes:
  • Using children instead of delegate in SliverGrid
  • Confusing SliverGridDelegateWithFixedCrossAxisCount as invalid
  • Thinking CustomScrollView disallows SliverGrid
5. You want to create a scrollable screen with a header that expands and pins, followed by a grid of images with 3 columns, and then a list of text items. Which of the following is the best way to structure your Flutter widget tree?
hard
A. CustomScrollView(slivers: [SliverAppBar(pinned: true, expandedHeight: 200, flexibleSpace: FlexibleSpaceBar(title: Text('Gallery'))), SliverGrid(delegate: SliverChildBuilderDelegate((c, i) => Image.network(urls[i]), childCount: urls.length), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3)), SliverList(delegate: SliverChildBuilderDelegate((c, i) => ListTile(title: Text(items[i])), childCount: items.length))])
B. ListView(children: [AppBar(title: Text('Gallery')), GridView.count(crossAxisCount: 3, children: images), ListView(children: texts)])
C. CustomScrollView(children: [SliverAppBar(), SliverGrid(), SliverList()])
D. Column(children: [SliverAppBar(), SliverGrid(), SliverList()])

Solution

  1. Step 1: Identify correct widget for scrollable slivers

    CustomScrollView with slivers parameter is needed to combine SliverAppBar, SliverGrid, and SliverList.
  2. Step 2: Check each option's structure

    CustomScrollView(slivers: [SliverAppBar(pinned: true, expandedHeight: 200, flexibleSpace: FlexibleSpaceBar(title: Text('Gallery'))), SliverGrid(delegate: SliverChildBuilderDelegate((c, i) => Image.network(urls[i]), childCount: urls.length), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3)), SliverList(delegate: SliverChildBuilderDelegate((c, i) => ListTile(title: Text(items[i])), childCount: items.length))]) correctly uses CustomScrollView with slivers and proper delegates. ListView(children: [AppBar(title: Text('Gallery')), GridView.count(crossAxisCount: 3, children: images), ListView(children: texts)]) uses nested scrollables incorrectly. CustomScrollView(children: [SliverAppBar(), SliverGrid(), SliverList()]) uses children instead of slivers. Column(children: [SliverAppBar(), SliverGrid(), SliverList()]) uses Column which cannot host slivers.
  3. Final Answer:

    CustomScrollView with SliverAppBar, SliverGrid, and SliverList using delegates -> Option A
  4. Quick Check:

    Use CustomScrollView with slivers and delegates = CustomScrollView(slivers: [SliverAppBar(pinned: true, expandedHeight: 200, flexibleSpace: FlexibleSpaceBar(title: Text('Gallery'))), SliverGrid(delegate: SliverChildBuilderDelegate((c, i) => Image.network(urls[i]), childCount: urls.length), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3)), SliverList(delegate: SliverChildBuilderDelegate((c, i) => ListTile(title: Text(items[i])), childCount: items.length))]) [OK]
Hint: Use CustomScrollView with slivers and delegates for mixed scroll content [OK]
Common Mistakes:
  • Using ListView or Column instead of CustomScrollView for slivers
  • Passing slivers as children instead of slivers parameter
  • Not using delegates for SliverGrid and SliverList
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard