Skip to content

Commit b259481

Browse files
author
fbchen
committed
optimize code
1 parent 64dca01 commit b259481

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

lib/src/constraint_layout.dart

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
2727

2828
///By default, constraints are not recalculated during layout if the constraints of
2929
///child elements do not change. Even the constraint computation is extremely fast.
30-
//
31-
// But when the ListView is swiped quickly, constraints are calculated for each item
32-
// layout process, even though the constraints of these items may not change. This is
33-
// not necessary. At this point ChildConstraintsCache can be used to optimize it so that
34-
// constraints for entries of the same type are computed only once. Refer to example/complex_list.dart
35-
//
36-
// Constraints can also be calculated ahead of time so that they don't need to be
37-
// calculated during layout. Refer to example/preprocess_complex_list.dart
30+
///
31+
/// But when the ListView is swiped quickly, constraints are calculated for each item
32+
/// layout process, even though the constraints of these items may not change. This is
33+
/// not necessary. At this point ChildConstraintsCache can be used to optimize it so that
34+
/// constraints for entries of the same type are computed only once. Refer to example/complex_list.dart
35+
///
36+
/// Constraints can also be calculated ahead of time so that they don't need to be
37+
/// calculated during layout. Refer to example/preprocess_complex_list.dart
38+
///
39+
/// Warning: Can only be used when the constraints of child elements do not change.
40+
/// Warning: It doesn't make sense to use outside of ListView.
3841
final ChildConstraintsCache? childConstraintsCache;
3942
final bool useCacheConstraints;
4043

@@ -1763,7 +1766,10 @@ class _ConstraintRenderBox extends RenderBox
17631766
if (_useCacheConstraints &&
17641767
_childConstraintsCache!._processedNodes != null) {
17651768
List<_ConstrainedNode> layoutList = [];
1766-
List<_ConstrainedNode> paintList = [];
1769+
List<_ConstrainedNode>? paintList;
1770+
if (_needsReorderChildren) {
1771+
paintList = [];
1772+
}
17671773
for (final element in _childConstraintsCache!._processedNodes!) {
17681774
_ConstrainedNode constrainedNode = _ConstrainedNode()
17691775
..nodeId = element.nodeId
@@ -1778,10 +1784,14 @@ class _ConstraintRenderBox extends RenderBox
17781784
..baselineConstraint = element.baselineConstraint
17791785
..baselineAlignType = element.baselineAlignType;
17801786
layoutList.add(constrainedNode);
1781-
paintList.add(constrainedNode);
1787+
paintList?.add(constrainedNode);
17821788
}
17831789

17841790
_layoutOrderList = layoutList;
1791+
if (_needsReorderChildren) {
1792+
_paintingOrderList = paintList!;
1793+
}
1794+
17851795
RenderBox? child = firstChild;
17861796
int childIndex = -1;
17871797
while (child != null) {
@@ -1793,11 +1803,13 @@ class _ConstraintRenderBox extends RenderBox
17931803
_layoutOrderList[childIndex].parentData = childParentData;
17941804
_layoutOrderList[childIndex].index = childIndex;
17951805
_layoutOrderList[childIndex].renderBox = child;
1806+
if (_needsReorderChildren) {
1807+
_paintingOrderList[childIndex].parentData = childParentData;
1808+
_paintingOrderList[childIndex].index = childIndex;
1809+
_paintingOrderList[childIndex].renderBox = child;
1810+
}
17961811
child = childParentData.nextSibling;
17971812
}
1798-
_layoutOrderList.sort((left, right) {
1799-
return left.getDepth() - right.getDepth();
1800-
});
18011813

18021814
assert(() {
18031815
if (_debugCheckConstraints) {
@@ -1807,32 +1819,11 @@ class _ConstraintRenderBox extends RenderBox
18071819
return true;
18081820
}());
18091821

1810-
assert(() {
1811-
/// Print constraints
1812-
if (_debugPrintConstraints) {
1813-
debugPrint(
1814-
'ConstraintLayout@${_debugName ?? hashCode} constraints: ' +
1815-
jsonEncode(
1816-
_layoutOrderList.map((e) => e.toJson()).toList()));
1817-
}
1818-
return true;
1819-
}());
1822+
_layoutOrderList.sort((left, right) {
1823+
return left.getDepth() - right.getDepth();
1824+
});
18201825

18211826
if (_needsReorderChildren) {
1822-
_paintingOrderList = paintList;
1823-
RenderBox? child = firstChild;
1824-
int childIndex = -1;
1825-
while (child != null) {
1826-
childIndex++;
1827-
_ConstraintBoxData childParentData =
1828-
child.parentData as _ConstraintBoxData;
1829-
childParentData._constrainedNodeMap =
1830-
_childConstraintsCache!._processedNodesMap!;
1831-
_paintingOrderList[childIndex].parentData = childParentData;
1832-
_paintingOrderList[childIndex].index = childIndex;
1833-
_paintingOrderList[childIndex].renderBox = child;
1834-
child = childParentData.nextSibling;
1835-
}
18361827
_paintingOrderList.sort((left, right) {
18371828
int result = left.zIndex - right.zIndex;
18381829
if (result == 0) {
@@ -1843,6 +1834,17 @@ class _ConstraintRenderBox extends RenderBox
18431834
_needsReorderChildren = false;
18441835
}
18451836

1837+
assert(() {
1838+
/// Print constraints
1839+
if (_debugPrintConstraints) {
1840+
debugPrint(
1841+
'ConstraintLayout@${_debugName ?? hashCode} constraints: ' +
1842+
jsonEncode(
1843+
_layoutOrderList.map((e) => e.toJson()).toList()));
1844+
}
1845+
return true;
1846+
}());
1847+
18461848
_needsRecalculateConstraints = false;
18471849
} else {
18481850
/// Traverse once, building the constrained node tree for each child element

0 commit comments

Comments
 (0)