|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/rendering.dart'; |
| 3 | + |
| 4 | +enum GFShimmerDirection { leftToRight, rightToLeft, topToBottom, bottomToTop } |
| 5 | + |
| 6 | +@immutable |
| 7 | +class GFShimmer extends StatefulWidget { |
| 8 | + const GFShimmer({ |
| 9 | + Key key, |
| 10 | + @required this.child, |
| 11 | + @required this.gradient, |
| 12 | + this.direction = GFShimmerDirection.leftToRight, |
| 13 | + this.duration = const Duration(milliseconds: 1500), |
| 14 | + this.loop = 0, |
| 15 | + this.enabled = true, |
| 16 | + }) : super(key: key); |
| 17 | + |
| 18 | + /// The widget below this widget in the tree. |
| 19 | + final Widget child; |
| 20 | + |
| 21 | + /// Controls the speed of the shimmer effect. The default value is 1500 milliseconds. |
| 22 | + final Duration duration; |
| 23 | + |
| 24 | + /// Controls the direction of the shimmer effect. The default value is GFShimmerDirection.leftToRight. |
| 25 | + final GFShimmerDirection direction; |
| 26 | + |
| 27 | + /// Controls the [child]'s shades of color. |
| 28 | + final Gradient gradient; |
| 29 | + |
| 30 | + /// Controls the animation loop. The default value is '0', that makes animation forever. |
| 31 | + final int loop; |
| 32 | + |
| 33 | + /// Controls animation effect, defaults true state that makes animation active. |
| 34 | + final bool enabled; |
| 35 | + |
| 36 | + @override |
| 37 | + _GFShimmerState createState() => _GFShimmerState(); |
| 38 | +} |
| 39 | + |
| 40 | +class _GFShimmerState extends State<GFShimmer> |
| 41 | + with SingleTickerProviderStateMixin { |
| 42 | + AnimationController _controller; |
| 43 | + int _count; |
| 44 | + |
| 45 | + @override |
| 46 | + void initState() { |
| 47 | + super.initState(); |
| 48 | + _count = 0; |
| 49 | + _controller = AnimationController(vsync: this, duration: widget.duration) |
| 50 | + ..addStatusListener((AnimationStatus status) { |
| 51 | + if (status != AnimationStatus.completed) { |
| 52 | + return; |
| 53 | + } |
| 54 | + _count++; |
| 55 | + if (widget.loop <= 0) { |
| 56 | + _controller.repeat(); |
| 57 | + } else if (_count < widget.loop) { |
| 58 | + _controller.forward(from: 0); |
| 59 | + } |
| 60 | + }); |
| 61 | + if (widget.enabled) { |
| 62 | + _controller.forward(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + void didUpdateWidget(GFShimmer oldWidget) { |
| 68 | + if (widget.enabled) { |
| 69 | + _controller.forward(); |
| 70 | + } else { |
| 71 | + _controller.stop(); |
| 72 | + } |
| 73 | + super.didUpdateWidget(oldWidget); |
| 74 | + } |
| 75 | + |
| 76 | + @override |
| 77 | + Widget build(BuildContext context) => AnimatedBuilder( |
| 78 | + animation: _controller, |
| 79 | + child: widget.child, |
| 80 | + builder: (BuildContext context, Widget child) => Transform.rotate( |
| 81 | + angle: _controller.value * 2.0 * 22 / 7, |
| 82 | + child: child, |
| 83 | + ), |
| 84 | + ); |
| 85 | + |
| 86 | + @override |
| 87 | + void dispose() { |
| 88 | + _controller.dispose(); |
| 89 | + super.dispose(); |
| 90 | + } |
| 91 | +} |
0 commit comments