From b0b383f6a9e38f37cd649fbb437b57f7ee547322 Mon Sep 17 00:00:00 2001 From: wamynobe Date: Fri, 22 Dec 2023 15:31:50 +0700 Subject: [PATCH 1/2] feat: add animationController and appearanceController to give better control over badge's animation --- lib/src/badge.dart | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/src/badge.dart b/lib/src/badge.dart index 7a13e8b..bcd3742 100644 --- a/lib/src/badge.dart +++ b/lib/src/badge.dart @@ -16,8 +16,21 @@ class Badge extends StatefulWidget { this.ignorePointer = false, this.stackFit = StackFit.loose, this.onTap, + this.animationController, + this.appearanceController, }) : super(key: key); + /// Controls badge animation. By default it is created automatically + /// but you can pass your own [AnimationController] to have better control + /// over badge animation. + final AnimationController? animationController; + + /// Controls badge appearance and disappearance animation. + /// By default it is created automatically + /// but you can pass your own [AnimationController] to have better control + /// over badge animation. + final AnimationController? appearanceController; + /// The badge child, e.g. cart icon button. final Widget? child; @@ -68,16 +81,19 @@ class BadgeState extends State with TickerProviderStateMixin { super.initState(); enableLoopAnimation = widget.badgeAnimation.animationDuration.inMilliseconds > 0; - _animationController = AnimationController( - duration: widget.badgeAnimation.animationDuration, - reverseDuration: widget.badgeAnimation.animationDuration, - vsync: this, - ); - _appearanceController = AnimationController( - duration: widget.badgeAnimation.disappearanceFadeAnimationDuration, - reverseDuration: widget.badgeAnimation.disappearanceFadeAnimationDuration, - vsync: this, - ); + _animationController = widget.animationController ?? + AnimationController( + duration: widget.badgeAnimation.animationDuration, + reverseDuration: widget.badgeAnimation.animationDuration, + vsync: this, + ); + _appearanceController = widget.animationController ?? + AnimationController( + duration: widget.badgeAnimation.disappearanceFadeAnimationDuration, + reverseDuration: + widget.badgeAnimation.disappearanceFadeAnimationDuration, + vsync: this, + ); _animation = CurvedAnimation( parent: _animationController, From 8d4a47acf2d259df0ea8456d154b89800d1b5dcb Mon Sep 17 00:00:00 2001 From: wamynobe Date: Fri, 22 Dec 2023 16:23:02 +0700 Subject: [PATCH 2/2] feat: add another way to get _animationController and _appearanceController after they were created --- lib/src/badge.dart | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/lib/src/badge.dart b/lib/src/badge.dart index bcd3742..f91a02f 100644 --- a/lib/src/badge.dart +++ b/lib/src/badge.dart @@ -16,20 +16,13 @@ class Badge extends StatefulWidget { this.ignorePointer = false, this.stackFit = StackFit.loose, this.onTap, - this.animationController, - this.appearanceController, + this.onCreated, }) : super(key: key); - /// Controls badge animation. By default it is created automatically - /// but you can pass your own [AnimationController] to have better control - /// over badge animation. - final AnimationController? animationController; - - /// Controls badge appearance and disappearance animation. - /// By default it is created automatically - /// but you can pass your own [AnimationController] to have better control - /// over badge animation. - final AnimationController? appearanceController; + final void Function( + AnimationController animationController, + AnimationController appearanceController, + )? onCreated; /// The badge child, e.g. cart icon button. final Widget? child; @@ -81,19 +74,21 @@ class BadgeState extends State with TickerProviderStateMixin { super.initState(); enableLoopAnimation = widget.badgeAnimation.animationDuration.inMilliseconds > 0; - _animationController = widget.animationController ?? - AnimationController( - duration: widget.badgeAnimation.animationDuration, - reverseDuration: widget.badgeAnimation.animationDuration, - vsync: this, - ); - _appearanceController = widget.animationController ?? - AnimationController( - duration: widget.badgeAnimation.disappearanceFadeAnimationDuration, - reverseDuration: - widget.badgeAnimation.disappearanceFadeAnimationDuration, - vsync: this, - ); + _animationController = AnimationController( + duration: widget.badgeAnimation.animationDuration, + reverseDuration: widget.badgeAnimation.animationDuration, + vsync: this, + ); + _appearanceController = AnimationController( + duration: widget.badgeAnimation.disappearanceFadeAnimationDuration, + reverseDuration: widget.badgeAnimation.disappearanceFadeAnimationDuration, + vsync: this, + ); + + widget.onCreated?.call( + _animationController, + _appearanceController, + ); _animation = CurvedAnimation( parent: _animationController,