getPositionForChild method
The position where the child should be placed.
The size argument is the size of the parent, which might be different
from the value returned by getSize if that size doesn't satisfy the
constraints passed to getSize. The childSize argument is the size of
the child, which will satisfy the constraints returned by
getConstraintsForChild.
Defaults to positioning the child in the upper left corner of the parent.
Implementation
@override
Offset getPositionForChild(Size size, Size childSize) {
final name = placement.name;
final vertical = name.startsWith('below') || name.startsWith('above');
final side = name.startsWith('below')
? 'below'
: name.startsWith('above')
? 'above'
: name.startsWith('right')
? 'right'
: name.startsWith('left')
? 'left'
: 'center';
final logicalAlong = name.endsWith('Start')
? -1
: name.endsWith('End')
? 1
: 0;
final along = vertical && textDirection == TextDirection.rtl ? -logicalAlong : logicalAlong;
double x, y;
if (side == 'center') {
x = anchor.center.dx - childSize.width / 2;
y = anchor.center.dy - childSize.height / 2;
} else if (vertical) {
// Below unless it only fits above (and the other way round).
var below = side == 'below';
final roomBelow = size.height - margin - (anchor.bottom + gap);
final roomAbove = anchor.top - gap - margin;
// A popover that reserves room stays below and moves up instead.
final flips = !(below && reserve > 0);
if (flips && below && childSize.height > roomBelow && roomAbove > roomBelow) below = false;
if (flips && !below && childSize.height > roomAbove && roomBelow > roomAbove) below = true;
y = below ? anchor.bottom + gap : anchor.top - gap - childSize.height;
if (below && reserve > 0 && y + reserve > size.height - margin) {
y = math.max(margin, size.height - margin - reserve);
}
x = switch (along) {
-1 => anchor.left + offset,
1 => anchor.right - childSize.width - offset,
_ => anchor.center.dx - childSize.width / 2 + offset,
};
} else {
var right = side == 'right';
final roomRight = size.width - margin - (anchor.right + gap);
final roomLeft = anchor.left - gap - margin;
if (right && childSize.width > roomRight && roomLeft > roomRight) right = false;
if (!right && childSize.width > roomLeft && roomRight > roomLeft) right = true;
x = right ? anchor.right + gap : anchor.left - gap - childSize.width;
y = switch (along) {
-1 => anchor.top + offset,
1 => anchor.bottom - childSize.height - offset,
_ => anchor.center.dy - childSize.height / 2 + offset,
};
}
double clampTo(double v, double extent, double total) =>
v.clamp(margin, (total - margin - extent).clamp(margin, double.infinity)).toDouble();
return position = Offset(clampTo(x, childSize.width, size.width), clampTo(y, childSize.height, size.height));
}