@@ -823,19 +823,23 @@ class Constraint {
823
823
bool needsLayout = false ;
824
824
bool needsPaint = false ;
825
825
bool needsReorderChildren = false ;
826
+ bool needsRecalculateConstraints = false ;
826
827
827
828
if (parentData.id != id) {
828
829
parentData.id = id;
830
+ needsRecalculateConstraints = true ;
829
831
needsLayout = true ;
830
832
}
831
833
832
834
if (parentData.width != width) {
833
835
parentData.width = width;
836
+ needsRecalculateConstraints = true ;
834
837
needsLayout = true ;
835
838
}
836
839
837
840
if (parentData.height != height) {
838
841
parentData.height = height;
842
+ needsRecalculateConstraints = true ;
839
843
needsLayout = true ;
840
844
}
841
845
@@ -867,26 +871,31 @@ class Constraint {
867
871
868
872
if (parentData.left != left) {
869
873
parentData.left = left;
874
+ needsRecalculateConstraints = true ;
870
875
needsLayout = true ;
871
876
}
872
877
873
878
if (parentData.right != right) {
874
879
parentData.right = right;
880
+ needsRecalculateConstraints = true ;
875
881
needsLayout = true ;
876
882
}
877
883
878
884
if (parentData.top != top) {
879
885
parentData.top = top;
886
+ needsRecalculateConstraints = true ;
880
887
needsLayout = true ;
881
888
}
882
889
883
890
if (parentData.bottom != bottom) {
884
891
parentData.bottom = bottom;
892
+ needsRecalculateConstraints = true ;
885
893
needsLayout = true ;
886
894
}
887
895
888
896
if (parentData.baseline != baseline) {
889
897
parentData.baseline = baseline;
898
+ needsRecalculateConstraints = true ;
890
899
needsLayout = true ;
891
900
}
892
901
@@ -897,8 +906,8 @@ class Constraint {
897
906
898
907
if (parentData.zIndex != zIndex) {
899
908
parentData.zIndex = zIndex;
900
- needsPaint = true ;
901
909
needsReorderChildren = true ;
910
+ needsPaint = true ;
902
911
}
903
912
904
913
if (parentData.translateConstraint != translateConstraint) {
@@ -979,6 +988,11 @@ class Constraint {
979
988
980
989
if (needsLayout) {
981
990
AbstractNode ? targetParent = renderObject.parent;
991
+ if (needsRecalculateConstraints) {
992
+ if (targetParent is _ConstraintRenderBox ) {
993
+ targetParent.markNeedsRecalculateConstraints ();
994
+ }
995
+ }
982
996
if (targetParent is RenderObject ) {
983
997
targetParent.markNeedsLayout ();
984
998
}
@@ -1134,8 +1148,14 @@ class _ConstraintRenderBox extends RenderBox
1134
1148
late bool _debugShowZIndex;
1135
1149
late bool _needsReorderChildren;
1136
1150
1151
+ bool _needsRecalculateConstraints = true ;
1137
1152
final Map <RenderBox , _ConstrainedNode > _constrainedNodes = HashMap ();
1138
1153
final Map <ConstraintId , _ConstrainedNode > _tempConstrainedNodes = HashMap ();
1154
+
1155
+ /// For layout
1156
+ late List <_ConstrainedNode > _layoutOrderList;
1157
+
1158
+ /// for paint
1139
1159
late List <_ConstrainedNode > _paintingOrderList;
1140
1160
1141
1161
static const int maxTimeUsage = 20 ;
@@ -1156,6 +1176,7 @@ class _ConstraintRenderBox extends RenderBox
1156
1176
}
1157
1177
if (! isSameList) {
1158
1178
_childConstraints = value;
1179
+ markNeedsRecalculateConstraints ();
1159
1180
markNeedsLayout ();
1160
1181
}
1161
1182
}
@@ -1177,6 +1198,7 @@ class _ConstraintRenderBox extends RenderBox
1177
1198
set debugPrintConstraints (bool value) {
1178
1199
if (_debugPrintConstraints != value) {
1179
1200
_debugPrintConstraints = value;
1201
+ markNeedsRecalculateConstraints ();
1180
1202
markNeedsLayout ();
1181
1203
}
1182
1204
}
@@ -1191,6 +1213,7 @@ class _ConstraintRenderBox extends RenderBox
1191
1213
set debugCheckConstraints (bool value) {
1192
1214
if (_debugCheckConstraints != value) {
1193
1215
_debugCheckConstraints = value;
1216
+ markNeedsRecalculateConstraints ();
1194
1217
markNeedsLayout ();
1195
1218
}
1196
1219
}
@@ -1205,6 +1228,7 @@ class _ConstraintRenderBox extends RenderBox
1205
1228
set debugName (String ? value) {
1206
1229
if (_debugName != value) {
1207
1230
_debugName = value;
1231
+ markNeedsRecalculateConstraints ();
1208
1232
markNeedsLayout ();
1209
1233
}
1210
1234
}
@@ -1453,6 +1477,22 @@ class _ConstraintRenderBox extends RenderBox
1453
1477
}
1454
1478
}
1455
1479
1480
+ @override
1481
+ void adoptChild (covariant RenderObject child) {
1482
+ super .adoptChild (child);
1483
+ markNeedsRecalculateConstraints ();
1484
+ }
1485
+
1486
+ @override
1487
+ void dropChild (covariant RenderObject child) {
1488
+ super .dropChild (child);
1489
+ markNeedsRecalculateConstraints ();
1490
+ }
1491
+
1492
+ void markNeedsRecalculateConstraints () {
1493
+ _needsRecalculateConstraints = true ;
1494
+ }
1495
+
1456
1496
@override
1457
1497
void performLayout () {
1458
1498
int startTime = 0 ;
@@ -1468,7 +1508,6 @@ class _ConstraintRenderBox extends RenderBox
1468
1508
1469
1509
/// Always fill the parent layout
1470
1510
/// TODO will support wrap_content in the future
1471
-
1472
1511
double consMaxWidth = constraints.maxWidth;
1473
1512
if (consMaxWidth == double .infinity) {
1474
1513
consMaxWidth = window.physicalSize.width / window.devicePixelRatio;
@@ -1479,51 +1518,57 @@ class _ConstraintRenderBox extends RenderBox
1479
1518
}
1480
1519
size = constraints.constrain (Size (consMaxWidth, consMaxHeight));
1481
1520
1482
- assert (() {
1483
- if (_debugCheckConstraints) {
1484
- _debugCheckIds ();
1485
- }
1486
- return true ;
1487
- }());
1521
+ if (_needsRecalculateConstraints) {
1522
+ assert (() {
1523
+ if (_debugCheckConstraints) {
1524
+ _debugCheckIds ();
1525
+ }
1526
+ return true ;
1527
+ }());
1488
1528
1489
- /// Traverse once, building the constrained node tree for each child element
1490
- _buildConstrainedNodeTrees ();
1529
+ /// Traverse once, building the constrained node tree for each child element
1530
+ _buildConstrainedNodeTrees ();
1491
1531
1492
- assert (() {
1493
- if (_debugCheckConstraints) {
1494
- _debugCheckConstraintsIntegrity ();
1495
- _debugCheckLoopConstraints ();
1496
- }
1497
- return true ;
1498
- }());
1532
+ assert (() {
1533
+ if (_debugCheckConstraints) {
1534
+ _debugCheckConstraintsIntegrity ();
1535
+ _debugCheckLoopConstraints ();
1536
+ }
1537
+ return true ;
1538
+ }());
1499
1539
1500
- /// Sort by the depth of constraint from shallow to deep, the lowest depth is 0, representing parent
1501
- List <_ConstrainedNode > constrainedNodeTrees =
1502
- _constrainedNodes.values.toList ();
1503
- constrainedNodeTrees.sort ((left, right) {
1504
- return left.getDepth () - right.getDepth ();
1505
- });
1506
-
1507
- _paintingOrderList = _constrainedNodes.values.toList ();
1508
- _paintingOrderList.sort ((left, right) {
1509
- int result = left.zIndex - right.zIndex;
1510
- if (result == 0 ) {
1511
- result = left.index - right.index;
1512
- }
1513
- return result;
1514
- });
1515
- _needsReorderChildren = false ;
1540
+ /// Sort by the depth of constraint from shallow to deep, the lowest depth is 0, representing parent
1541
+ _layoutOrderList = _constrainedNodes.values.toList ();
1542
+ _paintingOrderList = _constrainedNodes.values.toList ();
1543
+ _constrainedNodes.clear ();
1516
1544
1517
- assert (() {
1518
- /// Print constraints
1519
- if (_debugPrintConstraints) {
1520
- debugPrint ('ConstraintLayout@${_debugName ?? hashCode } constraints: ' +
1521
- jsonEncode (constrainedNodeTrees.map ((e) => e.toJson ()).toList ()));
1522
- }
1523
- return true ;
1524
- }());
1545
+ _layoutOrderList.sort ((left, right) {
1546
+ return left.getDepth () - right.getDepth ();
1547
+ });
1548
+
1549
+ _paintingOrderList.sort ((left, right) {
1550
+ int result = left.zIndex - right.zIndex;
1551
+ if (result == 0 ) {
1552
+ result = left.index - right.index;
1553
+ }
1554
+ return result;
1555
+ });
1556
+
1557
+ assert (() {
1558
+ /// Print constraints
1559
+ if (_debugPrintConstraints) {
1560
+ debugPrint (
1561
+ 'ConstraintLayout@${_debugName ?? hashCode } constraints: ' +
1562
+ jsonEncode (_layoutOrderList.map ((e) => e.toJson ()).toList ()));
1563
+ }
1564
+ return true ;
1565
+ }());
1566
+
1567
+ _needsReorderChildren = false ;
1568
+ _needsRecalculateConstraints = false ;
1569
+ }
1525
1570
1526
- _layoutByConstrainedNodeTrees (constrainedNodeTrees );
1571
+ _layoutByConstrainedNodeTrees ();
1527
1572
1528
1573
if (_releasePrintLayoutTime && kReleaseMode) {
1529
1574
layoutTimeUsage.add (DateTime .now ().millisecondsSinceEpoch - startTime);
@@ -1606,9 +1651,8 @@ class _ConstraintRenderBox extends RenderBox
1606
1651
_getBottomInsets (insets, percentageMargin, anchorHeight);
1607
1652
}
1608
1653
1609
- void _layoutByConstrainedNodeTrees (
1610
- List <_ConstrainedNode > constrainedNodeTrees) {
1611
- for (final element in constrainedNodeTrees) {
1654
+ void _layoutByConstrainedNodeTrees () {
1655
+ for (final element in _layoutOrderList) {
1612
1656
EdgeInsets margin = element.margin;
1613
1657
EdgeInsets goneMargin = element.goneMargin;
1614
1658
@@ -2706,6 +2750,7 @@ class _GuidelineRenderBox extends _InternalBox {
2706
2750
if (_id != value) {
2707
2751
_id = value;
2708
2752
updateParentData ();
2753
+ (this .parent as _ConstraintRenderBox ).markNeedsRecalculateConstraints ();
2709
2754
markParentNeedsLayout ();
2710
2755
}
2711
2756
}
@@ -2880,6 +2925,7 @@ class _BarrierRenderBox extends _InternalBox {
2880
2925
if (_id != value) {
2881
2926
_id = value;
2882
2927
updateParentData ();
2928
+ (this .parent as _ConstraintRenderBox ).markNeedsRecalculateConstraints ();
2883
2929
markParentNeedsLayout ();
2884
2930
}
2885
2931
}
@@ -2907,6 +2953,7 @@ class _BarrierRenderBox extends _InternalBox {
2907
2953
if (! isSameList) {
2908
2954
_referencedIds = value;
2909
2955
updateParentData ();
2956
+ (this .parent as _ConstraintRenderBox ).markNeedsRecalculateConstraints ();
2910
2957
markParentNeedsLayout ();
2911
2958
}
2912
2959
}
0 commit comments