computeDryBaseline method
- covariant BoxConstraints constraints,
- TextBaseline baseline
Computes the value returned by getDryBaseline.
This method is for overriding only and shouldn't be called directly. To
get this RenderBox's speculative baseline location for the given
constraints
, call getDryBaseline instead.
The "dry" in the method name means the implementation must not produce
observable side effects when called. For example, it must not change the
size of the RenderBox, or its children's paint offsets, otherwise that
would results in UI changes when paint is called, or hit-testing behavior
changes when hitTest is called. Moreover, accessing the current layout
of this RenderBox or child RenderBoxes (including accessing size, or
child.size
) usually indicates a bug in the implementation, as the current
layout is typically calculated using a set of BoxConstraints that's
different from the constraints
given as the first parameter. To get the
size of this RenderBox or a child RenderBox in this method's
implementation, use the getDryLayout method instead.
The implementation must return a value that represents the distance from
the top of the box to the first baseline of the box's contents, for the
given constraints
, or null
if the RenderBox has no baselines. It's
the same exact value RenderBox.computeDistanceToActualBaseline would
return, when this RenderBox was laid out at constraints
in the same
exact state.
Not all RenderBoxes support dry baseline computation. For example, to
compute the dry baseline of a LayoutBuilder, its builder
may have to
be called with different constraints, which may have side effects such as
updating the widget tree, violating the "dry" contract. In such cases the
RenderBox must call debugCannotComputeDryLayout in an assert, and
return a dummy baseline offset value (such as null
).
Implementation
@override
double? computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) {
if (firstChild == null) {
return null;
}
final BoxConstraints childConstraints = switch (direction) {
Axis.horizontal => BoxConstraints(maxWidth: constraints.maxWidth),
Axis.vertical => BoxConstraints(maxHeight: constraints.maxHeight),
};
final (_AxisSize childrenAxisSize, List<_RunMetrics> runMetrics) = _computeRuns(constraints, ChildLayoutHelper.dryLayoutChild);
final _AxisSize containerAxisSize = childrenAxisSize.applyConstraints(constraints, direction);
BaselineOffset baselineOffset = BaselineOffset.noBaseline;
void findHighestBaseline(Offset offset, RenderBox child) {
baselineOffset = baselineOffset.minOf(BaselineOffset(child.getDryBaseline(childConstraints, baseline)) + offset.dy);
}
Size getChildSize(RenderBox child) => child.getDryLayout(childConstraints);
_positionChildren(runMetrics, childrenAxisSize, containerAxisSize, findHighestBaseline, getChildSize);
return baselineOffset.offset;
}