Files
biz_xfj_cloud/lib/common/widgets/toast/toast_widget.dart
T
2026-07-30 17:06:17 +08:00

112 lines
3.4 KiB
Dart

import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:forui/forui.dart';
import 'package:get/get.dart';
import 'package:pj_supermarket_cloud/common/style/index.dart';
/// 应用级轻量反馈入口,统一约束横屏终端上的提示宽度和文本行数。
class Toast {
/// Toast 最多占当前视口宽度的比例,给横屏和窄屏两侧保留稳定安全间距。
static const double _maxWidthFactor = 0.82;
/// Toast 最多展示的文本行数,完整异常仍由业务日志和任务记录保存。
static const int _maxMessageLines = 3;
/// 构建带语义颜色、图标和可换行文本的通用 Toast 内容。
static Widget _content({
required BuildContext context,
required String msg,
required IconData icon,
required Color backgroundColor,
required Color foregroundColor,
}) {
final theme = FTheme.of(context);
final maxWidth = MediaQuery.sizeOf(context).width * _maxWidthFactor;
final content = <Widget>[
Icon(
icon,
size: 35,
color: foregroundColor,
),
SizedBox(width: 10.w),
Text(
msg,
maxLines: _maxMessageLines,
overflow: TextOverflow.ellipsis,
softWrap: true,
style: theme.typography.xl.copyWith(color: foregroundColor),
).flexible(),
];
final toast = content
.toRow(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
)
.padding(horizontal: 30.w, vertical: 15.h)
.decorated(
color: backgroundColor,
borderRadius: BorderRadius.circular(25),
)
.constrained(maxWidth: maxWidth);
return Semantics(
container: true,
liveRegion: true,
label: msg,
child: ExcludeSemantics(child: toast),
);
}
/// 调用平台原生 Toast,供不需要自定义图标布局的简单反馈使用。
static Future<bool?> base({
required String msg,
Color backgroundColor = Colors.black,
Color textColor = Colors.white,
ToastGravity gravity = ToastGravity.TOP,
}) {
return Fluttertoast.showToast(
msg: msg,
gravity: gravity,
timeInSecForIosWeb: 1,
backgroundColor: backgroundColor,
textColor: textColor,
fontSize: 16.sp,
);
}
/// 在顶部展示成功提示,长文案自动换行且不会撑出视口。
static void success({String msg = "操作成功"}) {
final context = Get.overlayContext ?? Get.context!;
final fToast = FToast();
fToast.init(context);
fToast.showToast(
gravity: ToastGravity.TOP,
child: _content(
context: context,
msg: msg,
icon: Icons.check_circle,
backgroundColor: XfjDesignTokens.success,
foregroundColor: Colors.white,
),
);
}
/// 在顶部展示失败提示,详细异常由调用方日志保存而不直接铺满界面。
static void error({String msg = "操作失败"}) {
final context = Get.overlayContext ?? Get.context!;
final fToast = FToast();
fToast.init(context);
fToast.showToast(
gravity: ToastGravity.TOP,
child: _content(
context: context,
msg: msg,
icon: Icons.error,
backgroundColor: XfjDesignTokens.danger,
foregroundColor: Colors.white,
),
);
}
}