Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to add delay between animation runs. #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/shimmer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -70,6 +71,7 @@ class Shimmer extends StatefulWidget {
required this.gradient,
this.direction = ShimmerDirection.ltr,
this.period = const Duration(milliseconds: 1500),
this.delay = Duration.zero,
this.loop = 0,
this.enabled = true,
}) : super(key: key);
Expand All @@ -85,6 +87,7 @@ class Shimmer extends StatefulWidget {
required Color baseColor,
required Color highlightColor,
this.period = const Duration(milliseconds: 1500),
this.delay = Duration.zero,
this.direction = ShimmerDirection.ltr,
this.loop = 0,
this.enabled = true,
Expand Down Expand Up @@ -118,6 +121,8 @@ class Shimmer extends StatefulWidget {
properties.add(EnumProperty<ShimmerDirection>('direction', direction));
properties.add(
DiagnosticsProperty<Duration>('period', period, defaultValue: null));
properties
.add(DiagnosticsProperty<Duration>('delay', delay, defaultValue: null));
properties
.add(DiagnosticsProperty<bool>('enabled', enabled, defaultValue: null));
properties.add(DiagnosticsProperty<int>('loop', loop, defaultValue: 0));
Expand All @@ -132,13 +137,18 @@ class _ShimmerState extends State<Shimmer> with SingleTickerProviderStateMixin {
void initState() {
super.initState();
_controller = AnimationController(vsync: this, duration: widget.period)
..addStatusListener((AnimationStatus status) {
..addStatusListener((AnimationStatus status) async {
if (status != AnimationStatus.completed) {
return;
}
_count++;
await Future<dynamic>.delayed(widget.delay);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need create new micro task when duration is zero:

if (widget.delay > Duration.zero) await Future.delayed(widget.delay);

if (!mounted) {
// if the widget was unmounted during the delay period
return;
}
if (widget.loop <= 0) {
_controller.repeat();
_controller.forward(from: 0.0);
} else if (_count < widget.loop) {
_controller.forward(from: 0.0);
}
Expand Down