showDialogAsDropDown<T> function

Future<T?> showDialogAsDropDown<T>(
  1. BuildContext buildContext,
  2. GlobalKey<State<StatefulWidget>> targetKey,
  3. Size sizeChild,
  4. WidgetBuilder childBuilder, {
  5. AsDropDownDirection direction = AsDropDownDirection.bottom,
  6. int marginTargetWidget = 0,
  7. bool barrierDismissible = true,
})

Implementation

Future<T?> showDialogAsDropDown<T>(BuildContext buildContext, GlobalKey targetKey, Size sizeChild, WidgetBuilder childBuilder, {AsDropDownDirection direction = AsDropDownDirection.bottom, int marginTargetWidget = 0, bool barrierDismissible = true}) {
  RenderBox? boxTarget = targetKey.currentContext?.findRenderObject() as RenderBox?;
  Size sizeTarget = boxTarget?.size ?? Size(0, 0);
  Offset offsetTarget = boxTarget?.localToGlobal(Offset.zero) ?? Offset.zero;

  double top = 0, left = 0;
  switch (direction) {
    case AsDropDownDirection.top:
      top = offsetTarget.dy - sizeChild.height - ScreenUtil().statusBarHeight - marginTargetWidget;
      left = offsetTarget.dx + (sizeTarget.width - sizeChild.width) / 2;
      break;
    case AsDropDownDirection.bottom:
      top = offsetTarget.dy + sizeTarget.height + marginTargetWidget - ScreenUtil().statusBarHeight;
      left = offsetTarget.dx + (sizeTarget.width - sizeChild.width) / 2;
      break;
    case AsDropDownDirection.left:
      top = offsetTarget.dy - ScreenUtil().statusBarHeight + (sizeTarget.height - sizeChild.height) / 2;
      left = offsetTarget.dx - sizeChild.width - marginTargetWidget;
      break;
    case AsDropDownDirection.right:
      top = offsetTarget.dy - ScreenUtil().statusBarHeight + (sizeTarget.height - sizeChild.height) / 2;
      left = offsetTarget.dx + sizeTarget.width + marginTargetWidget;
      break;
  }
  return showDialog<T>(
      context: buildContext,
      barrierColor: Colors.transparent,
      builder: (context) {
        return Dialog(
          backgroundColor: Colors.transparent,
          insetPadding: EdgeInsets.zero,
          elevation: 0,
          child: Stack(
            children: [
              GestureDetector(
                  onTap: () {
                    if (barrierDismissible) Navigator.pop(context);
                  },
                  behavior: HitTestBehavior.translucent,
                  child: Container(width: double.infinity, height: double.infinity)),
              Positioned(top: top, left: left, child: childBuilder.call(context)),
            ],
          ),
        );
      });
}