text stringlengths 1 474 |
|---|
@override |
void paintChildren(FlowPaintingContext context) { |
// TODO: We'll add more to this later. |
} |
@override |
bool shouldRepaint(covariant FlowDelegate oldDelegate) { |
// TODO: We'll add more to this later. |
return true; |
} |
}<code_end> |
A FlowDelegate controls how its children are sized |
and where those children are painted. In this case, |
your Flow widget has only one child: the background image. |
That image must be exactly as wide as the Flow widget.Return tight width constraints for your background image child. |
<code_start>@override |
BoxConstraints getConstraintsForChild(int i, BoxConstraints constraints) { |
return BoxConstraints.tightFor( |
width: constraints.maxWidth, |
); |
}<code_end> |
Your background images are now sized appropriately, |
but you still need to calculate the vertical position |
of each background image based on its scroll |
position, and then paint it.There are three critical pieces of information that |
you need to compute the desired position of a |
background image:To look up the bounds of the Scrollable, |
you pass a ScrollableState into your FlowDelegate.To look up the bounds of your individual list item, |
pass your list item’s BuildContext into your FlowDelegate.To look up the final size of your background image, |
assign a GlobalKey to your Image widget, |
and then you pass that GlobalKey into your |
FlowDelegate.Make this information available to ParallaxFlowDelegate. |
<code_start>@immutable |
class LocationListItem extends StatelessWidget { |
final GlobalKey _backgroundImageKey = GlobalKey(); |
Widget _buildParallaxBackground(BuildContext context) { |
return Flow( |
delegate: ParallaxFlowDelegate( |
scrollable: Scrollable.of(context), |
listItemContext: context, |
backgroundImageKey: _backgroundImageKey, |
), |
children: [ |
Image.network( |
imageUrl, |
key: _backgroundImageKey, |
fit: BoxFit.cover, |
), |
], |
); |
} |
}<code_end> |
<code_start>class ParallaxFlowDelegate extends FlowDelegate { |
ParallaxFlowDelegate({ |
required this.scrollable, |
required this.listItemContext, |
required this.backgroundImageKey, |
}); |
final ScrollableState scrollable; |
final BuildContext listItemContext; |
final GlobalKey backgroundImageKey; |
}<code_end> |
Having all the information needed to implement |
parallax scrolling, implement the shouldRepaint() method. |
<code_start>@override |
bool shouldRepaint(ParallaxFlowDelegate oldDelegate) { |
return scrollable != oldDelegate.scrollable || |
listItemContext != oldDelegate.listItemContext || |
backgroundImageKey != oldDelegate.backgroundImageKey; |
}<code_end> |
Now, implement the layout calculations for the parallax effect.First, calculate the pixel position of a list |
item within its ancestor Scrollable. |
<code_start>@override |
void paintChildren(FlowPaintingContext context) { |
// Calculate the position of this list item within the viewport. |
final scrollableBox = scrollable.context.findRenderObject() as RenderBox; |
final listItemBox = listItemContext.findRenderObject() as RenderBox; |
final listItemOffset = listItemBox.localToGlobal( |
listItemBox.size.centerLeft(Offset.zero), |
ancestor: scrollableBox); |
}<code_end> |
Use the pixel position of the list item to calculate its |
percentage from the top of the Scrollable. |
A list item at the top of the scrollable area should |
produce 0%, and a list item at the bottom of the |
scrollable area should produce 100%. |
<code_start>@override |
void paintChildren(FlowPaintingContext context) { |
// Calculate the position of this list item within the viewport. |
final scrollableBox = scrollable.context.findRenderObject() as RenderBox; |
final listItemBox = listItemContext.findRenderObject() as RenderBox; |
final listItemOffset = listItemBox.localToGlobal( |
listItemBox.size.centerLeft(Offset.zero), |
ancestor: scrollableBox); |
// Determine the percent position of this list item within the |
// scrollable area. |
final viewportDimension = scrollable.position.viewportDimension; |
final scrollFraction = |
(listItemOffset.dy / viewportDimension).clamp(0.0, 1.0);<code_end> |
Use the scroll percentage to calculate an Alignment. |
At 0%, you want Alignment(0.0, -1.0), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.