positionRelatedTo method

Offset? positionRelatedTo(
  1. GlobalKey<State<StatefulWidget>> targetKey, {
  2. Alignment position = Alignment.topCenter,
  3. Offset translate = Offset.zero,
  4. Offset padding = const Offset(4, 4),
})

Return the global offset (top left) that the widget related to this key should assume to be positioned related to the target widget.

Implementation

Offset? positionRelatedTo(
  GlobalKey targetKey, {
  Alignment position = Alignment.topCenter,
  Offset translate = Offset.zero,
  Offset padding = const Offset(4, 4),
}) {
  final Rect? _targetRect = targetKey.globalPaintBounds;
  final Rect? _rect = globalPaintBounds;

  Offset _position = Offset.zero;

  if (_targetRect != null && _rect != null) {
    final Offset _delta = Offset((_targetRect.width - _rect.width) / 2, (_targetRect.height - _rect.height) / 2);
    final Offset _d = _rect.size.offset + _delta + padding;

    _position = _targetRect.center - (_rect.size / 2).offset + _d.withAl(position) + translate;
  }

  return _position;
}