Skip to content

Commit 0811371

Browse files
committed
fix depth calcalute in self wrapContent mode
1 parent 572d7c4 commit 0811371

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

lib/src/constraint_layout.dart

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,11 +2018,11 @@ class _ConstraintRenderBox extends RenderBox
20182018
}
20192019

20202020
/// There should be no loop constraints
2021-
static void _debugCheckLoopConstraints(
2022-
List<_ConstrainedNode> nodeList, bool selfSizeConfirmed) {
2021+
static void _debugCheckLoopConstraints(List<_ConstrainedNode> nodeList,
2022+
bool selfSizeConfirmed, int parentDepth) {
20232023
for (final element in nodeList) {
20242024
try {
2025-
element.getDepth(selfSizeConfirmed);
2025+
element.getDepth(selfSizeConfirmed, parentDepth);
20262026
} on StackOverflowError catch (_) {
20272027
throw ConstraintLayoutException(
20282028
'There are some loop constraints, please check the code. For layout performance considerations, constraints are always one-way, and there should be no two child elements directly or indirectly restrain each other. Each constraint should describe exactly where the child elements are located. Use Guideline to break loop constraints.');
@@ -2454,7 +2454,7 @@ class _ConstraintRenderBox extends RenderBox
24542454
if (_debugCheckConstraints) {
24552455
List<_ConstrainedNode> nodeList = nodesMap.values.toList();
24562456
_debugCheckConstraintsIntegrity(nodeList);
2457-
_debugCheckLoopConstraints(nodeList, true);
2457+
_debugCheckLoopConstraints(nodeList, true, 0);
24582458
}
24592459
return true;
24602460
}());
@@ -2463,28 +2463,34 @@ class _ConstraintRenderBox extends RenderBox
24632463
assert(() {
24642464
if (_debugCheckConstraints) {
24652465
List<_ConstrainedNode> nodeList = nodesMap.values.toList();
2466+
late _ConstrainedNode parentNode;
24662467
for (int i = 0; i < nodeList.length; i++) {
24672468
if (nodeList[i].isParent()) {
2468-
nodeList.removeAt(i);
2469+
parentNode = nodeList.removeAt(i);
24692470
break;
24702471
}
24712472
}
24722473
_debugCheckConstraintsIntegrity(nodeList);
2473-
_debugCheckLoopConstraints(nodeList, false);
2474+
_debugCheckLoopConstraints(nodeList, false, parentNode.depth);
24742475
}
24752476
return true;
24762477
}());
24772478
}
24782479

24792480
/// Sort by the depth of constraint from shallow to deep, the lowest depth is 0, representing parent
24802481
_layoutOrderList = nodesMap.values.toList();
2481-
_layoutOrderList.sort((left, right) {
2482-
return left.getDepth(selfSizeConfirmed) -
2483-
right.getDepth(selfSizeConfirmed);
2484-
});
24852482

2486-
if (!selfSizeConfirmed) {
2487-
nodesMap.remove(parent);
2483+
if (selfSizeConfirmed) {
2484+
_layoutOrderList.sort((left, right) {
2485+
return left.getDepth(selfSizeConfirmed, 0) -
2486+
right.getDepth(selfSizeConfirmed, 0);
2487+
});
2488+
} else {
2489+
_ConstrainedNode parentNode = nodesMap.remove(parent)!;
2490+
_layoutOrderList.sort((left, right) {
2491+
return left.getDepth(selfSizeConfirmed, parentNode.depth) -
2492+
right.getDepth(selfSizeConfirmed, parentNode.depth);
2493+
});
24882494
}
24892495

24902496
_paintingOrderList = nodesMap.values.toList();
@@ -3348,7 +3354,7 @@ class _ConstraintRenderBox extends RenderBox
33483354
paragraphBuilder.pushStyle(ui.TextStyle(
33493355
color: Colors.black,
33503356
));
3351-
paragraphBuilder.addText("depth ${element.getDepth(null)}");
3357+
paragraphBuilder.addText("depth ${element.getDepth(null, -1)}");
33523358
ui.Paragraph paragraph = paragraphBuilder.build();
33533359
paragraph.layout(ui.ParagraphConstraints(
33543360
width: element.getMeasuredWidth(),
@@ -3666,8 +3672,8 @@ class _ConstrainedNode {
36663672
return baseline;
36673673
}
36683674

3669-
int getDepthFor(
3670-
_ConstrainedNode? constrainedNode, bool? parentSizeConfirmed) {
3675+
int getDepthFor(_ConstrainedNode? constrainedNode, bool? parentSizeConfirmed,
3676+
int parentDepth) {
36713677
if (constrainedNode == null) {
36723678
return -1;
36733679
}
@@ -3680,29 +3686,34 @@ class _ConstrainedNode {
36803686
}
36813687
}
36823688
}
3683-
return constrainedNode.getDepth(parentSizeConfirmed);
3689+
return constrainedNode.getDepth(parentSizeConfirmed, parentDepth);
36843690
}
36853691

3686-
int getDepth(bool? parentSizeConfirmed) {
3692+
int getDepth(bool? parentSizeConfirmed, int parentDepth) {
36873693
if (depth < 0) {
36883694
if (isBarrier) {
36893695
List<int> list = [];
36903696
for (final id in referencedIds!) {
36913697
list.add(parentData._constrainedNodeMap[id]!
3692-
.getDepth(parentSizeConfirmed));
3698+
.getDepth(parentSizeConfirmed, parentDepth));
36933699
}
36943700
list.sort((left, right) => left - right);
36953701
depth = list.last + 1;
36963702
} else {
36973703
List<int> list = [
3698-
getDepthFor(leftConstraint, parentSizeConfirmed),
3699-
getDepthFor(topConstraint, parentSizeConfirmed),
3700-
getDepthFor(rightConstraint, parentSizeConfirmed),
3701-
getDepthFor(bottomConstraint, parentSizeConfirmed),
3702-
getDepthFor(baselineConstraint, parentSizeConfirmed),
3704+
getDepthFor(leftConstraint, parentSizeConfirmed, parentDepth),
3705+
getDepthFor(topConstraint, parentSizeConfirmed, parentDepth),
3706+
getDepthFor(rightConstraint, parentSizeConfirmed, parentDepth),
3707+
getDepthFor(bottomConstraint, parentSizeConfirmed, parentDepth),
3708+
getDepthFor(baselineConstraint, parentSizeConfirmed, parentDepth),
37033709
];
37043710
list.sort((left, right) => left - right);
37053711
depth = list.last + 1;
3712+
if (parentSizeConfirmed == false) {
3713+
if (width == matchConstraint || height == matchConstraint) {
3714+
depth = max(depth, parentDepth + 1);
3715+
}
3716+
}
37063717
}
37073718
}
37083719
return depth;
@@ -3778,7 +3789,7 @@ class _ConstrainedNode {
37783789
}
37793790
}
37803791
}
3781-
map['depth'] = getDepth(null);
3792+
map['depth'] = getDepth(null, -1);
37823793
return map;
37833794
}
37843795
}

0 commit comments

Comments
 (0)