From 6d7922f3f5c1df6c54630b62c667480a76b47ee6 Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Mon, 1 Apr 2024 14:06:07 -0500 Subject: [PATCH] Fix SliverMainAxisGroup layout in reverse (#145572) Fixes https://github.com/flutter/flutter/issues/145068 The original tests for SliverMainAxisGroup did not actually check where the child was painted (#126596). Followed the same pattern in RenderSliverMultiBoxAdaptor: https://github.com/flutter/flutter/blob/11c034f0371eb28576d41a0c218ccae6b38e7702/packages/flutter/lib/src/rendering/sliver_multi_box_adaptor.dart#L666 --- .../lib/src/rendering/sliver_group.dart | 48 +++++++++++++-- .../widgets/sliver_main_axis_group_test.dart | 60 +++++++++++++++++-- 2 files changed, 99 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/rendering/sliver_group.dart b/packages/flutter/lib/src/rendering/sliver_group.dart index e0c4a31542..39a24063c3 100644 --- a/packages/flutter/lib/src/rendering/sliver_group.dart +++ b/packages/flutter/lib/src/rendering/sliver_group.dart @@ -274,8 +274,8 @@ class RenderSliverMainAxisGroup extends RenderSliver with ContainerRenderObjectM assert(() { if (child != null && maxPaintExtent.isInfinite) { throw FlutterError( - 'Unreachable sliver found, you may have a sliver behind ' - 'a sliver with infinite extent. ' + 'Unreachable sliver found, you may have a sliver following ' + 'a sliver with an infinite extent. ' ); } return true; @@ -327,12 +327,50 @@ class RenderSliverMainAxisGroup extends RenderSliver with ContainerRenderObjectM @override void paint(PaintingContext context, Offset offset) { - RenderSliver? child = lastChild; + if (firstChild == null) { + return; + } + // offset is to the top-left corner, regardless of our axis direction. + // originOffset gives us the delta from the real origin to the origin in the axis direction. + final Offset mainAxisUnit, crossAxisUnit, originOffset; + final bool addExtent; + switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) { + case AxisDirection.up: + mainAxisUnit = const Offset(0.0, -1.0); + crossAxisUnit = const Offset(1.0, 0.0); + originOffset = offset + Offset(0.0, geometry!.paintExtent); + addExtent = true; + case AxisDirection.right: + mainAxisUnit = const Offset(1.0, 0.0); + crossAxisUnit = const Offset(0.0, 1.0); + originOffset = offset; + addExtent = false; + case AxisDirection.down: + mainAxisUnit = const Offset(0.0, 1.0); + crossAxisUnit = const Offset(1.0, 0.0); + originOffset = offset; + addExtent = false; + case AxisDirection.left: + mainAxisUnit = const Offset(-1.0, 0.0); + crossAxisUnit = const Offset(0.0, 1.0); + originOffset = offset + Offset(geometry!.paintExtent, 0.0); + addExtent = true; + } + RenderSliver? child = lastChild; while (child != null) { + final double mainAxisDelta = childMainAxisPosition(child); + final double crossAxisDelta = childCrossAxisPosition(child); + Offset childOffset = Offset( + originOffset.dx + mainAxisUnit.dx * mainAxisDelta + crossAxisUnit.dx * crossAxisDelta, + originOffset.dy + mainAxisUnit.dy * mainAxisDelta + crossAxisUnit.dy * crossAxisDelta, + ); + if (addExtent) { + childOffset += mainAxisUnit * child.geometry!.paintExtent; + } + if (child.geometry!.visible) { - final SliverPhysicalParentData childParentData = child.parentData! as SliverPhysicalParentData; - context.paintChild(child, offset + childParentData.paintOffset); + context.paintChild(child, childOffset); } child = childBefore(child); } diff --git a/packages/flutter/test/widgets/sliver_main_axis_group_test.dart b/packages/flutter/test/widgets/sliver_main_axis_group_test.dart index 6ec6787fc7..e638dca79b 100644 --- a/packages/flutter/test/widgets/sliver_main_axis_group_test.dart +++ b/packages/flutter/test/widgets/sliver_main_axis_group_test.dart @@ -32,9 +32,16 @@ void main() { expect(controller.offset, 0); expect(find.text('Group 0 Tile 0'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 0')), + const Rect.fromLTRB(0.0, 0.0, 300.0, 300.0), + ); expect(find.text('Group 0 Tile 1'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 1')), + const Rect.fromLTRB(0.0, 300.0, 300.0, 600.0), + ); expect(find.text('Group 0 Tile 2'), findsNothing); - expect(find.text('Group 1 Tile 0'), findsNothing); const double scrollOffset = 19 * 300.0; @@ -44,7 +51,15 @@ void main() { expect(controller.offset, scrollOffset); expect(find.text('Group 0 Tile 18'), findsNothing); expect(find.text('Group 0 Tile 19'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 19')), + const Rect.fromLTRB(0.0, 0.0, 300.0, 300.0), + ); expect(find.text('Group 1 Tile 0'), findsOneWidget); + expect( + tester.getRect(find.text('Group 1 Tile 0')), + const Rect.fromLTRB(0.0, 300.0, 300.0, 500.0), + ); final List renderSlivers = tester.renderObjectList(find.byType(SliverList)).toList(); final RenderSliverList first = renderSlivers[0]; @@ -85,9 +100,16 @@ void main() { expect(controller.offset, 0); expect(find.text('Group 0 Tile 0'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 0')), + const Rect.fromLTRB(0.0, 300.0, 300.0, 600.0), + ); expect(find.text('Group 0 Tile 1'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 1')), + const Rect.fromLTRB(0.0, 0.0, 300.0, 300.0), + ); expect(find.text('Group 0 Tile 2'), findsNothing); - expect(find.text('Group 1 Tile 0'), findsNothing); const double scrollOffset = 19 * 300.0; @@ -97,7 +119,15 @@ void main() { expect(controller.offset, scrollOffset); expect(find.text('Group 0 Tile 18'), findsNothing); expect(find.text('Group 0 Tile 19'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 19')), + const Rect.fromLTRB(0.0, 0.0, 300.0, 300.0), + ); expect(find.text('Group 1 Tile 0'), findsOneWidget); + expect( + tester.getRect(find.text('Group 1 Tile 0')), + const Rect.fromLTRB(0.0, 400.0, 300.0, 600.0), + ); final List renderSlivers = tester.renderObjectList(find.byType(SliverList)).toList(); final RenderSliverList first = renderSlivers[0]; @@ -138,8 +168,11 @@ void main() { expect(controller.offset, 0); expect(find.text('Group 0 Tile 0'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 0')), + const Rect.fromLTRB(0.0, 0.0, 300.0, 600.0), + ); expect(find.text('Group 0 Tile 1'), findsNothing); - expect(find.text('Group 1 Tile 0'), findsNothing); const double scrollOffset = 19 * 300.0; @@ -149,6 +182,10 @@ void main() { expect(controller.offset, scrollOffset); expect(find.text('Group 0 Tile 18'), findsNothing); expect(find.text('Group 0 Tile 19'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 19')), + const Rect.fromLTRB(0.0, 0.0, 300.0, 600.0), + ); expect(find.text('Group 1 Tile 0'), findsNothing); const double scrollOffset2 = 20 * 300.0; @@ -156,6 +193,10 @@ void main() { await tester.pumpAndSettle(); expect(find.text('Group 0 Tile 19'), findsNothing); expect(find.text('Group 1 Tile 0'), findsOneWidget); + expect( + tester.getRect(find.text('Group 1 Tile 0')), + const Rect.fromLTRB(0.0, 0.0, 200.0, 600.0), + ); final List renderSlivers = tester.renderObjectList(find.byType(SliverList)).toList(); final RenderSliverList first = renderSlivers[0]; @@ -197,8 +238,11 @@ void main() { expect(controller.offset, 0); expect(find.text('Group 0 Tile 0'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 0')), + const Rect.fromLTRB(0.0, 0.0, 300.0, 600.0), + ); expect(find.text('Group 0 Tile 1'), findsNothing); - expect(find.text('Group 1 Tile 0'), findsNothing); const double scrollOffset = 19 * 300.0; @@ -208,6 +252,10 @@ void main() { expect(controller.offset, scrollOffset); expect(find.text('Group 0 Tile 18'), findsNothing); expect(find.text('Group 0 Tile 19'), findsOneWidget); + expect( + tester.getRect(find.text('Group 0 Tile 19')), + const Rect.fromLTRB(0.0, 0.0, 300.0, 600.0), + ); expect(find.text('Group 1 Tile 0'), findsNothing); const double scrollOffset2 = 20 * 300.0; @@ -215,6 +263,10 @@ void main() { await tester.pumpAndSettle(); expect(find.text('Group 0 Tile 19'), findsNothing); expect(find.text('Group 1 Tile 0'), findsOneWidget); + expect( + tester.getRect(find.text('Group 1 Tile 0')), + const Rect.fromLTRB(100.0, 0.0, 300.0, 600.0), + ); final List renderSlivers = tester.renderObjectList(find.byType(SliverList)).toList(); final RenderSliverList first = renderSlivers[0];