@@ -596,10 +596,6 @@ bool _debugEnsureNegativePercent(String name, double? percent) {
596
596
}
597
597
598
598
final ConstraintId parent = ConstraintId ('parent' );
599
- final _ConstrainedNode _parentNode = _ConstrainedNode ()
600
- ..nodeId = parent
601
- ..depth = 0
602
- ..notLaidOut = false ;
603
599
const double matchConstraint = - 3.1415926 ;
604
600
const double matchParent = - 2.7182818 ;
605
601
const double wrapContent = - 0.6180339 ;
@@ -2018,11 +2014,11 @@ class _ConstraintRenderBox extends RenderBox
2018
2014
}
2019
2015
2020
2016
/// 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 ) {
2023
2019
for (final element in nodeList) {
2024
2020
try {
2025
- element.getDepth (selfSizeConfirmed, parentDepth );
2021
+ element.getDepth (selfSizeConfirmed);
2026
2022
} on StackOverflowError catch (_) {
2027
2023
throw ConstraintLayoutException (
2028
2024
'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
2136
2132
}
2137
2133
}
2138
2134
2139
- Map <ConstraintId , _ConstrainedNode > _buildConstrainedNodeTrees () {
2135
+ Map <ConstraintId , _ConstrainedNode > _buildConstrainedNodeTrees (
2136
+ bool selfSizeConfirmed) {
2140
2137
Map <ConstraintId , _ConstrainedNode > nodesMap = HashMap ();
2141
2138
_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 ()
2259
2140
..nodeId = parent
2141
+ ..depth = selfSizeConfirmed ? 0 : childCount + 1
2260
2142
..notLaidOut = false ;
2261
- nodesMap[parent] = parentNode;
2143
+ if (! selfSizeConfirmed) {
2144
+ nodesMap[parent] = parentNode;
2145
+ }
2262
2146
2263
2147
_ConstrainedNode _getConstrainedNodeForChild (ConstraintId id,
2264
2148
[int ? childIndex]) {
@@ -2366,8 +2250,6 @@ class _ConstraintRenderBox extends RenderBox
2366
2250
child = childParentData.nextSibling;
2367
2251
}
2368
2252
2369
- parentNode.depth = childCount + 1 ;
2370
-
2371
2253
return nodesMap;
2372
2254
}
2373
2255
@@ -2447,55 +2329,30 @@ class _ConstraintRenderBox extends RenderBox
2447
2329
}());
2448
2330
2449
2331
/// 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
+ }());
2479
2344
2480
2345
/// Sort by the depth of constraint from shallow to deep, the lowest depth is 0, representing parent
2481
2346
_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! );
2494
2349
}
2350
+ _layoutOrderList.sort ((left, right) {
2351
+ return left.getDepth (selfSizeConfirmed) -
2352
+ right.getDepth (selfSizeConfirmed);
2353
+ });
2495
2354
2496
2355
_paintingOrderList = nodesMap.values.toList ();
2497
- _eventOrderList = nodesMap.values.toList ();
2498
-
2499
2356
_paintingOrderList.sort ((left, right) {
2500
2357
int result = left.zIndex - right.zIndex;
2501
2358
if (result == 0 ) {
@@ -2504,6 +2361,7 @@ class _ConstraintRenderBox extends RenderBox
2504
2361
return result;
2505
2362
});
2506
2363
2364
+ _eventOrderList = nodesMap.values.toList ();
2507
2365
_eventOrderList.sort ((left, right) {
2508
2366
int result = right.eIndex - left.eIndex;
2509
2367
if (result == 0 ) {
@@ -3426,7 +3284,7 @@ class _ConstraintRenderBox extends RenderBox
3426
3284
paragraphBuilder.pushStyle (ui.TextStyle (
3427
3285
color: Colors .black,
3428
3286
));
3429
- paragraphBuilder.addText ("depth ${element .getDepth (null , - 1 )}" );
3287
+ paragraphBuilder.addText ("depth ${element .getDepth (null )}" );
3430
3288
ui.Paragraph paragraph = paragraphBuilder.build ();
3431
3289
paragraph.layout (ui.ParagraphConstraints (
3432
3290
width: element.getMeasuredWidth (),
@@ -3746,8 +3604,8 @@ class _ConstrainedNode {
3746
3604
return baseline;
3747
3605
}
3748
3606
3749
- int getDepthFor (_ConstrainedNode ? constrainedNode, bool ? parentSizeConfirmed,
3750
- int parentDepth ) {
3607
+ int getDepthFor (
3608
+ _ConstrainedNode ? constrainedNode, bool ? parentSizeConfirmed ) {
3751
3609
if (constrainedNode == null ) {
3752
3610
return - 1 ;
3753
3611
}
@@ -3760,26 +3618,26 @@ class _ConstrainedNode {
3760
3618
}
3761
3619
}
3762
3620
}
3763
- return constrainedNode.getDepth (parentSizeConfirmed, parentDepth );
3621
+ return constrainedNode.getDepth (parentSizeConfirmed);
3764
3622
}
3765
3623
3766
- int getDepth (bool ? parentSizeConfirmed, int parentDepth ) {
3624
+ int getDepth (bool ? parentSizeConfirmed) {
3767
3625
if (depth < 0 ) {
3768
3626
if (isBarrier) {
3769
3627
List <int > list = [];
3770
3628
for (final id in referencedIds! ) {
3771
3629
list.add (parentData._constrainedNodeMap[id]!
3772
- .getDepth (parentSizeConfirmed, parentDepth ));
3630
+ .getDepth (parentSizeConfirmed));
3773
3631
}
3774
3632
list.sort ((left, right) => left - right);
3775
3633
depth = list.last + 1 ;
3776
3634
} else {
3777
3635
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),
3783
3641
];
3784
3642
list.sort ((left, right) => left - right);
3785
3643
depth = list.last + 1 ;
@@ -3858,7 +3716,7 @@ class _ConstrainedNode {
3858
3716
}
3859
3717
}
3860
3718
}
3861
- map['depth' ] = getDepth (null , - 1 );
3719
+ map['depth' ] = getDepth (null );
3862
3720
return map;
3863
3721
}
3864
3722
}
0 commit comments