85 lines
3.1 KiB
Dart
85 lines
3.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
/// Global key for the root ScaffoldMessenger, allowing snackbars to survive navigation.
|
|
final rootScaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
|
|
|
/// Nested messenger inside MainScreen — its Scaffold owns the bottom NavigationBar
|
|
/// so floating snackbars auto-offset above the navbar.
|
|
final mainScaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
|
|
|
/// Types of snackbars available in the app
|
|
enum SnackBarType {
|
|
/// Standard informational snackbar
|
|
info,
|
|
|
|
/// Success snackbar (green background)
|
|
success,
|
|
|
|
/// Error snackbar (red background)
|
|
error,
|
|
}
|
|
|
|
/// Utility functions for showing snackbars throughout the application
|
|
|
|
/// Shows a snackbar with the specified type
|
|
///
|
|
/// [context] The build context
|
|
/// [message] The message to display
|
|
/// [type] The type of snackbar (info, success, error)
|
|
/// [duration] Optional duration override
|
|
void showSnackBar(BuildContext context, String message, {SnackBarType type = SnackBarType.info, Duration? duration}) {
|
|
if (!context.mounted) return;
|
|
|
|
final (backgroundColor, defaultDuration) = switch (type) {
|
|
SnackBarType.info => (null, const Duration(seconds: 3)),
|
|
SnackBarType.success => (Colors.green, const Duration(seconds: 3)),
|
|
SnackBarType.error => (Colors.red, const Duration(seconds: 4)),
|
|
};
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(message), backgroundColor: backgroundColor, duration: duration ?? defaultDuration),
|
|
);
|
|
}
|
|
|
|
/// Shows a standard snackbar with a message
|
|
///
|
|
/// [context] The build context
|
|
/// [message] The message to display
|
|
/// [duration] Optional duration, defaults to 3 seconds
|
|
void showAppSnackBar(BuildContext context, String message, {Duration? duration}) {
|
|
showSnackBar(context, message, type: SnackBarType.info, duration: duration);
|
|
}
|
|
|
|
/// Shows an error snackbar with a message
|
|
///
|
|
/// [context] The build context
|
|
/// [message] The error message to display
|
|
void showErrorSnackBar(BuildContext context, String message) {
|
|
showSnackBar(context, message, type: SnackBarType.error);
|
|
}
|
|
|
|
/// Shows an error snackbar using the root ScaffoldMessenger (survives navigation).
|
|
void showGlobalErrorSnackBar(String message) {
|
|
rootScaffoldMessengerKey.currentState?.showSnackBar(
|
|
SnackBar(content: Text(message), backgroundColor: Colors.red, duration: const Duration(seconds: 4)),
|
|
);
|
|
}
|
|
|
|
/// Shows an info snackbar through the main-screen messenger when available
|
|
/// (so it floats above the mobile NavigationBar), falling back to the root
|
|
/// messenger when the main screen is not mounted.
|
|
void showMainSnackBar(String message, {Duration duration = const Duration(seconds: 3)}) {
|
|
final messenger = mainScaffoldMessengerKey.currentState ?? rootScaffoldMessengerKey.currentState;
|
|
messenger
|
|
?..removeCurrentSnackBar()
|
|
..showSnackBar(SnackBar(content: Text(message), duration: duration));
|
|
}
|
|
|
|
/// Shows a success snackbar with a message
|
|
///
|
|
/// [context] The build context
|
|
/// [message] The success message to display
|
|
void showSuccessSnackBar(BuildContext context, String message) {
|
|
showSnackBar(context, message, type: SnackBarType.success);
|
|
}
|