diff --git a/README.md b/README.md index 51f1a97..23e55c6 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ AvatarGlow( glowShape: BoxShape.circle, animate: _animate, curve: Curves.fastOutSlowIn, + glowRepeatCount: 5 // Animation will be repeated 5 times child: const Material( elevation: 8.0, shape: CircleBorder(), diff --git a/example/lib/main.dart b/example/lib/main.dart index 2240f86..bd4910d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -118,6 +118,7 @@ class _HomePageState extends State { const SizedBox(height: 32.0), AvatarGlow( animate: _animate, + glowRepeatCount: 5, glowColor: Colors.cyan, child: Material( elevation: 8.0, diff --git a/lib/src/avatar_glow.dart b/lib/src/avatar_glow.dart index b3f0366..adca94d 100644 --- a/lib/src/avatar_glow.dart +++ b/lib/src/avatar_glow.dart @@ -19,6 +19,7 @@ class AvatarGlow extends StatefulWidget { this.repeat = true, this.curve = Curves.fastOutSlowIn, this.glowRadiusFactor = 0.7, + this.glowRepeatCount = -1 }) : assert( glowShape != BoxShape.circle || glowBorderRadius == null, 'Cannot specify a border radius if the shape is a circle.', @@ -58,6 +59,9 @@ class AvatarGlow extends StatefulWidget { /// The factor that determines the size of each glow effect relative to the original size. final double glowRadiusFactor; + /// Number of times glowing animation should be repeated + final int glowRepeatCount; + @override State createState() => _AvatarGlowState(); } @@ -76,14 +80,27 @@ class _AvatarGlowState extends State // Check if the widget is still mounted before starting the animation. if (mounted) { - if (widget.repeat) { - _controller.repeat(); + if (widget.glowRepeatCount > 0) { + TickerFuture tickerFuture = _controller.repeat(); + tickerFuture.timeout(widget.duration * widget.glowRepeatCount, + onTimeout: () { + _controller.forward(from: 0); + _controller.stop(canceled: true); + }); } else { - _controller.forward(); + _manageController(); } } } + void _manageController() { + if (widget.repeat) { + _controller.repeat(); + } else { + _controller.forward(); + } + } + void _stopAnimation() { // Wait for the animation to finish before stopping it. _controller.reverse().then((_) { @@ -125,11 +142,7 @@ class _AvatarGlowState extends State } if (widget.repeat != oldWidget.repeat) { - if (widget.repeat) { - _controller.repeat(); - } else { - _controller.forward(); - } + _manageController(); } }