Skip to content

Commit e5ed885

Browse files
committed
optimize layout performance
1 parent b5223eb commit e5ed885

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

lib/src/constraint_layout.dart

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,27 +3156,28 @@ class _ConstraintRenderBox extends RenderBox
31563156
list.add(node.parentData._constrainedNodeMap[id]!.getBottom());
31573157
}
31583158
}
3159-
list.sort((left, right) {
3160-
if (left > right) {
3161-
return 1;
3162-
} else if (left == right) {
3163-
return 0;
3164-
} else {
3165-
return -1;
3159+
double min = double.maxFinite;
3160+
double max = double.minPositive;
3161+
for (final element in list) {
3162+
if (element > max) {
3163+
max = element;
31663164
}
3167-
});
3165+
if (element < min) {
3166+
min = element;
3167+
}
3168+
}
31683169
if (direction == BarrierDirection.left) {
3169-
offsetX = list.first;
3170+
offsetX = min;
31703171
offsetY = 0;
31713172
} else if (direction == BarrierDirection.top) {
31723173
offsetX = 0;
3173-
offsetY = list.first;
3174+
offsetY = min;
31743175
} else if (direction == BarrierDirection.right) {
3175-
offsetX = list.last;
3176+
offsetX = max;
31763177
offsetY = 0;
31773178
} else {
31783179
offsetX = 0;
3179-
offsetY = list.last;
3180+
offsetY = max;
31803181
}
31813182
} else {
31823183
/// Calculate child x offset
@@ -3830,11 +3831,8 @@ class _ConstrainedNode {
38303831
return baseline;
38313832
}
38323833

3833-
int getDepthFor(_ConstrainedNode? constrainedNode, bool? parentSizeConfirmed,
3834+
int getDepthFor(_ConstrainedNode constrainedNode, bool? parentSizeConfirmed,
38343835
double? resolvedWidth, double? resolvedHeight) {
3835-
if (constrainedNode == null) {
3836-
return -1;
3837-
}
38383836
if (parentSizeConfirmed == false) {
38393837
if (constrainedNode.isParent()) {
38403838
/// The width and height can be calculated directly without relying on parent
@@ -3861,23 +3859,26 @@ class _ConstrainedNode {
38613859
list.add(parentData._constrainedNodeMap[id]!
38623860
.getDepth(parentSizeConfirmed, resolvedWidth, resolvedHeight));
38633861
}
3864-
list.sort((left, right) => left - right);
3865-
depth = list.last + 1;
3862+
depth = list.reduce(max) + 1;
38663863
} else {
38673864
List<int> list = [
3868-
getDepthFor(leftConstraint, parentSizeConfirmed, resolvedWidth,
3869-
resolvedHeight),
3870-
getDepthFor(topConstraint, parentSizeConfirmed, resolvedWidth,
3871-
resolvedHeight),
3872-
getDepthFor(rightConstraint, parentSizeConfirmed, resolvedWidth,
3873-
resolvedHeight),
3874-
getDepthFor(bottomConstraint, parentSizeConfirmed, resolvedWidth,
3875-
resolvedHeight),
3876-
getDepthFor(baselineConstraint, parentSizeConfirmed, resolvedWidth,
3877-
resolvedHeight),
3865+
if (leftConstraint != null)
3866+
getDepthFor(leftConstraint!, parentSizeConfirmed, resolvedWidth,
3867+
resolvedHeight),
3868+
if (topConstraint != null)
3869+
getDepthFor(topConstraint!, parentSizeConfirmed, resolvedWidth,
3870+
resolvedHeight),
3871+
if (rightConstraint != null)
3872+
getDepthFor(rightConstraint!, parentSizeConfirmed, resolvedWidth,
3873+
resolvedHeight),
3874+
if (bottomConstraint != null)
3875+
getDepthFor(bottomConstraint!, parentSizeConfirmed, resolvedWidth,
3876+
resolvedHeight),
3877+
if (baselineConstraint != null)
3878+
getDepthFor(baselineConstraint!, parentSizeConfirmed, resolvedWidth,
3879+
resolvedHeight),
38783880
];
3879-
list.sort((left, right) => left - right);
3880-
depth = list.last + 1;
3881+
depth = list.reduce(max) + 1;
38813882
}
38823883
}
38833884
return depth;

0 commit comments

Comments
 (0)