import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; // import 'package:lottie/lottie.dart'; // 加载数据的函数类型 typedef LoadBlock = Future Function(); // 弹出Toast提示的类 class AppToast { static const String _defaultLoadingText = ""; static const String _loadingText = "loading..."; /// 显示普通 Toast 提示 static Future showToast( Object msg, { Duration displayTime = const Duration(seconds: 2), }) async { await dismiss(); return SmartDialog.showToast( msg.toString(), alignment: Alignment.center, debounce: true, displayTime: displayTime, ); } static Future showError( Object msg, { Duration displayTime = const Duration(seconds: 2), }) { return showFail(msg, displayTime: displayTime); } /// 显示失败提示 static Future showFail( Object msg, { Duration displayTime = const Duration(seconds: 2), Function()? onDismiss, }) async { await dismiss(); return SmartDialog.showNotify( msg: msg.toString(), alignment: Alignment.center, notifyType: NotifyType.failure, displayTime: displayTime, debounce: true, onDismiss: onDismiss, ); } /// 显示成功提示 static Future showSuccess( Object msg, { Duration displayTime = const Duration(seconds: 2), }) async { await dismiss(); return SmartDialog.showNotify( msg: msg.toString(), notifyType: NotifyType.success, debounce: true, displayTime: displayTime, ); } static Future showInfo( Object msg, { Duration displayTime = const Duration(seconds: 2), }) { return showWarning(msg, displayTime: displayTime); } static Future showWarning( Object msg, { Duration displayTime = const Duration(seconds: 2), }) async { await dismiss(); return SmartDialog.showNotify( msg: msg.toString(), notifyType: NotifyType.warning, debounce: true, displayTime: displayTime, ); } /// 显示 Lottie 加载提示 // static Future showLottieLoading({ // Duration? displayTime, // }) async { // await dismiss(); // return SmartDialog.showLoading( // displayTime: displayTime ?? const Duration(seconds: 5), // maskColor: Colors.transparent, // builder: (_) { // return SizedBox( // width: 120, // height: 120, // child: Lottie.asset( // 'assets/bagua.json', // fit: BoxFit.contain, // repeat: true, // ), // ); // }, // ); // } /// 显示加载提示 static Future showLoading({ String text = _loadingText, Duration displayTime = const Duration(seconds: 10), }) async { await dismiss(); return SmartDialog.showLoading(msg: text, displayTime: displayTime); } /// 关闭特定状态的弹窗 static Future dismiss({SmartStatus status = SmartStatus.loading}) { return SmartDialog.dismiss(status: status); } /// 关闭所有弹窗(占位) static void dismissAll() { SmartDialog.dismiss(); } /// 自动显示加载框并在完成后关闭 static Future loading( LoadBlock block, { String? text, bool isLoading = true, }) async { if (isLoading) { showLoading(text: text ?? _defaultLoadingText); } try { final result = await block(); return result; } finally { dismiss(); } } }