From 37f34cf45d5aec004ff7079212f0291165d9a32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B4nio=20A=20E=20Martins?= <81169982+AntonioAEMartins@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:58:21 -0300 Subject: [PATCH 1/2] add delay to overlayEntry future remove --- lib/src/core/toastification_manager.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/src/core/toastification_manager.dart b/lib/src/core/toastification_manager.dart index 7b05c38..d951f20 100644 --- a/lib/src/core/toastification_manager.dart +++ b/lib/src/core/toastification_manager.dart @@ -27,6 +27,8 @@ class ToastificationManager { /// if the list is empty, the overlay entry will be removed final List _notifications = []; + Duration delay = const Duration(milliseconds: 10); + /// Shows a [ToastificationItem] with the given [builder] and [animationBuilder]. /// /// if the [_notifications] list is empty, we will create the [_overlayEntry] @@ -53,7 +55,6 @@ class ToastificationManager { /// we need this delay because we want to show the item animation after /// the overlay created - var delay = const Duration(milliseconds: 10); if (_overlayEntry == null) { _createNotificationHolder(overlayState); @@ -103,7 +104,7 @@ class ToastificationManager { bool showRemoveAnimation = true, }) { final index = _notifications.indexOf(notification); - + // print("Toastification Manager Dismiss Notifications: $_notifications"); if (index != -1) { notification = _notifications[index]; @@ -143,7 +144,7 @@ class ToastificationManager { // TODO(payam): add the condition before the delay /// we will remove the [_overlayEntry] if there are no notifications Future.delayed( - removedItem.animationDuration ?? config.animationDuration, + (removedItem.animationDuration ?? config.animationDuration) + delay, () { if (_notifications.isEmpty) { _overlayEntry?.remove(); From da7bf7d247eac202b0ee886d84f3fb6bdbddb5e1 Mon Sep 17 00:00:00 2001 From: Payam Zahedi Date: Sat, 14 Sep 2024 13:35:07 +0200 Subject: [PATCH 2/2] fix: add different delays for create and remove overlay --- lib/src/core/toastification_manager.dart | 42 ++++++++++++++++-------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/src/core/toastification_manager.dart b/lib/src/core/toastification_manager.dart index d951f20..98c41bb 100644 --- a/lib/src/core/toastification_manager.dart +++ b/lib/src/core/toastification_manager.dart @@ -27,7 +27,19 @@ class ToastificationManager { /// if the list is empty, the overlay entry will be removed final List _notifications = []; - Duration delay = const Duration(milliseconds: 10); + /// this is the delay for showing the overlay entry + /// We need this delay because we want to show the item animation after + /// the overlay created + /// + /// When we want to show first toast, we need to wait for the overlay to be created + /// and then show the toast item. + final _createOverlayDelay = const Duration(milliseconds: 100); + + /// this is the delay for removing the overlay entry + /// + /// when we want to remove the last toast, we need to wait for the animation + /// to be completed and then remove the overlay. + final _removeOverlayDelay = const Duration(milliseconds: 50); /// Shows a [ToastificationItem] with the given [builder] and [animationBuilder]. /// @@ -55,12 +67,12 @@ class ToastificationManager { /// we need this delay because we want to show the item animation after /// the overlay created + Duration delay = const Duration(milliseconds: 10); if (_overlayEntry == null) { _createNotificationHolder(overlayState); - // TODO(payam): remove this in the future - delay = const Duration(milliseconds: 300); + delay = _createOverlayDelay; } Future.delayed( @@ -141,17 +153,21 @@ class ToastificationManager { ); } - // TODO(payam): add the condition before the delay /// we will remove the [_overlayEntry] if there are no notifications - Future.delayed( - (removedItem.animationDuration ?? config.animationDuration) + delay, - () { - if (_notifications.isEmpty) { - _overlayEntry?.remove(); - _overlayEntry = null; - } - }, - ); + /// We need to check if the _notifications list is empty twice. + /// To make sure after the delay, there are no new notifications added. + if (_notifications.isEmpty) { + Future.delayed( + (removedItem.animationDuration ?? config.animationDuration) + + _removeOverlayDelay, + () { + if (_notifications.isEmpty) { + _overlayEntry?.remove(); + _overlayEntry = null; + } + }, + ); + } } }