From 96e438b69090247ae5fe4523ded6cd8a1f7bfb5b Mon Sep 17 00:00:00 2001 From: sososdk Date: Fri, 27 Oct 2023 11:14:45 +0800 Subject: [PATCH] feat: Add `onRemoveFromRoute` in `DefaultFlashController`. --- CHANGELOG.md | 3 +++ README.md | 2 +- example/lib/main.dart | 3 +++ example/pubspec.lock | 2 +- lib/flash_helper.dart | 2 ++ lib/src/flash_controller.dart | 10 ++++++++-- pubspec.yaml | 4 ++-- 7 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ed64f4..9a69fd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [3.0.5+2] - 2023-10-27 +- Add `onRemoveFromRoute` in `DefaultFlashController`, it only works when `persistent` is false. + ## [3.0.5+1] - 2023-5-30 - `FlashBar` add `builder` param to warp child. diff --git a/README.md b/README.md index 0b26f23..78b40a0 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency: ```yaml dependencies: ... - flash: ^3.0.5+1 + flash: ^3.0.5+2 ``` In your library add the following import: diff --git a/example/lib/main.dart b/example/lib/main.dart index b72703b..3caf677 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -251,6 +251,9 @@ class _FlashPageState extends State { ElevatedButton( onPressed: () => context.showFlash( persistent: false, + onRemoveFromRoute: () { + context.showToast(Text('Flash removed')); + }, builder: (context, controller) => FlashBar( controller: controller, behavior: FlashBehavior.floating, diff --git a/example/pubspec.lock b/example/pubspec.lock index b29ff04..55e82c5 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -87,7 +87,7 @@ packages: path: ".." relative: true source: path - version: "3.0.5+1" + version: "3.0.5+2" flutter: dependency: "direct main" description: flutter diff --git a/lib/flash_helper.dart b/lib/flash_helper.dart index a83f637..a9ab30b 100644 --- a/lib/flash_helper.dart +++ b/lib/flash_helper.dart @@ -269,6 +269,7 @@ extension FlashShortcuts on BuildContext { FutureOr Function()? onBarrierTap, Curve barrierCurve = Curves.ease, bool persistent = true, + VoidCallback? onRemoveFromRoute, Duration? duration, required FlashBuilder builder, Completer? dismissCompleter, @@ -283,6 +284,7 @@ extension FlashShortcuts on BuildContext { onBarrierTap: onBarrierTap, barrierCurve: barrierCurve, persistent: persistent, + onRemoveFromRoute: onRemoveFromRoute, duration: duration, builder: builder, ); diff --git a/lib/src/flash_controller.dart b/lib/src/flash_controller.dart index 23e83d6..0d03bcf 100644 --- a/lib/src/flash_controller.dart +++ b/lib/src/flash_controller.dart @@ -26,6 +26,7 @@ Future showFlash({ Curve barrierCurve = Curves.ease, Duration? duration, bool persistent = true, + VoidCallback? onRemoveFromRoute, }) { return DefaultFlashController( context, @@ -39,6 +40,7 @@ Future showFlash({ barrierCurve: barrierCurve, duration: duration, persistent: persistent, + onRemoveFromRoute: onRemoveFromRoute, ).show(); } @@ -54,6 +56,7 @@ class DefaultFlashController implements FlashController { this.onBarrierTap, this.barrierCurve = Curves.ease, this.persistent = true, + this.onRemoveFromRoute, this.duration, }) : assert(onBarrierTap == null || (barrierDismissible || barrierColor != null || barrierBlur != null)), route = ModalRoute.of(context) { @@ -119,9 +122,11 @@ class DefaultFlashController implements FlashController { /// ``` final bool persistent; + /// Called when this flash is removed from the history of its associated [LocalHistoryRoute]. + /// Only works when [persistent] is false. + final VoidCallback? onRemoveFromRoute; + /// The animation controller that the route uses to drive the transitions. - /// - /// The animation itself is exposed by the [animation] property. @override AnimationController get controller => _controller; late AnimationController _controller; @@ -257,6 +262,7 @@ class DefaultFlashController implements FlashController { if (!persistent) { _historyEntry = LocalHistoryEntry(onRemove: () { assert(!_transitionCompleter.isCompleted, 'Cannot reuse a $runtimeType after disposing it.'); + onRemoveFromRoute?.call(); _removedHistoryEntry = true; if (!_dismissed) { _cancelTimer(); diff --git a/pubspec.yaml b/pubspec.yaml index b709404..ba5cc82 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ name: flash description: A highly customizable, powerful and easy-to-use alerting library for Flutter. -version: 3.0.5+1 +version: 3.0.5+2 homepage: https://github.com/sososdk/flash environment: - sdk: ">=2.17.0 <3.0.0" + sdk: ">=2.17.0 <4.0.0" dependencies: flutter: