Skip to content

Commit 5008138

Browse files
mbcoronaPiinks
authored andcommitted
Fix TreeSliver first node clipping during expand/collapse animation (flutter#188305)
1 parent 6bfd8f4 commit 5008138

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,14 @@ class RenderTreeSliver extends RenderSliverVariedExtentList {
371371
// leadingIndex), and the trailing edge of the trailing index. We cannot
372372
// rely on the leading edge of the leading index, because it is currently
373373
// moving.
374+
//
375+
// parentIndex is always a real animating node (the unclipped first
376+
// segment is already painted), so its extent is added even when it is
377+
// index 0. Otherwise the clip starts at the parent's leading edge and its
378+
// children paint over it. See https://github.com/flutter/flutter/issues/188305.
374379
final int parentIndex = math.max(segment.leadingIndex - 1, 0);
375380
final double leadingOffset =
376-
indexToLayoutOffset(0.0, parentIndex) +
377-
(parentIndex == 0 ? 0.0 : itemExtentBuilder(parentIndex, layoutDimensions)!);
381+
indexToLayoutOffset(0.0, parentIndex) + itemExtentBuilder(parentIndex, layoutDimensions)!;
378382
final double trailingOffset =
379383
indexToLayoutOffset(0.0, segment.trailingIndex) +
380384
itemExtentBuilder(segment.trailingIndex, layoutDimensions)!;

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,4 +1006,110 @@ void main() {
10061006
await expectLater(find.byKey(key), matchesGoldenFile('sliver_tree.scrolling.1.png'));
10071007
expect(tester.getTopLeft(find.byType(ColoredBox)), const Offset(0, -5));
10081008
});
1009+
1010+
// Regression test for https://github.com/flutter/flutter/issues/188305.
1011+
// While a node expands, its children must be clipped beneath the node (at the
1012+
// node's trailing edge) - including the first node, which used to clip at its
1013+
// leading edge and let its children paint over it.
1014+
group('Animating node clips children below its trailing edge', () {
1015+
const rowExtent = 40.0;
1016+
const animationDuration = Duration(seconds: 1);
1017+
1018+
// The top edge of every clip rect the sliver paints.
1019+
List<double> recordedClipTops(RenderObject renderObject) {
1020+
final tops = <double>[];
1021+
final canvas = TestRecordingCanvas();
1022+
final context = TestRecordingPaintingContext(canvas);
1023+
renderObject.paint(context, Offset.zero);
1024+
for (final RecordedInvocation recorded in canvas.invocations) {
1025+
if (recorded.invocation.memberName == #clipRect) {
1026+
tops.add((recorded.invocation.positionalArguments[0] as Rect).top);
1027+
}
1028+
}
1029+
return tops;
1030+
}
1031+
1032+
Widget buildTree(TreeSliverController controller) {
1033+
final tree = <TreeSliverNode<String>>[
1034+
TreeSliverNode<String>(
1035+
'First',
1036+
children: <TreeSliverNode<String>>[
1037+
TreeSliverNode<String>('First:0'),
1038+
TreeSliverNode<String>('First:1'),
1039+
],
1040+
),
1041+
TreeSliverNode<String>(
1042+
'Second',
1043+
children: <TreeSliverNode<String>>[
1044+
TreeSliverNode<String>('Second:0'),
1045+
TreeSliverNode<String>('Second:1'),
1046+
],
1047+
),
1048+
];
1049+
return Directionality(
1050+
textDirection: TextDirection.ltr,
1051+
child: CustomScrollView(
1052+
slivers: <Widget>[
1053+
TreeSliver<String>(
1054+
tree: tree,
1055+
controller: controller,
1056+
treeRowExtentBuilder: (_, _) => rowExtent,
1057+
toggleAnimationStyle: const AnimationStyle(
1058+
curve: Curves.linear,
1059+
duration: animationDuration,
1060+
),
1061+
treeNodeBuilder:
1062+
(
1063+
BuildContext context,
1064+
TreeSliverNode<Object?> node,
1065+
AnimationStyle animationStyle,
1066+
) {
1067+
return GestureDetector(
1068+
behavior: HitTestBehavior.translucent,
1069+
onTap: () => controller.toggleNode(node),
1070+
child: TreeSliver.defaultTreeNodeBuilder(context, node, animationStyle),
1071+
);
1072+
},
1073+
),
1074+
],
1075+
),
1076+
);
1077+
}
1078+
1079+
testWidgets('first node (index 0) clips at its trailing edge', (WidgetTester tester) async {
1080+
final controller = TreeSliverController();
1081+
await tester.pumpWidget(buildTree(controller));
1082+
1083+
await tester.tap(find.text('First'));
1084+
await tester.pump();
1085+
await tester.pump(animationDuration ~/ 2); // Mid-animation.
1086+
1087+
final RenderTreeSliver renderTree = tester.allRenderObjects
1088+
.whereType<RenderTreeSliver>()
1089+
.first;
1090+
final List<double> clipTops = recordedClipTops(renderTree);
1091+
// One row down (the node's trailing edge), not at the top (0.0).
1092+
expect(clipTops, isNotEmpty);
1093+
expect(clipTops, contains(rowExtent));
1094+
expect(clipTops, isNot(contains(0.0)));
1095+
});
1096+
1097+
testWidgets('non-first node (index 1) clips at its trailing edge', (WidgetTester tester) async {
1098+
final controller = TreeSliverController();
1099+
await tester.pumpWidget(buildTree(controller));
1100+
1101+
await tester.tap(find.text('Second'));
1102+
await tester.pump();
1103+
await tester.pump(animationDuration ~/ 2); // Mid-animation.
1104+
1105+
final RenderTreeSliver renderTree = tester.allRenderObjects
1106+
.whereType<RenderTreeSliver>()
1107+
.first;
1108+
final List<double> clipTops = recordedClipTops(renderTree);
1109+
// The second node is at index 1, so its trailing edge is two rows down.
1110+
expect(clipTops, isNotEmpty);
1111+
expect(clipTops, contains(rowExtent * 2));
1112+
expect(clipTops, isNot(contains(0.0)));
1113+
});
1114+
});
10091115
}

0 commit comments

Comments
 (0)