Skip to content

Commit 308f8fd

Browse files
authored
Use leading scroll offset instead of infinite offset to reveal pinned slivers (#191329)
Applicable only if the target provided to `RenderViewportBase.getOffsetToReveal` (and its callers e.g `Scrollable.ensureVisible`, `scrollUntilVisible` etc.) is a pinned sliver. (in say `AxisDirection.down`) If the pinned sliver is below the leading edge, scroll the viewport by the minimum offset required to pin the sliver rather than giving it an infinite target offset which just makes it scroll to the end of the viewport. While the header will always remain pinned, other slivers below it do not need to scroll. Fixes [When calling Scrollable.ensureVisible to pinned SliverPersistentHeader, the view scrolls too much](#115964)
1 parent 717d36b commit 308f8fd

2 files changed

Lines changed: 385 additions & 17 deletions

File tree

‎packages/flutter/lib/src/rendering/viewport.dart‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,31 +1192,32 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix
11921192
Rect targetRect = MatrixUtils.transformRect(transform, rect);
11931193
final double extentOfPinnedSlivers = maxScrollObstructionExtentBefore(sliver);
11941194

1195+
final double mainAxisExtentDifference = switch (axis) {
1196+
Axis.horizontal => size.width - extentOfPinnedSlivers - rectLocal.width,
1197+
Axis.vertical => size.height - extentOfPinnedSlivers - rectLocal.height,
1198+
};
1199+
final double targetOffset;
11951200
switch (sliver.constraints.growthDirection) {
11961201
case GrowthDirection.forward:
1197-
if (isPinned && alignment <= 0) {
1198-
return RevealedOffset(offset: double.infinity, rect: targetRect);
1199-
}
12001202
leadingScrollOffset -= extentOfPinnedSlivers;
1203+
targetOffset = isPinned && alignment <= 0
1204+
? math.max(offset.pixels, leadingScrollOffset)
1205+
: leadingScrollOffset - mainAxisExtentDifference * alignment;
12011206
case GrowthDirection.reverse:
12021207
if (isPinned && alignment >= 1) {
1203-
return RevealedOffset(offset: double.negativeInfinity, rect: targetRect);
1208+
targetOffset = math.min(offset.pixels, leadingScrollOffset);
1209+
} else {
1210+
// If child's growth direction is reverse, when viewport.offset is
1211+
// `leadingScrollOffset`, it is positioned just outside of the leading
1212+
// edge of the viewport.
1213+
leadingScrollOffset -= switch (axis) {
1214+
Axis.vertical => targetRect.height,
1215+
Axis.horizontal => targetRect.width,
1216+
};
1217+
targetOffset = leadingScrollOffset - mainAxisExtentDifference * alignment;
12041218
}
1205-
// If child's growth direction is reverse, when viewport.offset is
1206-
// `leadingScrollOffset`, it is positioned just outside of the leading
1207-
// edge of the viewport.
1208-
leadingScrollOffset -= switch (axis) {
1209-
Axis.vertical => targetRect.height,
1210-
Axis.horizontal => targetRect.width,
1211-
};
12121219
}
12131220

1214-
final double mainAxisExtentDifference = switch (axis) {
1215-
Axis.horizontal => size.width - extentOfPinnedSlivers - rectLocal.width,
1216-
Axis.vertical => size.height - extentOfPinnedSlivers - rectLocal.height,
1217-
};
1218-
1219-
final double targetOffset = leadingScrollOffset - mainAxisExtentDifference * alignment;
12201221
final double offsetDifference = offset.pixels - targetOffset;
12211222

12221223
targetRect = switch (axisDirection) {

0 commit comments

Comments
 (0)