showWidget static method

void showWidget({
  1. required Widget child,
  2. int duration = 1000,
})

@description: 吐司内容 (使用前,先确认是否init方法进行初始化) @param { child: 子内容 duration: 显示持续时间,单位毫秒,默认1000毫秒 }

Implementation

static void showWidget({required Widget child, int duration = 1000}) {
  if (_instance.navigatorKey == null) {
    print('navigatorKey不能为空,需要配置navigatorKey');
    throw Exception('navigatorKey不能为空,需要配置navigatorKey');
  }

  BuildContext context =
      _instance.navigatorKey!.currentState!.overlay!.context;

  Timer timer = Timer(Duration(milliseconds: duration), () {
    Navigator.pop(context);
  });

  // 显示的内容
  Widget contentWidget = GestureDetector(
      onTap: () {
        timer.cancel();
        Navigator.pop(context);
      },
      child: child);

  /**显示dialog,使用普通风格的对话框,为了获取动画消失时的状态,来取消timer */
  showGeneralDialog(
      barrierLabel:
          MaterialLocalizations.of(context).modalBarrierDismissLabel,
      barrierColor: Colors.transparent,
      barrierDismissible: true,
      context: context,
      pageBuilder: (BuildContext context, Animation<double> animation,
          Animation<double> secondAnimation) {
        // 已经显示了toast,不允许再显示其他的toast

        return UnconstrainedBox(
            // 先取消父级约束
            child: ConstrainedBox(
                // 设置自己的约束,规定好宽度的最大值,高度的最大值
                constraints: BoxConstraints(
                    maxWidth: MediaQuery.of(context).size.width - 48,
                    maxHeight: MediaQuery.of(context).size.height - 32 - 64),
                child: contentWidget));
      },
      transitionBuilder: (BuildContext context, Animation<double> animal,
          Animation<double> secondAnimal, Widget child) {
        // 消失动画结束时,取消定时器
        if (animal.status == AnimationStatus.reverse &&
            secondAnimal.status == AnimationStatus.dismissed) {
          if (timer.isActive) {
            timer.cancel();
          }

          // print("消失");
          SilToast._instance.isAllowShow = true;
        }

        // 设置自定义动画
        return FadeTransition(opacity: animal, child: child);
      });
}