showLoading static method

void showLoading(
  1. BuildContext context, {
  2. String? message,
  3. bool forbidClick = false,
  4. Color? barrierColor,
  5. double? progressSize,
  6. Color? progressColor = Colors.white,
  7. double? progressStrokeWidth = 2,
  8. double? borderRadius,
  9. EdgeInsetsGeometry? padding,
  10. Duration? fadeDuration,
  11. TextStyle? textStyle,
})

Implementation

static void showLoading(
  BuildContext context, {

  /// Message of the toast
  String? message,

  /// Whether to forbid click
  bool forbidClick = false,

  /// Color of the barrier
  Color? barrierColor,

  /// Size of the progress
  double? progressSize,

  /// Color of the progress
  Color? progressColor = Colors.white,

  /// Stroke width of the progress
  double? progressStrokeWidth = 2,

  /// Border radius of the progress
  double? borderRadius,

  /// Padding of the progress
  EdgeInsetsGeometry? padding,

  /// Fade duration of the progress
  Duration? fadeDuration,

  /// Text style of the progress
  TextStyle? textStyle,
}) {
  // 已有的loading不重复显示
  if (_currentLoadingEntry != null) {
    return;
  }
  progressSize ??= 25.bw;
  barrierColor ??= Colors.black.withAlpha(178);

  final overlay = Overlay.of(context);
  _loadingAnimationController = AnimationController(
    duration: fadeDuration ?? const Duration(milliseconds: 250),
    vsync: overlay,
  );
  final opacityAnimation = CurvedAnimation(
    parent: _loadingAnimationController!,
    curve: Curves.easeInOut,
  );

  _currentLoadingEntry = OverlayEntry(
    builder: (context) {
      return Stack(
        children: [
          // 半透明遮罩层(拦截点击)
          if (forbidClick)
            Positioned.fill(child: ColoredBox(color: Colors.transparent)),

          // 中心加载内容
          Positioned.fill(
            child: FadeTransition(
              opacity: opacityAnimation,
              child: Center(
                child: Material(
                  color: Colors.transparent,
                  child: Container(
                    padding:
                        padding ??
                        (message != null
                            ? EdgeInsets.only(
                                left: 20.bw,
                                right: 20.bw,
                                top: 25.bw,
                                bottom: 16.bw,
                              )
                            : EdgeInsets.all(10.bw)),
                    constraints: BoxConstraints(
                      maxWidth: BetterScreenUtil.screenWidth * 0.7,
                      minWidth: 88.bw,
                      minHeight: 88.bw,
                    ),
                    decoration: BoxDecoration(
                      color: barrierColor,
                      borderRadius: BorderRadius.circular(
                        borderRadius ?? 8.bw,
                      ),
                    ),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        SizedBox(
                          width: progressSize,
                          height: progressSize,
                          child: CircularProgressIndicator(
                            strokeWidth: progressStrokeWidth!,
                            valueColor: AlwaysStoppedAnimation(
                              progressColor!,
                            ),
                          ),
                        ),
                        if (message != null) ...[
                          SizedBox(height: 16.bw),
                          Text(
                            message,
                            style:
                                textStyle ??
                                TextStyle(
                                  color: Colors.white,
                                  fontSize: 14.bsp,
                                ),
                          ),
                        ],
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      );
    },
  );

  // 插入到Overlay
  overlay.insert(_currentLoadingEntry!);
  _loadingAnimationController!.forward();
}