showLoading static method

void showLoading({
  1. Widget? child,
  2. Color? backgroundColor,
  3. bool barrierDismissible = false,
  4. BuildContext? context,
  5. bool enableAnimation = true,
  6. int animationDuration = 300,
})

显示加载动画 child 自定义加载组件,如果为 null 则使用默认的 CircularProgressIndicator backgroundColor 背景颜色,默认半透明黑色 barrierDismissible 点击背景是否可以取消显示,默认 false context 上下文,如果为 null 则使用 navigatorState enableAnimation 是否启用淡入淡出动画,默认 true animationDuration 动画持续时间(毫秒),默认 300

Implementation

static void showLoading({
  Widget? child,
  Color? backgroundColor,
  bool barrierDismissible = false,
  BuildContext? context,
  bool enableAnimation = true,
  int animationDuration = 300,
}) {
  // 如果已经显示,先关闭
  closeLoading();

  final loadingWidget = child ?? const CircularProgressIndicator(
    valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
  );

  final bgColor = backgroundColor ?? const Color(0x77000000);

  final overlayEntry = OverlayEntry(
    builder: (BuildContext ctx) {
      return _LoadingWidget(
        loadingWidget: loadingWidget,
        backgroundColor: bgColor,
        barrierDismissible: barrierDismissible,
        enableAnimation: enableAnimation,
        animationDuration: animationDuration,
      );
    },
  );

  // 插入到 Overlay,确保在 Toast 之上
  if (context == null) {
    navigatorState.currentState?.overlay?.insert(overlayEntry);
  } else {
    Overlay.of(context).insert(overlayEntry);
  }

  _loadingOverlayManger = OverlayEntryManger(overlayEntry);
}