text
stringlengths
1
372
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),
and at 100%, you want alignment(0.0, 1.0).
these coordinates correspond to top and bottom
alignment, respectively.
<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);
// calculate the vertical alignment of the background
// based on the scroll percent.
final verticalAlignment = alignment(0.0, scrollFraction * 2 - 1);
<code_end>
use verticalAlignment, along with the size of the
list item and the size of the background image,
to produce a rect that determines where the
background image should be positioned.
<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);
// calculate the vertical alignment of the background
// based on the scroll percent.
final verticalAlignment = alignment(0.0, scrollFraction * 2 - 1);
// convert the background alignment into a pixel offset for
// painting purposes.
final backgroundSize =
(backgroundimagekey.currentcontext!.findrenderobject() as RenderBox)
.size;
final listItemSize = context.size;
final childRect =
verticalAlignment.inscribe(backgroundSize, offset.zero & listItemSize);
<code_end>
using childRect, paint the background image with
the desired translation transformation.
it’s this transformation over time that gives you the
parallax effect.
<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;