78 lines
2.2 KiB
Dart
78 lines
2.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
/// Centralized helper for showing SnackBars across the app.
|
|
///
|
|
/// This guarantees that every SnackBar has a fixed width of 400 and a
|
|
/// consistent floating behaviour, so the project-wide limit only has to
|
|
/// be set in one place.
|
|
class AppSnackBar {
|
|
AppSnackBar._();
|
|
|
|
/// The default width applied to every floating SnackBar in the app.
|
|
static const double width = 400;
|
|
|
|
/// Shows a SnackBar with the project-wide width of 400.
|
|
///
|
|
/// By default a [Text] widget with [message] is used as the content.
|
|
/// Pass [customContent] to override the default content widget (e.g. for
|
|
/// custom layouts that include icons, multiple lines, etc.).
|
|
static void show(
|
|
BuildContext context,
|
|
String message, {
|
|
Widget? customContent,
|
|
Color? backgroundColor,
|
|
Color? textColor,
|
|
SnackBarBehavior behavior = SnackBarBehavior.floating,
|
|
SnackBarAction? action,
|
|
Duration duration = const Duration(seconds: 4),
|
|
ShapeBorder? shape,
|
|
EdgeInsetsGeometry? margin,
|
|
EdgeInsetsGeometry? padding,
|
|
}) {
|
|
final messenger = ScaffoldMessenger.maybeOf(context);
|
|
if (messenger == null) return;
|
|
|
|
final Widget content = customContent ??
|
|
(textColor != null
|
|
? Text(message, style: TextStyle(color: textColor))
|
|
: Text(message));
|
|
|
|
messenger.showSnackBar(
|
|
SnackBar(
|
|
content: content,
|
|
backgroundColor: backgroundColor,
|
|
behavior: behavior,
|
|
action: action,
|
|
duration: duration,
|
|
shape: shape,
|
|
margin: margin,
|
|
padding: padding,
|
|
width: width,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Convenience helper for error messages.
|
|
static void showError(BuildContext context, String message) {
|
|
show(
|
|
context,
|
|
message,
|
|
backgroundColor: Colors.deepOrange,
|
|
);
|
|
}
|
|
|
|
/// Convenience helper for success messages.
|
|
static void showSuccess(BuildContext context, String message) {
|
|
show(
|
|
context,
|
|
message,
|
|
backgroundColor: Colors.green.shade700,
|
|
);
|
|
}
|
|
|
|
/// Convenience helper for informational messages.
|
|
static void showInfo(BuildContext context, String message) {
|
|
show(context, message);
|
|
}
|
|
}
|