calculateAxisPositioning function
Given an axis (x or y) of totalSize
, this will calculate how to position
an object in the axis based on its objectSize
and the axisPosition
.
If the object exceed the totalSize
at the top or the bottom, it will
be aligned at the start or at the end with _alignObject
.
Implementation
AxisPosition calculateAxisPositioning({
required double totalSize,
required double objectSize,
required double axisPosition,
}) {
if (axisPosition < 0.0 || axisPosition > 1.0) {
throw AssertionError('The axisPosition must be provided and must be a value'
' between 0.0 and 1.0 inclusive');
}
if (objectSize >= totalSize)
return _alignObject(
totalSize: totalSize,
objectSize: objectSize,
alignEnd: true,
alignStart: true,
);
final objectCenter = totalSize * axisPosition;
final objectHalfSize = objectSize / 2;
final firstSize = objectCenter - objectHalfSize;
if (firstSize < 0)
return _alignObject(
totalSize: totalSize,
objectSize: objectSize,
alignStart: true,
);
final secondSize = totalSize - objectCenter - objectHalfSize;
if (secondSize < 0)
return _alignObject(
totalSize: totalSize,
objectSize: objectSize,
alignEnd: true,
);
return AxisPosition(
firstSpace: AxisCoordinates(start: 0, end: firstSize),
objectSpace: AxisCoordinates(start: firstSize, end: firstSize + objectSize),
secondSpace: AxisCoordinates(start: firstSize + objectSize, end: totalSize),
);
}