Skip to content

Commit 5debaf2

Browse files
author
fbchen
committed
optimize code
1 parent 741a9ce commit 5debaf2

File tree

1 file changed

+41
-183
lines changed

1 file changed

+41
-183
lines changed

lib/src/constraint_layout.dart

Lines changed: 41 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,6 @@ bool _debugEnsureNegativePercent(String name, double? percent) {
596596
}
597597

598598
final ConstraintId parent = ConstraintId('parent');
599-
final _ConstrainedNode _parentNode = _ConstrainedNode()
600-
..nodeId = parent
601-
..depth = 0
602-
..notLaidOut = false;
603599
const double matchConstraint = -3.1415926;
604600
const double matchParent = -2.7182818;
605601
const double wrapContent = -0.6180339;
@@ -2018,11 +2014,11 @@ class _ConstraintRenderBox extends RenderBox
20182014
}
20192015

20202016
/// There should be no loop constraints
2021-
static void _debugCheckLoopConstraints(List<_ConstrainedNode> nodeList,
2022-
bool selfSizeConfirmed, int parentDepth) {
2017+
static void _debugCheckLoopConstraints(
2018+
List<_ConstrainedNode> nodeList, bool selfSizeConfirmed) {
20232019
for (final element in nodeList) {
20242020
try {
2025-
element.getDepth(selfSizeConfirmed, parentDepth);
2021+
element.getDepth(selfSizeConfirmed);
20262022
} on StackOverflowError catch (_) {
20272023
throw ConstraintLayoutException(
20282024
'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.');
@@ -2136,129 +2132,17 @@ class _ConstraintRenderBox extends RenderBox
21362132
}
21372133
}
21382134

2139-
Map<ConstraintId, _ConstrainedNode> _buildConstrainedNodeTrees() {
2135+
Map<ConstraintId, _ConstrainedNode> _buildConstrainedNodeTrees(
2136+
bool selfSizeConfirmed) {
21402137
Map<ConstraintId, _ConstrainedNode> nodesMap = HashMap();
21412138
_buildNodeTreesCount++;
2142-
2143-
_ConstrainedNode _getConstrainedNodeForChild(ConstraintId id,
2144-
[int? childIndex]) {
2145-
if (id == parent) {
2146-
return _parentNode;
2147-
}
2148-
2149-
if (id is RelativeConstraintId) {
2150-
int targetIndex = childIndex! + id.siblingIndexOffset;
2151-
id = IndexConstraintId(targetIndex);
2152-
}
2153-
2154-
/// Fewer reads to nodesMap for faster constraint building
2155-
_ConstrainedNode? node = id.getCacheNode(_buildNodeTreesCount ^ hashCode);
2156-
if (node != null) {
2157-
return node;
2158-
}
2159-
2160-
node = nodesMap[id];
2161-
if (node == null) {
2162-
node = _ConstrainedNode()..nodeId = id;
2163-
nodesMap[id] = node;
2164-
}
2165-
id.setCacheNode(_buildNodeTreesCount ^ hashCode, node);
2166-
return node;
2167-
}
2168-
2169-
if (_helperNodeMap.isNotEmpty) {
2170-
nodesMap.addAll(_helperNodeMap);
2171-
for (final element in _helperNodeMap.values) {
2172-
if (element.parentData.left != null) {
2173-
element.leftConstraint =
2174-
_getConstrainedNodeForChild(element.parentData.left!.id!);
2175-
element.leftAlignType = element.parentData.left!.type;
2176-
}
2177-
2178-
if (element.parentData.top != null) {
2179-
element.topConstraint =
2180-
_getConstrainedNodeForChild(element.parentData.top!.id!);
2181-
element.topAlignType = element.parentData.top!.type;
2182-
}
2183-
2184-
if (element.parentData.right != null) {
2185-
element.rightConstraint =
2186-
_getConstrainedNodeForChild(element.parentData.right!.id!);
2187-
element.rightAlignType = element.parentData.right!.type;
2188-
}
2189-
2190-
if (element.parentData.bottom != null) {
2191-
element.bottomConstraint =
2192-
_getConstrainedNodeForChild(element.parentData.bottom!.id!);
2193-
element.bottomAlignType = element.parentData.bottom!.type;
2194-
}
2195-
2196-
if (element.isBarrier) {
2197-
element.parentData._constrainedNodeMap = nodesMap;
2198-
}
2199-
}
2200-
}
2201-
2202-
RenderBox? child = firstChild;
2203-
int childIndex = -1;
2204-
while (child != null) {
2205-
childIndex++;
2206-
_ConstraintBoxData childParentData =
2207-
child.parentData as _ConstraintBoxData;
2208-
childParentData._constrainedNodeMap = nodesMap;
2209-
2210-
_ConstrainedNode currentNode = _getConstrainedNodeForChild(
2211-
childParentData.id ?? IndexConstraintId(childIndex));
2212-
currentNode.parentData = childParentData;
2213-
currentNode.index = childIndex;
2214-
currentNode.renderBox = child;
2215-
2216-
if (childParentData.left != null) {
2217-
currentNode.leftConstraint =
2218-
_getConstrainedNodeForChild(childParentData.left!.id!, childIndex);
2219-
currentNode.leftAlignType = childParentData.left!.type;
2220-
}
2221-
2222-
if (childParentData.top != null) {
2223-
currentNode.topConstraint =
2224-
_getConstrainedNodeForChild(childParentData.top!.id!, childIndex);
2225-
currentNode.topAlignType = childParentData.top!.type;
2226-
}
2227-
2228-
if (childParentData.right != null) {
2229-
currentNode.rightConstraint =
2230-
_getConstrainedNodeForChild(childParentData.right!.id!, childIndex);
2231-
currentNode.rightAlignType = childParentData.right!.type;
2232-
}
2233-
2234-
if (childParentData.bottom != null) {
2235-
currentNode.bottomConstraint = _getConstrainedNodeForChild(
2236-
childParentData.bottom!.id!, childIndex);
2237-
currentNode.bottomAlignType = childParentData.bottom!.type;
2238-
}
2239-
2240-
if (childParentData.baseline != null) {
2241-
currentNode.baselineConstraint = _getConstrainedNodeForChild(
2242-
childParentData.baseline!.id!, childIndex);
2243-
currentNode.baselineAlignType = childParentData.baseline!.type;
2244-
}
2245-
2246-
child = childParentData.nextSibling;
2247-
}
2248-
2249-
nodesMap.remove(parent);
2250-
2251-
return nodesMap;
2252-
}
2253-
2254-
Map<ConstraintId, _ConstrainedNode>
2255-
_buildConstrainedNodeTreesForWrapContent() {
2256-
Map<ConstraintId, _ConstrainedNode> nodesMap = HashMap();
2257-
_buildNodeTreesCount++;
2258-
final _ConstrainedNode parentNode = _ConstrainedNode()
2139+
_ConstrainedNode parentNode = _ConstrainedNode()
22592140
..nodeId = parent
2141+
..depth = selfSizeConfirmed ? 0 : childCount + 1
22602142
..notLaidOut = false;
2261-
nodesMap[parent] = parentNode;
2143+
if (!selfSizeConfirmed) {
2144+
nodesMap[parent] = parentNode;
2145+
}
22622146

22632147
_ConstrainedNode _getConstrainedNodeForChild(ConstraintId id,
22642148
[int? childIndex]) {
@@ -2366,8 +2250,6 @@ class _ConstraintRenderBox extends RenderBox
23662250
child = childParentData.nextSibling;
23672251
}
23682252

2369-
parentNode.depth = childCount + 1;
2370-
23712253
return nodesMap;
23722254
}
23732255

@@ -2447,55 +2329,30 @@ class _ConstraintRenderBox extends RenderBox
24472329
}());
24482330

24492331
/// Traverse once, building the constrained node tree for each child element
2450-
Map<ConstraintId, _ConstrainedNode> nodesMap;
2451-
if (selfSizeConfirmed) {
2452-
nodesMap = _buildConstrainedNodeTrees();
2453-
assert(() {
2454-
if (_debugCheckConstraints) {
2455-
List<_ConstrainedNode> nodeList = nodesMap.values.toList();
2456-
_debugCheckConstraintsIntegrity(nodeList);
2457-
_debugCheckLoopConstraints(nodeList, true, 0);
2458-
}
2459-
return true;
2460-
}());
2461-
} else {
2462-
nodesMap = _buildConstrainedNodeTreesForWrapContent();
2463-
assert(() {
2464-
if (_debugCheckConstraints) {
2465-
List<_ConstrainedNode> nodeList = nodesMap.values.toList();
2466-
late _ConstrainedNode parentNode;
2467-
for (int i = 0; i < nodeList.length; i++) {
2468-
if (nodeList[i].isParent()) {
2469-
parentNode = nodeList.removeAt(i);
2470-
break;
2471-
}
2472-
}
2473-
_debugCheckConstraintsIntegrity(nodeList);
2474-
_debugCheckLoopConstraints(nodeList, false, parentNode.depth);
2475-
}
2476-
return true;
2477-
}());
2478-
}
2332+
Map<ConstraintId, _ConstrainedNode> nodesMap =
2333+
_buildConstrainedNodeTrees(selfSizeConfirmed);
2334+
_ConstrainedNode? parentNode = nodesMap.remove(parent);
2335+
2336+
assert(() {
2337+
if (_debugCheckConstraints) {
2338+
List<_ConstrainedNode> nodeList = nodesMap.values.toList();
2339+
_debugCheckConstraintsIntegrity(nodeList);
2340+
_debugCheckLoopConstraints(nodeList, selfSizeConfirmed);
2341+
}
2342+
return true;
2343+
}());
24792344

24802345
/// Sort by the depth of constraint from shallow to deep, the lowest depth is 0, representing parent
24812346
_layoutOrderList = nodesMap.values.toList();
2482-
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-
});
2347+
if (!selfSizeConfirmed) {
2348+
_layoutOrderList.add(parentNode!);
24942349
}
2350+
_layoutOrderList.sort((left, right) {
2351+
return left.getDepth(selfSizeConfirmed) -
2352+
right.getDepth(selfSizeConfirmed);
2353+
});
24952354

24962355
_paintingOrderList = nodesMap.values.toList();
2497-
_eventOrderList = nodesMap.values.toList();
2498-
24992356
_paintingOrderList.sort((left, right) {
25002357
int result = left.zIndex - right.zIndex;
25012358
if (result == 0) {
@@ -2504,6 +2361,7 @@ class _ConstraintRenderBox extends RenderBox
25042361
return result;
25052362
});
25062363

2364+
_eventOrderList = nodesMap.values.toList();
25072365
_eventOrderList.sort((left, right) {
25082366
int result = right.eIndex - left.eIndex;
25092367
if (result == 0) {
@@ -3426,7 +3284,7 @@ class _ConstraintRenderBox extends RenderBox
34263284
paragraphBuilder.pushStyle(ui.TextStyle(
34273285
color: Colors.black,
34283286
));
3429-
paragraphBuilder.addText("depth ${element.getDepth(null, -1)}");
3287+
paragraphBuilder.addText("depth ${element.getDepth(null)}");
34303288
ui.Paragraph paragraph = paragraphBuilder.build();
34313289
paragraph.layout(ui.ParagraphConstraints(
34323290
width: element.getMeasuredWidth(),
@@ -3746,8 +3604,8 @@ class _ConstrainedNode {
37463604
return baseline;
37473605
}
37483606

3749-
int getDepthFor(_ConstrainedNode? constrainedNode, bool? parentSizeConfirmed,
3750-
int parentDepth) {
3607+
int getDepthFor(
3608+
_ConstrainedNode? constrainedNode, bool? parentSizeConfirmed) {
37513609
if (constrainedNode == null) {
37523610
return -1;
37533611
}
@@ -3760,26 +3618,26 @@ class _ConstrainedNode {
37603618
}
37613619
}
37623620
}
3763-
return constrainedNode.getDepth(parentSizeConfirmed, parentDepth);
3621+
return constrainedNode.getDepth(parentSizeConfirmed);
37643622
}
37653623

3766-
int getDepth(bool? parentSizeConfirmed, int parentDepth) {
3624+
int getDepth(bool? parentSizeConfirmed) {
37673625
if (depth < 0) {
37683626
if (isBarrier) {
37693627
List<int> list = [];
37703628
for (final id in referencedIds!) {
37713629
list.add(parentData._constrainedNodeMap[id]!
3772-
.getDepth(parentSizeConfirmed, parentDepth));
3630+
.getDepth(parentSizeConfirmed));
37733631
}
37743632
list.sort((left, right) => left - right);
37753633
depth = list.last + 1;
37763634
} else {
37773635
List<int> list = [
3778-
getDepthFor(leftConstraint, parentSizeConfirmed, parentDepth),
3779-
getDepthFor(topConstraint, parentSizeConfirmed, parentDepth),
3780-
getDepthFor(rightConstraint, parentSizeConfirmed, parentDepth),
3781-
getDepthFor(bottomConstraint, parentSizeConfirmed, parentDepth),
3782-
getDepthFor(baselineConstraint, parentSizeConfirmed, parentDepth),
3636+
getDepthFor(leftConstraint, parentSizeConfirmed),
3637+
getDepthFor(topConstraint, parentSizeConfirmed),
3638+
getDepthFor(rightConstraint, parentSizeConfirmed),
3639+
getDepthFor(bottomConstraint, parentSizeConfirmed),
3640+
getDepthFor(baselineConstraint, parentSizeConfirmed),
37833641
];
37843642
list.sort((left, right) => left - right);
37853643
depth = list.last + 1;
@@ -3858,7 +3716,7 @@ class _ConstrainedNode {
38583716
}
38593717
}
38603718
}
3861-
map['depth'] = getDepth(null, -1);
3719+
map['depth'] = getDepth(null);
38623720
return map;
38633721
}
38643722
}

0 commit comments

Comments
 (0)