fix(ux): show all global messages in front overlay for consistency
This commit is contained in:
parent
a2a8917506
commit
fded06c85a
1 changed files with 17 additions and 32 deletions
|
|
@ -51,40 +51,25 @@ final supabase = Supabase.instance.client;
|
|||
/// Global keys so dialogs & snackbars survive widget-tree rebuilds.
|
||||
final navigatorKey = GlobalKey<NavigatorState>();
|
||||
final scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
||||
OverlayEntry? _activeErrorOverlay;
|
||||
Timer? _activeErrorOverlayTimer;
|
||||
OverlayEntry? _activeMessageOverlay;
|
||||
Timer? _activeMessageOverlayTimer;
|
||||
|
||||
/// Show a snackbar safely through the global key.
|
||||
void showGlobalSnackBar(String message, {bool isError = false}) {
|
||||
if (isError) {
|
||||
_showGlobalErrorOverlay(message);
|
||||
return;
|
||||
}
|
||||
|
||||
final messenger = scaffoldMessengerKey.currentState;
|
||||
if (messenger == null) return;
|
||||
|
||||
messenger
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message),
|
||||
backgroundColor: isError ? Colors.red : null,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
margin: const EdgeInsets.fromLTRB(16, 0, 16, 96),
|
||||
duration: const Duration(seconds: 3),
|
||||
),
|
||||
);
|
||||
_showGlobalMessageOverlay(message, isError: isError);
|
||||
}
|
||||
|
||||
void _showGlobalErrorOverlay(String message) {
|
||||
void _showGlobalMessageOverlay(String message, {required bool isError}) {
|
||||
final overlay = navigatorKey.currentState?.overlay;
|
||||
if (overlay == null) return;
|
||||
|
||||
_activeErrorOverlayTimer?.cancel();
|
||||
_activeErrorOverlay?.remove();
|
||||
_activeMessageOverlayTimer?.cancel();
|
||||
_activeMessageOverlay?.remove();
|
||||
|
||||
_activeErrorOverlay = OverlayEntry(
|
||||
final backgroundColor = isError ? Colors.red : const Color(0xFF1F2937);
|
||||
final leadingIcon = isError ? Icons.error_outline : Icons.info_outline;
|
||||
|
||||
_activeMessageOverlay = OverlayEntry(
|
||||
builder: (context) {
|
||||
final topPadding = MediaQuery.of(context).padding.top;
|
||||
return Positioned(
|
||||
|
|
@ -98,7 +83,7 @@ void _showGlobalErrorOverlay(String message) {
|
|||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red,
|
||||
color: backgroundColor,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
|
|
@ -110,7 +95,7 @@ void _showGlobalErrorOverlay(String message) {
|
|||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.error_outline, color: Colors.white, size: 20),
|
||||
Icon(leadingIcon, color: Colors.white, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
|
|
@ -129,12 +114,12 @@ void _showGlobalErrorOverlay(String message) {
|
|||
},
|
||||
);
|
||||
|
||||
overlay.insert(_activeErrorOverlay!);
|
||||
overlay.insert(_activeMessageOverlay!);
|
||||
|
||||
_activeErrorOverlayTimer = Timer(const Duration(seconds: 4), () {
|
||||
_activeErrorOverlay?.remove();
|
||||
_activeErrorOverlay = null;
|
||||
_activeErrorOverlayTimer = null;
|
||||
_activeMessageOverlayTimer = Timer(const Duration(seconds: 4), () {
|
||||
_activeMessageOverlay?.remove();
|
||||
_activeMessageOverlay = null;
|
||||
_activeMessageOverlayTimer = null;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue