|
| 1 | +import 'dart:async'; |
| 2 | + |
1 | 3 | import 'package:admincraft/controllers/notification_controller.dart'; |
2 | 4 | import 'package:admincraft/models/app_notification.dart'; |
3 | 5 | import 'package:flutter/material.dart'; |
4 | | -import 'package:toastification/toastification.dart'; |
5 | 6 |
|
6 | 7 | class ToastUtils { |
7 | 8 | static NotificationController? _notifications; |
| 9 | + static final navigatorKey = GlobalKey<NavigatorState>(); |
| 10 | + static OverlayEntry? _popupEntry; |
| 11 | + static Timer? _popupTimer; |
8 | 12 |
|
9 | 13 | static void initialize(NotificationController notifications) { |
10 | 14 | _notifications = notifications; |
11 | 15 | } |
12 | 16 |
|
13 | 17 | static void showToastError(String message) { |
14 | | - _notifications?.add( |
| 18 | + final added = _notifications?.add( |
15 | 19 | kind: AppNotificationKind.error, |
16 | 20 | title: 'Connection or command error', |
17 | 21 | message: message, |
18 | 22 | ); |
19 | | - if (_notifications?.popupsEnabled == false) return; |
20 | | - toastification.show( |
21 | | - type: ToastificationType.error, |
22 | | - style: ToastificationStyle.flat, |
23 | | - autoCloseDuration: const Duration(seconds: 8), |
24 | | - title: const Text('Error'), |
25 | | - description: Text(message), |
26 | | - alignment: Alignment.topRight, |
27 | | - margin: const EdgeInsets.fromLTRB(12, 64, 12, 8), |
28 | | - animationDuration: const Duration(milliseconds: 100), |
| 23 | + if (added == false || _notifications?.popupsEnabled == false) return; |
| 24 | + _showPopup( |
| 25 | + kind: AppNotificationKind.error, |
| 26 | + title: 'Error', |
| 27 | + message: message, |
| 28 | + duration: const Duration(milliseconds: 3500), |
29 | 29 | ); |
30 | 30 | } |
31 | 31 |
|
32 | 32 | static void showToastSuccess(String message) { |
33 | | - _notifications?.add( |
| 33 | + final added = _notifications?.add( |
34 | 34 | kind: AppNotificationKind.success, |
35 | 35 | title: 'Completed', |
36 | 36 | message: message, |
37 | 37 | ); |
38 | | - if (_notifications?.popupsEnabled == false) return; |
39 | | - toastification.show( |
40 | | - type: ToastificationType.success, |
41 | | - style: ToastificationStyle.flat, |
42 | | - autoCloseDuration: const Duration(seconds: 2), |
43 | | - title: Text(message), |
44 | | - alignment: Alignment.topRight, |
45 | | - margin: const EdgeInsets.fromLTRB(12, 64, 12, 8), |
46 | | - animationDuration: const Duration(milliseconds: 100), |
| 38 | + if (added == false || _notifications?.popupsEnabled == false) return; |
| 39 | + _showPopup( |
| 40 | + kind: AppNotificationKind.success, |
| 41 | + title: message, |
| 42 | + duration: const Duration(milliseconds: 1400), |
47 | 43 | ); |
48 | 44 | } |
49 | 45 |
|
50 | 46 | static void showInfo(String title, String message) { |
51 | | - _notifications?.add( |
| 47 | + final added = _notifications?.add( |
| 48 | + kind: AppNotificationKind.info, |
| 49 | + title: title, |
| 50 | + message: message, |
| 51 | + ); |
| 52 | + if (added == false || _notifications?.popupsEnabled == false) return; |
| 53 | + _showPopup( |
52 | 54 | kind: AppNotificationKind.info, |
53 | 55 | title: title, |
54 | 56 | message: message, |
| 57 | + duration: const Duration(milliseconds: 2400), |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + static void _showPopup({ |
| 62 | + required AppNotificationKind kind, |
| 63 | + required String title, |
| 64 | + String? message, |
| 65 | + required Duration duration, |
| 66 | + }) { |
| 67 | + // Popups are only a glanceable hint; the inbox keeps the durable copy. |
| 68 | + // The entire overlay ignores pointers, not just the visible card, so its |
| 69 | + // full-screen layout can never intercept controls underneath. |
| 70 | + dismissPopups(); |
| 71 | + final overlay = navigatorKey.currentState?.overlay; |
| 72 | + if (overlay == null) return; |
| 73 | + |
| 74 | + _popupEntry = OverlayEntry( |
| 75 | + builder: (context) => Positioned.fill( |
| 76 | + child: IgnorePointer( |
| 77 | + child: SafeArea( |
| 78 | + bottom: false, |
| 79 | + child: Align( |
| 80 | + alignment: Alignment.topCenter, |
| 81 | + child: Padding( |
| 82 | + padding: const EdgeInsets.fromLTRB( |
| 83 | + 12, |
| 84 | + kToolbarHeight + 6, |
| 85 | + 12, |
| 86 | + 0, |
| 87 | + ), |
| 88 | + child: ConstrainedBox( |
| 89 | + constraints: const BoxConstraints(maxWidth: 520), |
| 90 | + child: _CompactNotification( |
| 91 | + kind: kind, |
| 92 | + title: title, |
| 93 | + message: message, |
| 94 | + duration: duration, |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + ), |
55 | 102 | ); |
56 | | - if (_notifications?.popupsEnabled == false) return; |
57 | | - toastification.show( |
58 | | - type: ToastificationType.info, |
59 | | - style: ToastificationStyle.flat, |
60 | | - autoCloseDuration: const Duration(seconds: 5), |
61 | | - title: Text(title), |
62 | | - description: Text(message), |
63 | | - alignment: Alignment.topRight, |
64 | | - margin: const EdgeInsets.fromLTRB(12, 64, 12, 8), |
65 | | - animationDuration: const Duration(milliseconds: 100), |
| 103 | + overlay.insert(_popupEntry!); |
| 104 | + _popupTimer = Timer(duration, dismissPopups); |
| 105 | + } |
| 106 | + |
| 107 | + static void dismissPopups() { |
| 108 | + _popupTimer?.cancel(); |
| 109 | + _popupTimer = null; |
| 110 | + _popupEntry?.remove(); |
| 111 | + _popupEntry = null; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +class _CompactNotification extends StatelessWidget { |
| 116 | + final AppNotificationKind kind; |
| 117 | + final String title; |
| 118 | + final String? message; |
| 119 | + final Duration duration; |
| 120 | + |
| 121 | + const _CompactNotification({ |
| 122 | + required this.kind, |
| 123 | + required this.title, |
| 124 | + required this.duration, |
| 125 | + this.message, |
| 126 | + }); |
| 127 | + |
| 128 | + @override |
| 129 | + Widget build(BuildContext context) { |
| 130 | + final scheme = Theme.of(context).colorScheme; |
| 131 | + final (icon, color) = switch (kind) { |
| 132 | + AppNotificationKind.error => (Icons.error_outline, scheme.error), |
| 133 | + AppNotificationKind.warning => (Icons.warning_amber, Colors.orange), |
| 134 | + AppNotificationKind.success => (Icons.check_circle_outline, Colors.green), |
| 135 | + AppNotificationKind.info => (Icons.info_outline, scheme.primary), |
| 136 | + }; |
| 137 | + final detail = message?.trim(); |
| 138 | + |
| 139 | + return Material( |
| 140 | + elevation: 6, |
| 141 | + color: scheme.surfaceContainerHigh, |
| 142 | + borderRadius: BorderRadius.circular(12), |
| 143 | + clipBehavior: Clip.antiAlias, |
| 144 | + child: Column( |
| 145 | + mainAxisSize: MainAxisSize.min, |
| 146 | + children: [ |
| 147 | + Padding( |
| 148 | + padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 9), |
| 149 | + child: Row( |
| 150 | + mainAxisSize: MainAxisSize.min, |
| 151 | + children: [ |
| 152 | + Icon(icon, color: color, size: 20), |
| 153 | + const SizedBox(width: 10), |
| 154 | + Flexible( |
| 155 | + child: Column( |
| 156 | + mainAxisSize: MainAxisSize.min, |
| 157 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 158 | + children: [ |
| 159 | + Text( |
| 160 | + title, |
| 161 | + maxLines: 1, |
| 162 | + overflow: TextOverflow.ellipsis, |
| 163 | + style: Theme.of(context).textTheme.labelLarge, |
| 164 | + ), |
| 165 | + if (detail != null && |
| 166 | + detail.isNotEmpty && |
| 167 | + detail != title) |
| 168 | + Text( |
| 169 | + detail, |
| 170 | + maxLines: 2, |
| 171 | + overflow: TextOverflow.ellipsis, |
| 172 | + style: Theme.of(context).textTheme.bodySmall, |
| 173 | + ), |
| 174 | + ], |
| 175 | + ), |
| 176 | + ), |
| 177 | + ], |
| 178 | + ), |
| 179 | + ), |
| 180 | + TweenAnimationBuilder<double>( |
| 181 | + tween: Tween(begin: 0, end: 1), |
| 182 | + duration: duration, |
| 183 | + builder: (context, value, _) => LinearProgressIndicator( |
| 184 | + value: value, |
| 185 | + minHeight: 2, |
| 186 | + color: color, |
| 187 | + backgroundColor: color.withValues(alpha: 0.12), |
| 188 | + ), |
| 189 | + ), |
| 190 | + ], |
| 191 | + ), |
66 | 192 | ); |
67 | 193 | } |
68 | 194 | } |
0 commit comments