offsetsForScaleChange static method
({double? horizontal, double? vertical})
offsetsForScaleChange({
- required double currentVerticalOffset,
- required double oldItemExtent,
- required double newItemExtent,
- required double oldTopPadding,
- required double newTopPadding,
- required double newContentHeight,
- required double viewportHeight,
- required double? currentHorizontalOffset,
- required double oldContentWidth,
- required double newContentWidth,
- required double hMinScrollExtent,
- required double hMaxScrollExtent,
New vertical/horizontal offsets after a change of the row extent or the
top padding, such as a Scale change, preserving the node at the top of
the viewport. An offset inside the old top padding keeps its share of the
padding. The horizontal offset scales by the content-width ratio when
currentHorizontalOffset is given. A field is null when that axis needs
no move.
Implementation
static ({double? vertical, double? horizontal}) offsetsForScaleChange({
required double currentVerticalOffset,
required double oldItemExtent,
required double newItemExtent,
required double oldTopPadding,
required double newTopPadding,
required double newContentHeight,
required double viewportHeight,
required double? currentHorizontalOffset,
required double oldContentWidth,
required double newContentWidth,
required double hMinScrollExtent,
required double hMaxScrollExtent,
}) {
// Vertical: an offset inside the top padding keeps its share of the
// padding; below it, the top fractional item position maps into the new
// item extent.
final double newOffset;
if (oldTopPadding > 0 && currentVerticalOffset < oldTopPadding) {
newOffset = currentVerticalOffset / oldTopPadding * newTopPadding;
} else {
final topFractionalIndex = oldItemExtent > 0
? (currentVerticalOffset - oldTopPadding) / oldItemExtent
: 0.0;
newOffset = newTopPadding + topFractionalIndex * newItemExtent;
}
final newMaxExtent = (newContentHeight - viewportHeight).clamp(
0.0,
double.infinity,
);
final clampedV = newOffset.clamp(0.0, newMaxExtent);
final double? vertical = (clampedV - currentVerticalOffset).abs() > 0.5
? clampedV
: null;
// Horizontal: scale the offset by the content-width ratio.
double? horizontal;
if (currentHorizontalOffset != null && oldContentWidth > 0) {
final ratio = newContentWidth / oldContentWidth;
final newH = currentHorizontalOffset * ratio;
final clampedH = newH.clamp(hMinScrollExtent, hMaxScrollExtent);
if ((clampedH - currentHorizontalOffset).abs() > 0.5) {
horizontal = clampedH;
}
}
return (vertical: vertical, horizontal: horizontal);
}