CustomScrollView lets you create a scrollable area with different types of scrollable widgets combined. It helps you build flexible scrolling screens.
CustomScrollView in Flutter
CustomScrollView(
slivers: [
// List of sliver widgets like SliverAppBar, SliverList, SliverGrid
],
)The slivers property takes a list of sliver widgets that define the scrollable content.
Slivers are special widgets that work inside CustomScrollView to create scroll effects.
CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildListDelegate([
Text('Item 1'),
Text('Item 2'),
]),
),
],
)CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 150,
flexibleSpace: FlexibleSpaceBar(
title: Text('Header'),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(title: Text('Item $index')),
childCount: 5,
),
),
],
)This app shows a scrollable screen with a header that stays pinned at the top and a list of 10 items below it. You can scroll the list, and the header stays visible.
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: CustomScrollView( slivers: [ SliverAppBar( pinned: true, expandedHeight: 100, flexibleSpace: const FlexibleSpaceBar( title: Text('My Scroll View'), ), ), SliverList( delegate: SliverChildBuilderDelegate( (context, index) => ListTile( title: Text('List Item $index'), ), childCount: 10, ), ), ], ), ), ); } }
SliverAppBar can be pinned, floating, or snap to control how it behaves when scrolling.
Use SliverChildBuilderDelegate for large or infinite lists to build items on demand.
CustomScrollView requires all children to be slivers, so normal widgets must be wrapped in sliver adapters if needed.
CustomScrollView combines multiple scrollable widgets called slivers into one scrollable area.
It is useful for creating complex scrolling screens with headers, lists, and grids.
Slivers control how each part scrolls and appears on the screen.
Practice
CustomScrollView in Flutter?Solution
Step 1: Understand the role of CustomScrollView
CustomScrollView allows combining different scrollable widgets called slivers into one scrollable area.Step 2: Compare options with this understanding
Only To combine multiple scrollable widgets called slivers into one scrollable area. correctly describes this purpose; others describe unrelated features.Final Answer:
To combine multiple scrollable widgets called slivers into one scrollable area. -> Option AQuick Check:
CustomScrollView = combine slivers [OK]
- Thinking CustomScrollView only shows one list
- Confusing CustomScrollView with fixed headers
- Assuming it handles input events directly
CustomScrollView with a sliver list in Flutter?Solution
Step 1: Check CustomScrollView property for slivers
CustomScrollView uses thesliversproperty to accept sliver widgets, notchildren.Step 2: Identify correct sliver list syntax
SliverList requires a delegate like SliverChildBuilderDelegate to build items lazily.Final Answer:
CustomScrollView(slivers: [SliverList(delegate: SliverChildBuilderDelegate((context, index) => Text('Item $index'), childCount: 10))]) -> Option BQuick Check:
Use slivers property with SliverList [OK]
- Using children instead of slivers property
- Putting ListView inside CustomScrollView slivers
- Not using SliverChildBuilderDelegate for SliverList
CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 100,
flexibleSpace: FlexibleSpaceBar(title: Text('Header')),
pinned: true,
),
SliverList(
delegate: SliverChildListDelegate([
Container(height: 50, color: Colors.red),
Container(height: 50, color: Colors.green),
Container(height: 50, color: Colors.blue),
]),
),
],
)Solution
Step 1: Understand SliverAppBar behavior
SliverAppBar with pinned: true stays visible at top when scrolling.Step 2: Analyze SliverList content
SliverList shows three containers stacked vertically with specified colors and heights.Final Answer:
A scrollable screen with a pinned header titled 'Header' and three colored boxes stacked vertically. -> Option DQuick Check:
SliverAppBar pinned + SliverList visible = A scrollable screen with a pinned header titled 'Header' and three colored boxes stacked vertically. [OK]
- Thinking header disappears on scroll
- Assuming no scrolling because of pinned header
- Believing SliverAppBar causes error inside CustomScrollView
CustomScrollView:
CustomScrollView(
children: [
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => Text('Item $index'),
childCount: 5,
),
),
],
)Solution
Step 1: Check CustomScrollView properties
CustomScrollView expects sliver widgets inside thesliversproperty, notchildren.Step 2: Verify other parts
SliverChildBuilderDelegate has childCount, SliverList is valid, and Text is allowed inside delegate.Final Answer:
Using children instead of slivers property in CustomScrollView. -> Option CQuick Check:
CustomScrollView uses slivers, not children [OK]
- Using children property instead of slivers
- Confusing SliverList with ListView
- Thinking Text widget is invalid inside SliverChildBuilderDelegate
CustomScrollView will achieve this?Solution
Step 1: Identify slivers for header, grid, and list
SliverAppBar with flexibleSpace creates a shrinking header. SliverGrid shows a grid of items. SliverList shows a scrollable list.Step 2: Check options for correct sliver usage
SliverAppBar (with flexibleSpace), SliverGrid, SliverList uses all correct slivers inside CustomScrollView. Others mix non-sliver widgets or incorrect combinations.Final Answer:
SliverAppBar (with flexibleSpace), SliverGrid, SliverList -> Option AQuick Check:
Header + grid + list = SliverAppBar + SliverGrid + SliverList [OK]
- Using ListView/GridView inside CustomScrollView slivers
- Not using SliverAppBar for shrinking header
- Confusing SliverPersistentHeader with SliverAppBar
Ready to prove it?
Pass the test and this pattern moves unknown
