adjustSpacing method
({double additionalEndSpacing, double additionalSpacing, double additionalStartSpacing})?
adjustSpacing({
- required ParentLayout parent,
- required LayoutAxis axis,
- required double viewportSize,
- required double contentSize,
- required double startSpacing,
- required double spacing,
- required double endSpacing,
- required int affectedCount,
override
Calculates additional spacing to distribute remaining space evenly.
This method distributes any remaining space in the viewport after accounting for content size and minimum spacings. The distribution depends on the spacing type:
- space-between: distributes space only between items (aroundStart=0, aroundEnd=0)
- space-around: distributes space around each item (aroundStart=0.5, aroundEnd=0.5)
- space-evenly: distributes space evenly between and at edges (aroundStart=1, aroundEnd=1)
The calculation:
- Calculate remaining space: viewportSize - contentSize
- Calculate total flex units: (items-1) + aroundStart + aroundEnd
- Divide remaining space by total flex units to get flexUnit
- Return additional spacing: start=edgeRatio, between=1.0, end=edgeRatio
Implementation
@override
({
double additionalStartSpacing,
double additionalSpacing,
double additionalEndSpacing,
})?
adjustSpacing({
required ParentLayout parent,
required LayoutAxis axis,
required double viewportSize,
required double contentSize,
required double startSpacing,
required double spacing,
required double endSpacing,
required int affectedCount,
}) {
if (affectedCount <= 1) {
return null;
}
// initial startSpacing, spacing, and endSpacing acts as minimum values
// startSpacing and endSpacing are obtained from padding
// spacing is obtained from the horizontalSpacing/verticalSpacing
// note that viewportSize is already reduced by padding
// and contentSize already contains the spacing between items
double remainingSpace = max(0.0, viewportSize - contentSize);
double totalFlex = (affectedCount - 1).toDouble() + aroundStart + aroundEnd;
if (totalFlex <= 0.0) {
return null;
}
double flexUnit = remainingSpace / totalFlex;
return (
additionalStartSpacing: flexUnit * aroundStart,
additionalSpacing: flexUnit,
additionalEndSpacing: flexUnit * aroundEnd,
);
}