text stringlengths 1 372 |
|---|
// 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 |
far off in the distant background. this effect is |
known as parallax. |
in this recipe, you create the parallax effect by building |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.