37 lines
1.3 KiB
Dart
37 lines
1.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../core/navigation/navigator_key.dart';
|
|
import '../widgets/cute_icon.dart';
|
|
|
|
/// 统一提示封装(对齐 uni.showToast 的常用场景)
|
|
///
|
|
/// [icon] 传图标名(见 [cuteIconNames]),会在文字前面显示一个小图标 ——
|
|
/// 比在文案里塞 emoji 干净,而且能跟着主题色走。
|
|
void showToast(BuildContext context, String message,
|
|
{bool error = false, String? icon}) {
|
|
ScaffoldMessenger.of(context).hideCurrentSnackBar();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Row(
|
|
children: [
|
|
if (icon != null) ...[
|
|
// 深色底上用单色,双色在小尺寸下会糊成一团
|
|
CuteIcon(icon, size: 18, tint: Colors.white),
|
|
const SizedBox(width: 8),
|
|
],
|
|
Expanded(child: Text(message)),
|
|
],
|
|
),
|
|
backgroundColor: error ? const Color(0xFFB71C1C) : const Color(0xFF323232),
|
|
behavior: SnackBarBehavior.floating,
|
|
duration: const Duration(seconds: 2),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 全局提示:供没有 BuildContext 的地方调用(网络层、Provider)
|
|
void showGlobalToast(String message, {bool error = false, String? icon}) {
|
|
final context = navigatorKey.currentContext;
|
|
if (context != null) showToast(context, message, error: error, icon: icon);
|
|
}
|