Skip to content

Commit 9cea82c

Browse files
committed
fix barrier offset
1 parent 491e969 commit 9cea82c

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

lib/src/constraint_layout.dart

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,6 @@ class _ConstraintRenderBox extends RenderBox
13621362

13631363
bool _needsRecalculateConstraints = true;
13641364
bool _needsReorderChildren = true;
1365-
final Map<ConstraintId, _ConstrainedNode> _constrainedNodeMap = HashMap();
13661365

13671366
/// For layout
13681367
late List<_ConstrainedNode> _layoutOrderList;
@@ -1651,33 +1650,34 @@ class _ConstraintRenderBox extends RenderBox
16511650
}
16521651
}
16531652

1654-
_ConstrainedNode _getConstrainedNodeForChild(
1655-
RenderBox? child,
1656-
ConstraintId id,
1657-
) {
1658-
_ConstrainedNode node = _constrainedNodeMap.putIfAbsent(
1659-
id, () => _ConstrainedNode()..nodeId = id);
1660-
if (child != null && node.renderBox == null) {
1661-
node.renderBox = child;
1662-
}
1663-
return node;
1664-
}
1653+
Map<ConstraintId, _ConstrainedNode> _buildConstrainedNodeTrees() {
1654+
Map<ConstraintId, _ConstrainedNode> nodesMap = HashMap();
16651655

1666-
void _buildConstrainedNodeTrees() {
1667-
_constrainedNodeMap.clear();
16681656
List<_ConstrainedNode>? _nodes;
16691657
if (_useCacheConstraints) {
16701658
_nodes = [];
16711659
_childConstraintsCache!._nodesCache[_cacheKey] = _nodes;
16721660
}
16731661

1662+
_ConstrainedNode _getConstrainedNodeForChild(
1663+
RenderBox? child,
1664+
ConstraintId id,
1665+
) {
1666+
_ConstrainedNode node =
1667+
nodesMap.putIfAbsent(id, () => _ConstrainedNode()..nodeId = id);
1668+
if (child != null && node.renderBox == null) {
1669+
node.renderBox = child;
1670+
}
1671+
return node;
1672+
}
1673+
16741674
RenderBox? child = firstChild;
16751675
int childIndex = -1;
16761676
while (child != null) {
16771677
childIndex++;
16781678
_ConstraintBoxData childParentData =
16791679
child.parentData as _ConstraintBoxData;
1680-
childParentData._constrainedNodeMap = _constrainedNodeMap;
1680+
childParentData._constrainedNodeMap = nodesMap;
16811681

16821682
_ConstrainedNode currentNode = _getConstrainedNodeForChild(
16831683
child,
@@ -1722,8 +1722,10 @@ class _ConstraintRenderBox extends RenderBox
17221722
child = childParentData.nextSibling;
17231723
}
17241724

1725-
_constrainedNodeMap.remove(parent);
1726-
_childConstraintsCache?._nodesMapCache[_cacheKey] = _constrainedNodeMap;
1725+
nodesMap.remove(parent);
1726+
_childConstraintsCache?._nodesMapCache[_cacheKey] = nodesMap;
1727+
1728+
return nodesMap;
17271729
}
17281730

17291731
@override
@@ -1861,21 +1863,21 @@ class _ConstraintRenderBox extends RenderBox
18611863
_needsRecalculateConstraints = false;
18621864
} else {
18631865
/// Traverse once, building the constrained node tree for each child element
1864-
_buildConstrainedNodeTrees();
1866+
Map<ConstraintId, _ConstrainedNode> nodesMap =
1867+
_buildConstrainedNodeTrees();
18651868

18661869
assert(() {
18671870
if (_debugCheckConstraints) {
1868-
List<_ConstrainedNode> nodeList =
1869-
_constrainedNodeMap.values.toList();
1871+
List<_ConstrainedNode> nodeList = nodesMap.values.toList();
18701872
_debugCheckConstraintsIntegrity(nodeList);
18711873
_debugCheckLoopConstraints(nodeList);
18721874
}
18731875
return true;
18741876
}());
18751877

18761878
/// Sort by the depth of constraint from shallow to deep, the lowest depth is 0, representing parent
1877-
_layoutOrderList = _constrainedNodeMap.values.toList();
1878-
_paintingOrderList = _constrainedNodeMap.values.toList();
1879+
_layoutOrderList = nodesMap.values.toList();
1880+
_paintingOrderList = nodesMap.values.toList();
18791881

18801882
_layoutOrderList.sort((left, right) {
18811883
return left.getDepth() - right.getDepth();
@@ -2220,13 +2222,15 @@ class _ConstraintRenderBox extends RenderBox
22202222
List<double> list = [];
22212223
for (final id in element.referencedIds!) {
22222224
if (direction == BarrierDirection.left) {
2223-
list.add(_constrainedNodeMap[id]!.getX());
2225+
list.add(element.parentData._constrainedNodeMap[id]!.getX());
22242226
} else if (direction == BarrierDirection.top) {
2225-
list.add(_constrainedNodeMap[id]!.getY());
2227+
list.add(element.parentData._constrainedNodeMap[id]!.getY());
22262228
} else if (direction == BarrierDirection.right) {
2227-
list.add(_constrainedNodeMap[id]!.getRight(size));
2229+
list.add(
2230+
element.parentData._constrainedNodeMap[id]!.getRight(size));
22282231
} else {
2229-
list.add(_constrainedNodeMap[id]!.getBottom(size));
2232+
list.add(
2233+
element.parentData._constrainedNodeMap[id]!.getBottom(size));
22302234
}
22312235
}
22322236
list.sort((left, right) {

0 commit comments

Comments
 (0)