showInContext static method

dynamic showInContext(
  1. BuildContext context, {
  2. int? minLoadingMilliseconds,
  3. int? maxLoadingMilliseconds,
})

在 context 中展示 loading ,(一定记得在 dispose 方法中调用 LoadingUtil.dismissInContext(context);)

Implementation

static showInContext(
  BuildContext context, {
  int? minLoadingMilliseconds,
  int? maxLoadingMilliseconds,
}) {
  String contextKey = context.hashCode.toString();
  // print('==============showInContext:$contextKey');
  OverlayEntry? pageOverlayEntry = contextOverlayMap[contextKey];
  if (pageOverlayEntry != null) {
    return; // 显示中
  }

  double loadingWidth = 49;
  double loadingHeight = 20;
  Widget child = Image.asset(
    'assets/loading_center_new.gif',
    package: 'flutter_demo_kit',
    width: loadingWidth,
    height: loadingHeight,
  );

  pageOverlayEntry =
      _getCenterOverlayInContext(context, child, loadingWidth, loadingHeight);

  // if (Overlay.of(context) == null) {
  //   return;
  // }

  Overlay.of(context).insert(pageOverlayEntry);
  contextOverlayMap[contextKey] = pageOverlayEntry;
  if (minLoadingMilliseconds != null) {
    _minLoadingMilliseconds = minLoadingMilliseconds;
    _startLoadingDateTime = DateTime.now();
  }

  if (maxLoadingMilliseconds != null) {
    Future.delayed(Duration(milliseconds: maxLoadingMilliseconds))
        .then((value) {
      //
    });
  }
}