|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'animated_interpolation.dart'; |
| 3 | + |
| 4 | +class AnimatedConfig { |
| 5 | + AnimatedConfig({ |
| 6 | + this.opacity = 1, |
| 7 | + this.scale = 1, |
| 8 | + this.translateX = 0, |
| 9 | + this.translateY = 0, |
| 10 | + this.skewX = 0, |
| 11 | + this.skewY = 0, |
| 12 | + this.rotateX = 0, |
| 13 | + this.rotateY = 0, |
| 14 | + }); |
| 15 | + final double opacity; |
| 16 | + final double scale; |
| 17 | + final double translateX; |
| 18 | + final double translateY; |
| 19 | + final double skewX; |
| 20 | + final double skewY; |
| 21 | + final double rotateX; |
| 22 | + final double rotateY; |
| 23 | +} |
| 24 | + |
| 25 | +enum AnimatedType { opacity, scale, translateX, translateY, skewX, skewY, rotateX, rotateY } |
| 26 | + |
| 27 | +const Duration _kDefaultDuration = const Duration(seconds: 1); |
| 28 | + |
| 29 | +class SmartAnimatedWidget extends StatefulWidget { |
| 30 | + SmartAnimatedWidget({ |
| 31 | + Key key, |
| 32 | + this.from, |
| 33 | + this.to, |
| 34 | + this.configMap, |
| 35 | + this.child, |
| 36 | + this.curve, |
| 37 | + this.duration = _kDefaultDuration, |
| 38 | + this.autoPlay = false, |
| 39 | + }) : super(key: key); |
| 40 | + final AnimatedConfig from; |
| 41 | + final AnimatedConfig to; |
| 42 | + final Map<double, AnimatedConfig> configMap; |
| 43 | + final Widget child; |
| 44 | + final Curve curve; |
| 45 | + final Duration duration; |
| 46 | + final bool autoPlay; |
| 47 | + |
| 48 | + @override |
| 49 | + SmartAnimatedWidgetState createState() => SmartAnimatedWidgetState(); |
| 50 | +} |
| 51 | + |
| 52 | +class SmartAnimatedWidgetState extends State<SmartAnimatedWidget> with SingleTickerProviderStateMixin<SmartAnimatedWidget> { |
| 53 | + AnimationController _controller; |
| 54 | + Animation<double> _animation; |
| 55 | + bool _animating = false; //是否正在动画 |
| 56 | + |
| 57 | + @override |
| 58 | + void initState() { |
| 59 | + // TODO: implement initState |
| 60 | + super.initState(); |
| 61 | + _controller = AnimationController(vsync: this, duration: widget.duration); |
| 62 | + _animation = Tween<double>(begin: 0, end: 1).animate(_controller); |
| 63 | + if (widget.autoPlay == true) { |
| 64 | + _controller.forward(); |
| 65 | + _animating = true; |
| 66 | + } |
| 67 | + _controller.addStatusListener((status) { |
| 68 | + if (status == AnimationStatus.completed) { |
| 69 | + _animating = false; |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + animate({Duration duration}) { |
| 75 | + if (_animating == false) { |
| 76 | + _controller.reset(); |
| 77 | + _controller.forward(); |
| 78 | + _animating = true; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + void dispose() { |
| 84 | + // TODO: implement dispose |
| 85 | + _controller?.dispose(); |
| 86 | + super.dispose(); |
| 87 | + } |
| 88 | + |
| 89 | + @override |
| 90 | + Widget build(BuildContext context) { |
| 91 | + return AnimatedBuilder( |
| 92 | + animation: _animation, |
| 93 | + builder: (BuildContext context, Widget child) => this.buildWidget(child), |
| 94 | + child: widget.child, |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + Widget buildWidget(Widget _child) { |
| 99 | + InterpolationTween opacityTween; |
| 100 | + InterpolationTween scaleTween; |
| 101 | + InterpolationTween translateXTween; |
| 102 | + InterpolationTween translateYTween; |
| 103 | + InterpolationTween skewXTween; |
| 104 | + InterpolationTween skewYTween; |
| 105 | + InterpolationTween rotateXTween; |
| 106 | + InterpolationTween rotateYTween; |
| 107 | + List<double> inputRange = [0, 1]; |
| 108 | + if (widget.from != null && widget.to != null) { |
| 109 | + AnimatedConfig fc = widget.from; |
| 110 | + AnimatedConfig tc = widget.to; |
| 111 | + scaleTween = InterpolationTween(inputRange: inputRange, outputRange: [fc.scale, tc.scale], curve: widget.curve); |
| 112 | + translateXTween = InterpolationTween(inputRange: inputRange, outputRange: [fc.translateX, tc.translateX], curve: widget.curve); |
| 113 | + translateYTween = InterpolationTween(inputRange: inputRange, outputRange: [fc.translateY, tc.translateY], curve: widget.curve); |
| 114 | + skewXTween = InterpolationTween(inputRange: inputRange, outputRange: [fc.skewX, tc.skewX], curve: widget.curve); |
| 115 | + skewYTween = InterpolationTween(inputRange: inputRange, outputRange: [fc.skewY, tc.skewY], curve: widget.curve); |
| 116 | + rotateXTween = InterpolationTween(inputRange: inputRange, outputRange: [fc.rotateX, tc.rotateX], curve: widget.curve); |
| 117 | + rotateYTween = InterpolationTween(inputRange: inputRange, outputRange: [fc.rotateY, tc.rotateY], curve: widget.curve); |
| 118 | + opacityTween = InterpolationTween(inputRange: inputRange, outputRange: [fc.opacity, tc.opacity], curve: widget.curve); |
| 119 | + } |
| 120 | + if (widget.configMap != null) { |
| 121 | + List<double> inputRange = []; |
| 122 | + List<double> scaleOutRange = []; |
| 123 | + List<double> translateXOutRange = []; |
| 124 | + List<double> translateYOutRange = []; |
| 125 | + List<double> skewXOutRange = []; |
| 126 | + List<double> skewYOutRange = []; |
| 127 | + List<double> rotateXOutRange = []; |
| 128 | + List<double> rotateYOutRange = []; |
| 129 | + List<double> opacityOutRange = []; |
| 130 | + List<double> keysList = widget.configMap.keys.toList(); |
| 131 | + keysList.sort(); //从小到大排序 |
| 132 | + for (int i = 0; i < keysList.length; i++) { |
| 133 | + double key = keysList.elementAt(i); |
| 134 | + inputRange.add(key); |
| 135 | + scaleOutRange.add(widget.configMap[key].scale); |
| 136 | + translateXOutRange.add(widget.configMap[key].translateX); |
| 137 | + translateYOutRange.add(widget.configMap[key].translateY); |
| 138 | + skewXOutRange.add(widget.configMap[key].skewX); |
| 139 | + skewYOutRange.add(widget.configMap[key].skewY); |
| 140 | + rotateXOutRange.add(widget.configMap[key].rotateX); |
| 141 | + rotateYOutRange.add(widget.configMap[key].rotateY); |
| 142 | + opacityOutRange.add(widget.configMap[key].opacity); |
| 143 | + } |
| 144 | + scaleTween = InterpolationTween(inputRange: inputRange, outputRange: scaleOutRange, curve: widget.curve); |
| 145 | + translateXTween = InterpolationTween(inputRange: inputRange, outputRange: translateXOutRange, curve: widget.curve); |
| 146 | + translateYTween = InterpolationTween(inputRange: inputRange, outputRange: translateYOutRange, curve: widget.curve); |
| 147 | + skewXTween = InterpolationTween(inputRange: inputRange, outputRange: skewXOutRange, curve: widget.curve); |
| 148 | + skewYTween = InterpolationTween(inputRange: inputRange, outputRange: skewYOutRange, curve: widget.curve); |
| 149 | + rotateXTween = InterpolationTween(inputRange: inputRange, outputRange: rotateXOutRange, curve: widget.curve); |
| 150 | + rotateYTween = InterpolationTween(inputRange: inputRange, outputRange: rotateYOutRange, curve: widget.curve); |
| 151 | + opacityTween = InterpolationTween(inputRange: inputRange, outputRange: opacityOutRange, curve: widget.curve); |
| 152 | + } |
| 153 | + |
| 154 | + return Transform.scale( |
| 155 | + scale: scaleTween.evaluate(_animation), |
| 156 | + child: Transform.translate( |
| 157 | + offset: Offset( |
| 158 | + translateXTween.evaluate(_animation), |
| 159 | + translateYTween.evaluate(_animation), |
| 160 | + ), |
| 161 | + child: Transform( |
| 162 | + transform: Matrix4.skew( |
| 163 | + skewXTween.evaluate(_animation), |
| 164 | + skewYTween.evaluate(_animation), |
| 165 | + ), |
| 166 | + child: Transform( |
| 167 | + transform: Matrix4.rotationX( |
| 168 | + rotateXTween.evaluate(_animation), |
| 169 | + ), |
| 170 | + child: Transform( |
| 171 | + transform: Matrix4.rotationY( |
| 172 | + rotateYTween.evaluate(_animation), |
| 173 | + ), |
| 174 | + child: Opacity( |
| 175 | + opacity: opacityTween.evaluate(_animation), |
| 176 | + child: _child, |
| 177 | + ), |
| 178 | + ), |
| 179 | + ), |
| 180 | + ), |
| 181 | + ), |
| 182 | + ); |
| 183 | + } |
| 184 | +} |
0 commit comments