showDialogCustom<T> function

Future<T?> showDialogCustom<T>({
  1. required BuildContext context,
  2. required WidgetBuilder builder,
  3. Duration? duration,
  4. Color barrierColor = Colors.black54,
  5. bool barrierDismissible = true,
  6. bool cushion = true,
  7. double offset = 0,
  8. Location location = Location.center,
  9. OffsetHandle? offsetHandle,
  10. TextStyle? style,
})

cushion 垫层 offset偏移值

Implementation

Future<T?> showDialogCustom<T>({
  required BuildContext context,
  required WidgetBuilder builder,
  Duration? duration,
  Color barrierColor = Colors.black54,
  bool barrierDismissible = true,
  bool cushion = true,
  double offset = 0,
  Location location = Location.center,
  OffsetHandle? offsetHandle,
  TextStyle? style,
}) {
  assert(debugCheckHasMaterialLocalizations(context));
  var _targetContext = context;
//  final ThemeData theme = Theme.of(context, shadowThemeOnly: true);

  var transition = offsetHandle == null ? null : offsetAnim(offsetHandle);

  return showGeneralDialog(
    context: context,
    barrierColor: barrierColor,
    barrierDismissible: barrierDismissible,
    barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
    transitionDuration: duration ?? const Duration(milliseconds: 200),
    transitionBuilder: transition ?? _buildMaterialDialogTransitions,
    pageBuilder: (
      BuildContext buildContext,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
    ) {
      final Widget pageChild = Builder(
        builder: (context) => DefaultTextStyle(
          style: style ?? StyleText.normal(),
          child: builder(context),
        ),
      );

      return Builder(
        builder: (BuildContext context) {
          if (transition != null) {
            switch (location) {
              case Location.center:
                return pageChild;

              case Location.left:
              case Location.right:
                return Row(
                  children: <Widget>[Spacing.fillView(), pageChild],
                );
              case Location.top:
                return Column(
                  children: <Widget>[pageChild, Spacing.fillView()],
                );
              case Location.bottom:
                return Column(
                  children: <Widget>[Spacing.fillView(), pageChild],
                );
            }
          }

          RenderBox renderBox = _targetContext.findRenderObject() as RenderBox;
          Offset _offset = renderBox.localToGlobal(Offset.zero);
          double left = 0;
          double top = 0;
          switch (location) {
            case Location.center:
              return Container(
                alignment: Alignment.center,
                child: pageChild,
              );
            case Location.left:
              left = _offset.dx + offset;
              break;
            case Location.right:
              left = _offset.dx + renderBox.size.width + offset;
              break;
            case Location.top:
              top = _offset.dy + offset;
              break;
            case Location.bottom:
              top = _offset.dy + renderBox.size.height + offset;
              break;
          }

          /// 判断是否需要自己设置灰色阴影层 并点击关闭dialog
          Widget _child() {
            if (location == Location.center) {
              return pageChild;
            } else {
              return Stack(
                children: <Widget>[
                  Spacing.vView(
                    isShow: cushion,
                    child: () => TouchWidget(
                      pressedOpacity: 0,
                      onTap: (_) => FastRouter.popBack(),
                      child: Container(color: Colors.black54),
                    ),
                  ),
                  pageChild,
                ],
              );
            }
          }

          return Container(
            padding: EdgeInsets.fromLTRB(left, top, 0, 0),
            child: _child(),
          );
        },
      );
    },
  );
}