calculatePosition static method

OverlayPositionData calculatePosition({
  1. required Rect targetRect,
  2. required Size overlaySize,
  3. required Size screenSize,
  4. required SmartSearchOverlayConfig config,
  5. EdgeInsets padding = EdgeInsets.zero,
})

Calculates the best position for the overlay based on available space.

targetRect - The rectangle of the search box. overlaySize - The desired size of the overlay. screenSize - The size of the screen. config - The overlay configuration. padding - Safe area padding to avoid.

Implementation

static OverlayPositionData calculatePosition({
  required Rect targetRect,
  required Size overlaySize,
  required Size screenSize,
  required SmartSearchOverlayConfig config,
  EdgeInsets padding = EdgeInsets.zero,
}) {
  final position = config.position;
  final offset = config.offset;

  // Calculate available space in each direction
  final spaceAbove = targetRect.top - padding.top;
  final spaceBelow = screenSize.height - targetRect.bottom - padding.bottom;
  final spaceLeft = targetRect.left - padding.left;
  final spaceRight = screenSize.width - targetRect.right - padding.right;

  // Determine the best position if auto
  OverlayPosition resolvedPosition;
  if (position == OverlayPosition.auto) {
    resolvedPosition = _findBestPosition(
      overlaySize: overlaySize,
      spaceAbove: spaceAbove,
      spaceBelow: spaceBelow,
      spaceLeft: spaceLeft,
      spaceRight: spaceRight,
      offset: offset,
    );
  } else {
    resolvedPosition = position;
  }

  // Calculate the actual position and size
  return _calculatePositionData(
    targetRect: targetRect,
    overlaySize: overlaySize,
    screenSize: screenSize,
    config: config,
    padding: padding,
    resolvedPosition: resolvedPosition,
    spaceAbove: spaceAbove,
    spaceBelow: spaceBelow,
    spaceLeft: spaceLeft,
    spaceRight: spaceRight,
  );
}