computeDryLayout method Null safety
- BoxConstraints constraints
Computes the value returned by getDryLayout. Do not call this function directly, instead, call getDryLayout.
Override in subclasses that implement performLayout or performResize or when setting sizedByParent to true without overriding performResize. This method should return the Size that this RenderBox would like to be given the provided BoxConstraints.
The size returned by this method must match the size that the RenderBox will compute for itself in performLayout (or performResize, if sizedByParent is true).
If this algorithm depends on the size of a child, the size of that child should be obtained using its getDryLayout method.
This layout is called "dry" layout as opposed to the regular "wet" layout run performed by performLayout because it computes the desired size for the given constraints without changing any internal state.
When the size cannot be known
There are cases where render objects do not have an efficient way to compute their size without doing a full layout. For example, the size may depend on the baseline of a child (which is not available without doing a full layout), it may be computed by a callback about which the render object cannot reason, or the layout is so complex that it is simply impractical to calculate the size in an efficient way.
In such cases, it may be impossible (or at least impractical) to actually
return a valid answer. In such cases, the function should call
debugCannotComputeDryLayout from within an assert and return a dummy
value of const Size(0, 0)
.
Implementation
@override
Size computeDryLayout(BoxConstraints constraints) {
assert(() {
if (!constraints.hasBoundedHeight || !constraints.hasBoundedWidth) {
switch (axis) {
case Axis.vertical:
if (!constraints.hasBoundedHeight) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('Vertical viewport was given unbounded height.'),
ErrorDescription(
'Viewports expand in the scrolling direction to fill their container. '
'In this case, a vertical viewport was given an unlimited amount of '
'vertical space in which to expand. This situation typically happens '
'when a scrollable widget is nested inside another scrollable widget.',
),
ErrorHint(
'If this widget is always nested in a scrollable widget there '
'is no need to use a viewport because there will always be enough '
'vertical space for the children. In this case, consider using a '
'Column or Wrap instead. Otherwise, consider using a '
'CustomScrollView to concatenate arbitrary slivers into a '
'single scrollable.',
),
]);
}
if (!constraints.hasBoundedWidth) {
throw FlutterError(
'Vertical viewport was given unbounded width.\n'
'Viewports expand in the cross axis to fill their container and '
'constrain their children to match their extent in the cross axis. '
'In this case, a vertical viewport was given an unlimited amount of '
'horizontal space in which to expand.',
);
}
break;
case Axis.horizontal:
if (!constraints.hasBoundedWidth) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('Horizontal viewport was given unbounded width.'),
ErrorDescription(
'Viewports expand in the scrolling direction to fill their container. '
'In this case, a horizontal viewport was given an unlimited amount of '
'horizontal space in which to expand. This situation typically happens '
'when a scrollable widget is nested inside another scrollable widget.',
),
ErrorHint(
'If this widget is always nested in a scrollable widget there '
'is no need to use a viewport because there will always be enough '
'horizontal space for the children. In this case, consider using a '
'Row or Wrap instead. Otherwise, consider using a '
'CustomScrollView to concatenate arbitrary slivers into a '
'single scrollable.',
),
]);
}
if (!constraints.hasBoundedHeight) {
throw FlutterError(
'Horizontal viewport was given unbounded height.\n'
'Viewports expand in the cross axis to fill their container and '
'constrain their children to match their extent in the cross axis. '
'In this case, a horizontal viewport was given an unlimited amount of '
'vertical space in which to expand.',
);
}
break;
}
}
return true;
}());
return constraints.biggest;
}