Skip to content

Commit da1eef2

Browse files
authored
SliverFillRemaining extends beyond viewport size when fillOverscroll is true (flutter#191236)
Overscroll on the trailing edge is reported by the `constraints.scrollOffset`. Previously, we were using only `constraints.remainingPaintExtent` to determine the `maxPaintExtent`. This PR adds `constraints.scrollOffset` to the `maxPaintExtent` to account for trailing edge overscroll. In reverse `AxisDirection`s overscrolling at the bottom of the viewport (which is now the leading edge) stretches the child, increasing its size. So, this PR uses the child's dynamic size to anchor it to the viewport edge when overscrolling. Fixes [SliverFillRemaining(fillOverscroll: true) does not extend beyond the viewport size](flutter#141077)
1 parent daf5c35 commit da1eef2

2 files changed

Lines changed: 243 additions & 2 deletions

File tree

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,16 @@ class RenderSliverFillRemainingAndOverscroll extends RenderSliverSingleBoxAdapte
267267
double extent = constraints.viewportMainAxisExtent - constraints.precedingScrollExtent;
268268
// The maxExtent includes any overscrolled area. Can be < 0 if we have
269269
// overscroll in the opposite direction, away from the end of the list.
270-
double maxExtent = constraints.remainingPaintExtent - math.min(constraints.overlap, 0.0);
270+
//
271+
// When overscrolling past this sliver, the viewport clamps
272+
// remainingPaintExtent to the viewport extent and delivers the
273+
// overscrolled distance as this sliver's scrollOffset, so the
274+
// scrollOffset term is needed for the stretched child to still reach the
275+
// viewport's trailing edge.
276+
double maxExtent =
277+
constraints.remainingPaintExtent -
278+
math.min(constraints.overlap, 0.0) +
279+
constraints.scrollOffset;
271280

272281
if (child != null) {
273282
final double childExtent = switch (constraints.axis) {
@@ -283,7 +292,10 @@ class RenderSliverFillRemainingAndOverscroll extends RenderSliverSingleBoxAdapte
283292
// size or overscrolling at the top of the scrollable (rather than at the
284293
// end where this sliver is).
285294
maxExtent = math.max(extent, maxExtent);
286-
child!.layout(constraints.asBoxConstraints(minExtent: extent, maxExtent: maxExtent));
295+
child!.layout(
296+
constraints.asBoxConstraints(minExtent: extent, maxExtent: maxExtent),
297+
parentUsesSize: true,
298+
);
287299
}
288300

289301
assert(
@@ -310,4 +322,39 @@ class RenderSliverFillRemainingAndOverscroll extends RenderSliverSingleBoxAdapte
310322
setChildParentData(child!, constraints, geometry!);
311323
}
312324
}
325+
326+
@override
327+
void setChildParentData(
328+
RenderObject child,
329+
SliverConstraints constraints,
330+
SliverGeometry geometry,
331+
) {
332+
super.setChildParentData(child, constraints, geometry);
333+
if (child is! RenderBox || !child.hasSize) {
334+
return;
335+
}
336+
// A child stretched into the overscroll area is larger than the
337+
// scrollExtent, so for reversed axis directions it must be anchored by its
338+
// actual size to keep its leading edge glued to the viewport's edge.
339+
final childParentData = child.parentData! as SliverPhysicalParentData;
340+
final Size childSize = child.size;
341+
switch (applyGrowthDirectionToAxisDirection(
342+
constraints.axisDirection,
343+
constraints.growthDirection,
344+
)) {
345+
case AxisDirection.up:
346+
childParentData.paintOffset = Offset(
347+
0.0,
348+
geometry.paintExtent + constraints.scrollOffset - childSize.height,
349+
);
350+
case AxisDirection.left:
351+
childParentData.paintOffset = Offset(
352+
geometry.paintExtent + constraints.scrollOffset - childSize.width,
353+
0.0,
354+
);
355+
case AxisDirection.right:
356+
case AxisDirection.down:
357+
break;
358+
}
359+
}
313360
}

‎packages/flutter/test/widgets/sliver_fill_remaining_test.dart‎

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:math' as math;
66

77
import 'package:flutter/foundation.dart';
8+
import 'package:flutter/gestures.dart' show HitTestEntry, HitTestResult;
89
import 'package:flutter/widgets.dart';
910
import 'package:flutter_test/flutter_test.dart';
1011

@@ -593,6 +594,199 @@ void main() {
593594
}),
594595
);
595596

597+
testWidgets(
598+
'child stretches into overscroll that exceeds precedingScrollExtent',
599+
(WidgetTester tester) async {
600+
// Regression test for https://github.com/flutter/flutter/issues/141077.
601+
final controller = ScrollController();
602+
addTearDown(controller.dispose);
603+
final slivers = <Widget>[
604+
sliverBox,
605+
const SliverFillRemaining(
606+
hasScrollBody: false,
607+
fillOverscroll: true,
608+
child: SizedBox.expand(
609+
child: ColoredBox(key: ValueKey<String>('fill'), color: _debugRed),
610+
),
611+
),
612+
];
613+
await tester.pumpWidget(boilerplate(slivers, controller: controller));
614+
expect(
615+
tester.getRect(find.byKey(const ValueKey<String>('fill'))),
616+
const Rect.fromLTRB(0.0, 150.0, 800.0, 600.0),
617+
);
618+
619+
// Overscroll by 200: the preceding box frees up 150 of paint room
620+
// and the remaining 50 become this sliver's scrollOffset.
621+
controller.jumpTo(200.0);
622+
await tester.pump();
623+
624+
final Rect rect = tester.getRect(find.byKey(const ValueKey<String>('fill')));
625+
expect(rect.bottom, 600.0);
626+
expect(rect.height, 650.0);
627+
628+
controller.jumpTo(0.0);
629+
await tester.pump();
630+
expect(
631+
tester.getRect(find.byKey(const ValueKey<String>('fill'))),
632+
const Rect.fromLTRB(0.0, 150.0, 800.0, 600.0),
633+
);
634+
},
635+
variant: const TargetPlatformVariant(<TargetPlatform>{
636+
TargetPlatform.iOS,
637+
TargetPlatform.macOS,
638+
}),
639+
);
640+
641+
testWidgets(
642+
'lone child stretches to cover the whole overscroll',
643+
(WidgetTester tester) async {
644+
// Regression test for https://github.com/flutter/flutter/issues/141077.
645+
final controller = ScrollController();
646+
addTearDown(controller.dispose);
647+
final slivers = <Widget>[
648+
const SliverFillRemaining(
649+
hasScrollBody: false,
650+
fillOverscroll: true,
651+
child: SizedBox.expand(
652+
child: ColoredBox(key: ValueKey<String>('fill'), color: _debugRed),
653+
),
654+
),
655+
];
656+
await tester.pumpWidget(boilerplate(slivers, controller: controller));
657+
expect(
658+
tester.getRect(find.byKey(const ValueKey<String>('fill'))),
659+
const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
660+
);
661+
662+
controller.jumpTo(50.0);
663+
await tester.pump();
664+
665+
final Rect rect = tester.getRect(find.byKey(const ValueKey<String>('fill')));
666+
expect(rect.bottom, 600.0);
667+
expect(rect.height, 650.0);
668+
669+
controller.jumpTo(0.0);
670+
await tester.pump();
671+
expect(
672+
tester.getRect(find.byKey(const ValueKey<String>('fill'))),
673+
const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
674+
);
675+
},
676+
variant: const TargetPlatformVariant(<TargetPlatform>{
677+
TargetPlatform.iOS,
678+
TargetPlatform.macOS,
679+
}),
680+
);
681+
682+
testWidgets(
683+
'child in a reversed scroll view stretches towards the leading edge',
684+
(WidgetTester tester) async {
685+
// Regression test for https://github.com/flutter/flutter/issues/141077.
686+
final controller = ScrollController();
687+
addTearDown(controller.dispose);
688+
await tester.pumpWidget(
689+
TestWidgetsApp(
690+
home: CustomScrollView(
691+
reverse: true,
692+
controller: controller,
693+
slivers: const <Widget>[
694+
SliverFillRemaining(
695+
hasScrollBody: false,
696+
fillOverscroll: true,
697+
child: SizedBox.expand(
698+
child: ColoredBox(key: ValueKey<String>('fill'), color: _debugRed),
699+
),
700+
),
701+
],
702+
),
703+
),
704+
);
705+
expect(
706+
tester.getRect(find.byKey(const ValueKey<String>('fill'))),
707+
const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
708+
);
709+
710+
controller.jumpTo(50.0);
711+
await tester.pump();
712+
713+
final Rect rect = tester.getRect(find.byKey(const ValueKey<String>('fill')));
714+
expect(rect.top, 0.0);
715+
expect(rect.height, 650.0);
716+
717+
// The stretched region hit tests to the child, matching where it
718+
// is painted.
719+
final HitTestResult hitTestResult = tester.hitTestOnBinding(const Offset(400.0, 25.0));
720+
expect(
721+
hitTestResult.path.any(
722+
(HitTestEntry entry) =>
723+
entry.target == tester.renderObject(find.byKey(const ValueKey<String>('fill'))),
724+
),
725+
isTrue,
726+
);
727+
728+
controller.jumpTo(0.0);
729+
await tester.pump();
730+
expect(
731+
tester.getRect(find.byKey(const ValueKey<String>('fill'))),
732+
const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
733+
);
734+
},
735+
variant: const TargetPlatformVariant(<TargetPlatform>{
736+
TargetPlatform.iOS,
737+
TargetPlatform.macOS,
738+
}),
739+
);
740+
741+
testWidgets(
742+
'child in a reversed horizontal scroll view stretches towards the leading edge',
743+
(WidgetTester tester) async {
744+
// Regression test for https://github.com/flutter/flutter/issues/141077.
745+
final controller = ScrollController();
746+
addTearDown(controller.dispose);
747+
await tester.pumpWidget(
748+
TestWidgetsApp(
749+
home: CustomScrollView(
750+
scrollDirection: Axis.horizontal,
751+
reverse: true,
752+
controller: controller,
753+
slivers: const <Widget>[
754+
SliverFillRemaining(
755+
hasScrollBody: false,
756+
fillOverscroll: true,
757+
child: SizedBox.expand(
758+
child: ColoredBox(key: ValueKey<String>('fill'), color: _debugRed),
759+
),
760+
),
761+
],
762+
),
763+
),
764+
);
765+
expect(
766+
tester.getRect(find.byKey(const ValueKey<String>('fill'))),
767+
const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
768+
);
769+
770+
controller.jumpTo(50.0);
771+
await tester.pump();
772+
773+
final Rect rect = tester.getRect(find.byKey(const ValueKey<String>('fill')));
774+
expect(rect.left, 0.0);
775+
expect(rect.width, 850.0);
776+
777+
controller.jumpTo(0.0);
778+
await tester.pump();
779+
expect(
780+
tester.getRect(find.byKey(const ValueKey<String>('fill'))),
781+
const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
782+
);
783+
},
784+
variant: const TargetPlatformVariant(<TargetPlatform>{
785+
TargetPlatform.iOS,
786+
TargetPlatform.macOS,
787+
}),
788+
);
789+
596790
testWidgets(
597791
'child with smaller size is overridden and sized by extent and overscroll',
598792
(WidgetTester tester) async {

0 commit comments

Comments
 (0)