game/lib/utils/toast_utils.dart

79 lines
2.5 KiB
Dart

import 'package:flutter/material.dart';
import '../game/config.dart';
class ToastUtils {
static void showTopToast(
BuildContext context,
String message, {
Color? color,
}) {
final overlay = Overlay.of(context);
late OverlayEntry overlayEntry;
overlayEntry = OverlayEntry(
builder: (context) => Positioned(
top: MediaQuery.of(context).padding.top + 10,
left: 20,
right: 20,
child: Material(
color: Colors.transparent,
child: TweenAnimationBuilder<double>(
tween: Tween(begin: 0.0, end: 1.0),
duration: const Duration(milliseconds: 300),
curve: Curves.easeOutBack,
builder: (context, value, child) {
return Transform.translate(
offset: Offset(0, -50 * (1 - value)),
child: Opacity(
opacity: value.clamp(0.0, 1.0),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
decoration: BoxDecoration(
color: color ?? ThemeConfig.snackBarErrorBg,
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
blurRadius: 8,
offset: const Offset(0, 4),
),
],
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.info_outline, color: Colors.white),
const SizedBox(width: 12),
Expanded(
child: Text(
message,
style: const TextStyle(
color: Colors.white,
fontSize: ThemeConfig.fontSizeMedium,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
),
);
},
),
),
),
);
overlay.insert(overlayEntry);
// Auto remove after duration
Future.delayed(const Duration(seconds: 2), () {
overlayEntry.remove();
});
}
}