horizontalPositionDependentBox function
Offset
horizontalPositionDependentBox(
{ - required Size size,
- required Size childSize,
- required Offset target,
- required bool preferLeft,
- double horizontalOffset = 0.0,
- double margin = 10.0,
})
Implementation
Offset horizontalPositionDependentBox({
required Size size,
required Size childSize,
required Offset target,
required bool preferLeft,
double horizontalOffset = 0.0,
double margin = 10.0,
}) {
// Horizontal DIRECTION
final fitsLeft =
target.dx + horizontalOffset + childSize.width <= size.width - margin;
final fitsRight = target.dx - horizontalOffset - childSize.width >= margin;
final tooltipLeft =
preferLeft ? fitsLeft || !fitsRight : !(fitsRight || !fitsLeft);
double x;
if (tooltipLeft) {
x = math.min(target.dx + horizontalOffset, size.width - margin);
} else {
x = math.max(target.dx - horizontalOffset - childSize.width, margin);
}
// Vertical DIRECTION
double y;
if (size.height - margin * 2.0 < childSize.height) {
y = (size.height - childSize.height) / 2.0;
} else {
final normalizedTargetY = target.dy.clamp(margin, size.height - margin);
final edge = margin + childSize.height / 2.0;
if (normalizedTargetY < edge) {
y = margin;
} else if (normalizedTargetY > size.height - edge) {
y = size.height - margin - childSize.height;
} else {
y = normalizedTargetY - childSize.height / 2.0;
}
}
return Offset(x, y);
}