text
stringlengths
1
474
Furthermore, you can configure the SliverAppBar to shrink and
expand as the user scrolls.To create this effect:
<code_start>CustomScrollView(
slivers: [
// Add the app bar to the CustomScrollView.
const SliverAppBar(
// Provide a standard title.
title: Text(title),
// Allows the user to reveal the app bar if they begin scrolling
// back up the list of items.
floating: true,
// Display a placeholder widget to visualize the shrinking size.
flexibleSpace: Placeholder(),
// Make the initial height of the SliverAppBar larger than normal.
expandedHeight: 200,
),
],
)<code_end>
lightbulb Tip
Play around with the
various properties you can pass to the SliverAppBar widget,
and use hot reload to see the results. For example, use an Image
widget for the flexibleSpace property to create a background image that
shrinks in size as it’s scrolled offscreen.<topic_end>
<topic_start>
3. Add a list of items using a SliverList
Now that you have the app bar in place, add a list of items to the
CustomScrollView. You have two options: a SliverList
or a SliverGrid. If you need to display a list of items one after the other,
use the SliverList widget.
If you need to display a grid list, use the SliverGrid widget.The SliverList and SliverGrid widgets take one required parameter: a
SliverChildDelegate, which provides a list
of widgets to SliverList or SliverGrid.
For example, the SliverChildBuilderDelegate
allows you to create a list of items that are built lazily as you scroll,
just like the ListView.builder widget.
<code_start>// Next, create a SliverList
SliverList(
// Use a delegate to build items as they're scrolled on screen.
delegate: SliverChildBuilderDelegate(
// The builder function returns a ListTile with a title that
// displays the index of the current item.
(context, index) => ListTile(title: Text('Item #$index')),
// Builds 1000 ListTiles
childCount: 1000,
),
)<code_end>
<topic_end>
<topic_start>
Interactive example
<code_start>import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const title = 'Floating App Bar';
return MaterialApp(
title: title,
home: Scaffold(
// No appbar provided to the Scaffold, only a body with a
// CustomScrollView.
body: CustomScrollView(
slivers: [
// Add the app bar to the CustomScrollView.
const SliverAppBar(
// Provide a standard title.
title: Text(title),
// Allows the user to reveal the app bar if they begin scrolling
// back up the list of items.
floating: true,
// Display a placeholder widget to visualize the shrinking size.
flexibleSpace: Placeholder(),
// Make the initial height of the SliverAppBar larger than normal.
expandedHeight: 200,
),
// Next, create a SliverList
SliverList(
// Use a delegate to build items as they're scrolled on screen.
delegate: SliverChildBuilderDelegate(
// The builder function returns a ListTile with a title that
// displays the index of the current item.
(context, index) => ListTile(title: Text('Item #$index')),
// Builds 1000 ListTiles
childCount: 1000,
),
),
],
),
),
);
}
}<code_end>
<topic_end>
<topic_start>Create a scrolling parallax effect
When you scroll a list of cards (containing images,
for example) in an app, you might notice that those
images appear to scroll more slowly than the rest of the
screen. It almost looks as if the cards in the list
are in the foreground, but the images themselves sit