import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; /// ============================================================ /// 【CenterToast】居中轻量提示 —— 对标 uni.showToast(icon: 'none') /// /// 一、适用场景: /// 表单校验失败("请填写账号"/"请填写密码")、轻量成功提示等 /// 不需要用户确认、1.2s 自动消失、不阻塞交互(IgnorePointer)。 /// /// 二、与 SnackBar / ConfirmBox 的分工: /// CenterToast → 短暂轻量提示,自动消失,中间弹出 /// SnackBar → 底部提示(信息可能较长),可手动关闭 /// ConfirmBox → 需要用户决策的确认框,双按钮 /// /// 三、用法: /// CenterToast.show(context, '请填写账号'); /// ============================================================ class CenterToast { CenterToast._(); static OverlayEntry? _current; static void show( BuildContext context, String msg, { Duration duration = const Duration(milliseconds: 1200), }) { // 连续调用时先移除上一个,避免叠加 _current?.remove(); late final OverlayEntry entry; entry = OverlayEntry( builder: (_) => Positioned.fill( child: IgnorePointer( child: Center( child: Container( padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 14.h), decoration: BoxDecoration( color: Colors.black.withValues(alpha: 0.75), borderRadius: BorderRadius.circular(12.r), ), child: Text( msg, style: TextStyle( fontSize: 14.sp, color: Colors.white, decoration: TextDecoration.none, ), ), ), ), ), ), ); _current = entry; Overlay.of(context).insert(entry); Future.delayed(duration, () { if (entry.mounted && _current == entry) { entry.remove(); _current = null; } }); } }