text
stringlengths
1
474
style: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
Text(
country,
style: const TextStyle(
color: Colors.white,
fontSize: 14,
),
),
],
),
);
}
}<code_end>
Next, add the list items to your ParallaxRecipe widget.
<code_start>class ParallaxRecipe extends StatelessWidget {
const ParallaxRecipe({super.key});
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
for (final location in locations)
LocationListItem(
imageUrl: location.imageUrl,
name: location.name,
country: location.place,
),
],
),
);
}
}<code_end>
You now have a typical, scrollable list of cards
that displays seven unique locations in the world.
In the next step, you add a parallax effect to the
background image.<topic_end>
<topic_start>
Implement the parallax effect
A parallax scrolling effect is achieved by slightly
pushing the background image in the opposite direction
of the rest of the list. As the list items slide up
the screen, each background image slides slightly downward.
Conversely, as the list items slide down the screen,
each background image slides slightly upward.
Visually, this results in parallax.The parallax effect depends on the list item’s
current position within its ancestor Scrollable.
As the list item’s scroll position changes, the position
of the list item’s background image must also change.
This is an interesting problem to solve. The position
of a list item within the Scrollable isn’t
available until Flutter’s layout phase is complete.
This means that the position of the background image
must be determined in the paint phase, which comes after
the layout phase. Fortunately, Flutter provides a widget
called Flow, which is specifically designed to give you
control over the transform of a child widget immediately
before the widget is painted. In other words,
you can intercept the painting phase and take control
to reposition your child widgets however you want.info Note
To learn more, check out this short
Widget of the Week video on the Flow widget:info Note
In cases where you need control over what a child paints,
rather than where a child is painted,
consider using a CustomPaint widget.In cases where you need control over the layout,
painting, and hit testing, consider defining a
custom RenderBox.Wrap your background Image widget with a
Flow widget.
<code_start>Widget _buildParallaxBackground(BuildContext context) {
return Flow(
children: [
Image.network(
imageUrl,
fit: BoxFit.cover,
),
],
);
}<code_end>
Introduce a new FlowDelegate called ParallaxFlowDelegate.
<code_start>Widget _buildParallaxBackground(BuildContext context) {
return Flow(
delegate: ParallaxFlowDelegate(),
children: [
Image.network(
imageUrl,
fit: BoxFit.cover,
),
],
);
}<code_end>
<code_start>class ParallaxFlowDelegate extends FlowDelegate {
ParallaxFlowDelegate();
@override
BoxConstraints getConstraintsForChild(int i, BoxConstraints constraints) {
// TODO: We'll add more to this later.
}