81 lines
1.9 KiB
Dart
81 lines
1.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class ToastHelper {
|
|
static void showSuccess(
|
|
BuildContext context,
|
|
String message, {
|
|
Duration duration = const Duration(seconds: 3),
|
|
}) {
|
|
_showToast(
|
|
context,
|
|
message,
|
|
duration: duration,
|
|
backgroundColor: Colors.green,
|
|
);
|
|
}
|
|
|
|
static void showError(
|
|
BuildContext context,
|
|
String message, {
|
|
Duration duration = const Duration(seconds: 5),
|
|
}) {
|
|
_showToast(
|
|
context,
|
|
message,
|
|
duration: duration,
|
|
backgroundColor: Colors.red,
|
|
);
|
|
}
|
|
|
|
static void showInfo(
|
|
BuildContext context,
|
|
String message, {
|
|
Duration duration = const Duration(seconds: 3),
|
|
}) {
|
|
_showToast(
|
|
context,
|
|
message,
|
|
duration: duration,
|
|
);
|
|
}
|
|
|
|
static void _showToast(
|
|
BuildContext context,
|
|
String message, {
|
|
required Duration duration,
|
|
Color? backgroundColor,
|
|
}) {
|
|
final screenWidth = MediaQuery.of(context).size.width;
|
|
final screenHeight = MediaQuery.of(context).size.height;
|
|
final topPadding = MediaQuery.of(context).padding.top;
|
|
final toastWidth = screenWidth * 0.5; // 50% of screen width
|
|
final horizontalMargin = (screenWidth - toastWidth) / 2;
|
|
|
|
// Position toast very close to top (10px into safe area)
|
|
final topMargin = topPadding - 10;
|
|
const toastHeight = 60.0;
|
|
final bottomMargin = screenHeight - topMargin - toastHeight;
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
message,
|
|
style: const TextStyle(
|
|
color: Colors.white,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
duration: duration,
|
|
behavior: SnackBarBehavior.floating,
|
|
backgroundColor: backgroundColor,
|
|
margin: EdgeInsets.only(
|
|
top: topMargin,
|
|
left: horizontalMargin,
|
|
right: horizontalMargin,
|
|
bottom: bottomMargin,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|