Skip to content

Commit a5ad088

Browse files
authored
Fix assertion failure when reordering two dimensional children (flutter#141504)
It fixes assertion failure due to unstable state of children list during reordering in `RenderTwoDimensionalViewport.parentDataOf`. This changes the assertion to check debug orphan list and `keepAlive` bucket in addition to children list to determine whether child belongs to this render object or not. - Fixes flutter#141101
1 parent aaa3eba commit a5ad088

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

packages/flutter/lib/src/widgets/two_dimensional_viewport.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,11 @@ abstract class RenderTwoDimensionalViewport extends RenderBox implements RenderA
779779
/// Children must have a [ParentData] of type
780780
/// [TwoDimensionalViewportParentData], or a subclass thereof.
781781
@protected
782+
@mustCallSuper
782783
TwoDimensionalViewportParentData parentDataOf(RenderBox child) {
783-
assert(_children.containsValue(child));
784+
assert(_children.containsValue(child) ||
785+
_keepAliveBucket.containsValue(child) ||
786+
_debugOrphans!.contains(child));
784787
return child.parentData! as TwoDimensionalViewportParentData;
785788
}
786789

packages/flutter/test/widgets/two_dimensional_utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class RenderSimpleBuilderTableViewport extends RenderTwoDimensionalViewport {
195195

196196
@override
197197
TestExtendedParentData parentDataOf(RenderBox child) {
198-
return child.parentData! as TestExtendedParentData;
198+
return super.parentDataOf(child) as TestExtendedParentData;
199199
}
200200

201201
@override

packages/flutter/test/widgets/two_dimensional_viewport_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,6 +2711,54 @@ void main() {
27112711
);
27122712
});
27132713
});
2714+
2715+
testWidgets('correctly reorders children and wont throw assertion failure',
2716+
(WidgetTester tester) async {
2717+
final TwoDimensionalChildBuilderDelegate delegate1 =
2718+
TwoDimensionalChildBuilderDelegate(
2719+
maxXIndex: 5,
2720+
maxYIndex: 5,
2721+
addAutomaticKeepAlives: false,
2722+
addRepaintBoundaries: false,
2723+
builder: (BuildContext context, ChildVicinity vicinity) {
2724+
ValueKey<int>? key;
2725+
if (vicinity == const ChildVicinity(xIndex: 1, yIndex: 1)) {
2726+
key = const ValueKey<int>(1);
2727+
} else if (vicinity ==
2728+
const ChildVicinity(xIndex: 1, yIndex: 2)) {
2729+
key = const ValueKey<int>(2);
2730+
}
2731+
return SizedBox.square(key: key, dimension: 200);
2732+
});
2733+
final TwoDimensionalChildBuilderDelegate delegate2 =
2734+
TwoDimensionalChildBuilderDelegate(
2735+
maxXIndex: 5,
2736+
maxYIndex: 5,
2737+
addAutomaticKeepAlives: false,
2738+
addRepaintBoundaries: false,
2739+
builder: (BuildContext context, ChildVicinity vicinity) {
2740+
ValueKey<int>? key;
2741+
if (vicinity == const ChildVicinity(xIndex: 0, yIndex: 0)) {
2742+
key = const ValueKey<int>(1);
2743+
} else if (vicinity ==
2744+
const ChildVicinity(xIndex: 1, yIndex: 1)) {
2745+
key = const ValueKey<int>(2);
2746+
}
2747+
return SizedBox.square(key: key, dimension: 200);
2748+
});
2749+
addTearDown(delegate1.dispose);
2750+
addTearDown(delegate2.dispose);
2751+
2752+
await tester.pumpWidget(simpleBuilderTest(delegate: delegate1));
2753+
expect(tester.getRect(find.byKey(const ValueKey<int>(1))),
2754+
const Rect.fromLTWH(200.0, 200.0, 200.0, 200.0));
2755+
await tester.pumpWidget(simpleBuilderTest(delegate: delegate2));
2756+
expect(tester.getRect(find.byKey(const ValueKey<int>(1))),
2757+
const Rect.fromLTWH(0.0, 0.0, 200.0, 200.0));
2758+
await tester.pumpWidget(simpleBuilderTest(delegate: delegate1));
2759+
expect(tester.getRect(find.byKey(const ValueKey<int>(1))),
2760+
const Rect.fromLTWH(200.0, 200.0, 200.0, 200.0));
2761+
}, variant: TargetPlatformVariant.all());
27142762
});
27152763
}
27162764

0 commit comments

Comments
 (0)