From d2dc90f020e8899ba45ba6c593dd4ae9990edc3b Mon Sep 17 00:00:00 2001 From: jinyus Date: Fri, 8 Jan 2021 00:53:04 -0500 Subject: [PATCH 1/3] add ability to add delay between animation runs. --- lib/shimmer.dart | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/shimmer.dart b/lib/shimmer.dart index 08b617a..f56551d 100644 --- a/lib/shimmer.dart +++ b/lib/shimmer.dart @@ -59,6 +59,7 @@ enum ShimmerDirection { ltr, rtl, ttb, btt } class Shimmer extends StatefulWidget { final Widget child; final Duration period; + final Duration delay; final ShimmerDirection direction; final Gradient gradient; final int loop; @@ -70,6 +71,7 @@ class Shimmer extends StatefulWidget { @required this.gradient, this.direction = ShimmerDirection.ltr, this.period = const Duration(milliseconds: 1500), + this.delay = const Duration(microseconds: 0), this.loop = 0, this.enabled = true, }) : super(key: key); @@ -85,6 +87,7 @@ class Shimmer extends StatefulWidget { @required Color baseColor, @required Color highlightColor, this.period = const Duration(milliseconds: 1500), + this.delay = const Duration(microseconds: 0), this.direction = ShimmerDirection.ltr, this.loop = 0, this.enabled = true, @@ -118,6 +121,8 @@ class Shimmer extends StatefulWidget { properties.add(EnumProperty('direction', direction)); properties.add( DiagnosticsProperty('period', period, defaultValue: null)); + properties.add( + DiagnosticsProperty('delay', delay, defaultValue: null)); properties .add(DiagnosticsProperty('enabled', enabled, defaultValue: null)); } @@ -132,13 +137,14 @@ class _ShimmerState extends State with SingleTickerProviderStateMixin { super.initState(); _count = 0; _controller = AnimationController(vsync: this, duration: widget.period) - ..addStatusListener((AnimationStatus status) { + ..addStatusListener((AnimationStatus status) async { if (status != AnimationStatus.completed) { return; } _count++; + await Future.delayed(widget.delay); if (widget.loop <= 0) { - _controller.repeat(); + _controller.forward(from: 0.0); } else if (_count < widget.loop) { _controller.forward(from: 0.0); } From 36c3485688b98048fb248a776b8dff7700b45000 Mon Sep 17 00:00:00 2001 From: jinyus Date: Fri, 8 Jan 2021 01:11:11 -0500 Subject: [PATCH 2/3] fix bug where forward was called on _controller after it was disposed. --- lib/shimmer.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/shimmer.dart b/lib/shimmer.dart index f56551d..0d4b8d6 100644 --- a/lib/shimmer.dart +++ b/lib/shimmer.dart @@ -143,6 +143,9 @@ class _ShimmerState extends State with SingleTickerProviderStateMixin { } _count++; await Future.delayed(widget.delay); + if (!mounted) { + return; + } if (widget.loop <= 0) { _controller.forward(from: 0.0); } else if (_count < widget.loop) { From 85576e2a1929f07379d07520fd2b010e8bbabe35 Mon Sep 17 00:00:00 2001 From: jinyus Date: Tue, 28 Dec 2021 11:37:56 -0500 Subject: [PATCH 3/3] use Duration.zero and add comment --- lib/shimmer.dart | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/shimmer.dart b/lib/shimmer.dart index aaad09b..ea4b28f 100644 --- a/lib/shimmer.dart +++ b/lib/shimmer.dart @@ -71,7 +71,7 @@ class Shimmer extends StatefulWidget { required this.gradient, this.direction = ShimmerDirection.ltr, this.period = const Duration(milliseconds: 1500), - this.delay = const Duration(microseconds: 0), + this.delay = Duration.zero, this.loop = 0, this.enabled = true, }) : super(key: key); @@ -87,7 +87,7 @@ class Shimmer extends StatefulWidget { required Color baseColor, required Color highlightColor, this.period = const Duration(milliseconds: 1500), - this.delay = const Duration(microseconds: 0), + this.delay = Duration.zero, this.direction = ShimmerDirection.ltr, this.loop = 0, this.enabled = true, @@ -121,8 +121,8 @@ class Shimmer extends StatefulWidget { properties.add(EnumProperty('direction', direction)); properties.add( DiagnosticsProperty('period', period, defaultValue: null)); - properties.add( - DiagnosticsProperty('delay', delay, defaultValue: null)); + properties + .add(DiagnosticsProperty('delay', delay, defaultValue: null)); properties .add(DiagnosticsProperty('enabled', enabled, defaultValue: null)); properties.add(DiagnosticsProperty('loop', loop, defaultValue: 0)); @@ -144,6 +144,7 @@ class _ShimmerState extends State with SingleTickerProviderStateMixin { _count++; await Future.delayed(widget.delay); if (!mounted) { + // if the widget was unmounted during the delay period return; } if (widget.loop <= 0) {