Skip to content

Commit

Permalink
Merge pull request #133 from AntonioAEMartins/issue-132
Browse files Browse the repository at this point in the history
Fix: AnimationController.dispose() called more than once. on multiple action show
  • Loading branch information
payam-zahedi authored Sep 14, 2024
2 parents 423dae7 + da7bf7d commit cc2bf36
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions lib/src/core/toastification_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ class ToastificationManager {
/// if the list is empty, the overlay entry will be removed
final List<ToastificationItem> _notifications = [];

/// 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].
///
/// if the [_notifications] list is empty, we will create the [_overlayEntry]
Expand All @@ -53,13 +67,12 @@ class ToastificationManager {

/// we need this delay because we want to show the item animation after
/// the overlay created
var delay = const Duration(milliseconds: 10);
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(
Expand Down Expand Up @@ -103,7 +116,7 @@ class ToastificationManager {
bool showRemoveAnimation = true,
}) {
final index = _notifications.indexOf(notification);

// print("Toastification Manager Dismiss Notifications: $_notifications");
if (index != -1) {
notification = _notifications[index];

Expand Down Expand Up @@ -140,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,
() {
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;
}
},
);
}
}
}

Expand Down

0 comments on commit cc2bf36

Please sign in to comment.