getOverlayPosition method

RelativeRect getOverlayPosition(
  1. BuildContext pContext,
  2. MenuAlign? pAlign,
  3. BoxConstraints pConstraints
)

return overlay position based on different params such, constrains, alignment and screen size

Implementation

RelativeRect getOverlayPosition(
    BuildContext pContext, MenuAlign? pAlign, BoxConstraints pConstraints) {
  final dropdownBox = pContext.findRenderObject() as RenderBox;
  final overlayBox =
      Overlay.of(pContext).context.findRenderObject() as RenderBox;
  var popupSize = computePopupSize(dropdownBox, overlayBox, pConstraints);
  var lAlign = pAlign ?? MenuAlign.bottomCenter;

  final dropDownY =
      dropdownBox.localToGlobal(Offset.zero, ancestor: overlayBox).dy;
  final bottomHeight =
      overlayBox.size.height - dropDownY - dropdownBox.size.height;
  final topHeight = dropDownY;

  //get best orientation
  if (lAlign.isDown && popupSize.height > bottomHeight) {
    //check if there enough space in opposite direction,
    if (popupSize.height <= topHeight) {
      lAlign = lAlign.reverse;
    } else if (topHeight > bottomHeight) {
      //if there is more available space in the opposite direction then use that direction
      lAlign = lAlign.reverse;
      popupSize = Size(popupSize.width, topHeight);
    } else {
      //Other wise minimize the height to max available space
      popupSize = Size(popupSize.width, bottomHeight);
    }
  } else if (lAlign.isUp && popupSize.height > topHeight) {
    //check if there enough space in opposite direction,
    if (popupSize.height <= bottomHeight) {
      lAlign = lAlign.reverse;
    } else if (bottomHeight > topHeight) {
      //if there is more available space in the opposite direction then use that direction
      lAlign = lAlign.reverse;
      popupSize = Size(popupSize.width, bottomHeight);
    } else {
      //Other wise minimize the height to max available space
      popupSize = Size(popupSize.width, topHeight);
    }
  }

  return getPosition(dropdownBox, overlayBox, popupSize, lAlign);
}