text stringlengths 1 372 |
|---|
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. |
} |
@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 || |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.