|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +enum GFAnimationType { |
| 4 | + align, |
| 5 | + size, |
| 6 | + container, |
| 7 | + rotateTransition, |
| 8 | + scaleTransition, |
| 9 | + slideTransition, |
| 10 | + textStyle |
| 11 | +} |
| 12 | + |
| 13 | +class GFAnimation extends StatefulWidget { |
| 14 | + const GFAnimation({ |
| 15 | + Key key, |
| 16 | + this.duration, |
| 17 | + this.alignment, |
| 18 | + this.child, |
| 19 | + this.curve, |
| 20 | + this.type, |
| 21 | + this.width, |
| 22 | + this.height, |
| 23 | + this.activeColor, |
| 24 | + this.color, |
| 25 | + this.padding, |
| 26 | + this.activeAlignment, |
| 27 | + this.onTap, |
| 28 | + this.margin, |
| 29 | + this.turnsAnimation, |
| 30 | + this.scaleAnimation, |
| 31 | + this.controller, |
| 32 | + this.textDirection, |
| 33 | + this.slidePosition, |
| 34 | + this.style, |
| 35 | + this.textAlign, |
| 36 | + this.textOverflow, |
| 37 | + this.maxLines, |
| 38 | + this.textWidthBasis, |
| 39 | + }) : super(key: key); |
| 40 | + |
| 41 | + final Duration duration; |
| 42 | + final Alignment alignment; |
| 43 | + final Alignment activeAlignment; |
| 44 | + final Widget child; |
| 45 | + final Curve curve; |
| 46 | + final GFAnimationType type; |
| 47 | + final double width; |
| 48 | + final double height; |
| 49 | + final Color activeColor; |
| 50 | + final Color color; |
| 51 | + final EdgeInsets padding, margin; |
| 52 | + final Function onTap; |
| 53 | + final Animation<double> turnsAnimation; |
| 54 | + final Animation<double> scaleAnimation; |
| 55 | + final AnimationController controller; |
| 56 | + final TextDirection textDirection; |
| 57 | + final Animation<Offset> slidePosition; |
| 58 | + final TextStyle style; |
| 59 | + final TextAlign textAlign; |
| 60 | + final TextOverflow textOverflow; |
| 61 | + final int maxLines; |
| 62 | + final TextWidthBasis textWidthBasis; |
| 63 | + |
| 64 | + @override |
| 65 | + _GFAnimationState createState() => _GFAnimationState(); |
| 66 | +} |
| 67 | + |
| 68 | +class _GFAnimationState extends State<GFAnimation> |
| 69 | + with SingleTickerProviderStateMixin { |
| 70 | + bool selected = false; |
| 71 | + bool expand = false; |
| 72 | + |
| 73 | + AnimationController controller; |
| 74 | + Animation<double> animation; |
| 75 | + Animation<Offset> offsetAnimation; |
| 76 | + |
| 77 | + @override |
| 78 | + void initState() { |
| 79 | + if (widget.type == GFAnimationType.rotateTransition) { |
| 80 | + controller = widget.controller ?? |
| 81 | + AnimationController( |
| 82 | + duration: widget.duration ?? const Duration(seconds: 2), |
| 83 | + vsync: this); |
| 84 | + animation = widget.turnsAnimation ?? |
| 85 | + Tween<double>(begin: 0, end: 20).animate(controller); |
| 86 | + if (widget.turnsAnimation == null) { |
| 87 | + controller.forward(); |
| 88 | + } |
| 89 | + } else if (widget.type == GFAnimationType.scaleTransition) { |
| 90 | + controller = widget.controller ?? |
| 91 | + AnimationController( |
| 92 | + vsync: this, |
| 93 | + duration: widget.duration ?? const Duration(seconds: 2)); |
| 94 | + animation = widget.scaleAnimation ?? |
| 95 | + CurvedAnimation( |
| 96 | + parent: controller, curve: widget.curve ?? Curves.ease); |
| 97 | + controller.forward(); |
| 98 | + } else if (widget.type == GFAnimationType.slideTransition) { |
| 99 | + controller = AnimationController( |
| 100 | + duration: widget.duration ?? const Duration(seconds: 2), |
| 101 | + vsync: this, |
| 102 | + )..repeat(reverse: true); |
| 103 | + offsetAnimation = Tween<Offset>( |
| 104 | + begin: Offset.zero, |
| 105 | + end: const Offset(1.5, 0), |
| 106 | + ).animate(CurvedAnimation( |
| 107 | + parent: controller, |
| 108 | + curve: Curves.linear, |
| 109 | + )); |
| 110 | + } |
| 111 | + super.initState(); |
| 112 | + } |
| 113 | + |
| 114 | + @override |
| 115 | + Widget build(BuildContext context) => getAnimatedTypeWidget(); |
| 116 | + |
| 117 | + Widget buildAnimatedContainerWidget() => GestureDetector( |
| 118 | + onTap: () { |
| 119 | + if (mounted) { |
| 120 | + setState(() { |
| 121 | + selected = !selected; |
| 122 | + }); |
| 123 | + } |
| 124 | + }, |
| 125 | + child: AnimatedContainer( |
| 126 | + margin: widget.margin ?? const EdgeInsets.all(0), |
| 127 | + padding: widget.padding ?? const EdgeInsets.all(8), |
| 128 | + width: selected ? widget.width ?? 200.0 : 100.0, |
| 129 | + height: selected ? widget.width ?? 100.0 : 200.0, |
| 130 | + color: selected |
| 131 | + ? widget.activeColor ?? Colors.red |
| 132 | + : widget.color ?? Colors.blue, |
| 133 | + alignment: selected |
| 134 | + ? widget.activeAlignment ?? Alignment.center |
| 135 | + : widget.alignment ?? Alignment.center, |
| 136 | + duration: widget.duration ?? const Duration(milliseconds: 2000), |
| 137 | + curve: widget.curve ?? Curves.linear, |
| 138 | + child: widget.child, |
| 139 | + ), |
| 140 | + ); |
| 141 | + |
| 142 | + Widget buildAnimatedAlignWidget() => GestureDetector( |
| 143 | + onTap: () { |
| 144 | + if (mounted) { |
| 145 | + setState(() { |
| 146 | + selected = !selected; |
| 147 | + }); |
| 148 | + } |
| 149 | + }, |
| 150 | + child: Container( |
| 151 | + margin: widget.margin ?? const EdgeInsets.all(0), |
| 152 | + padding: widget.padding ?? const EdgeInsets.all(0), |
| 153 | + child: AnimatedAlign( |
| 154 | + curve: widget.curve ?? Curves.linear, |
| 155 | + alignment: selected |
| 156 | + ? widget.alignment ?? Alignment.center |
| 157 | + : Alignment.topCenter, |
| 158 | + duration: widget.duration ?? const Duration(seconds: 2), |
| 159 | + child: widget.child, |
| 160 | + ), |
| 161 | + ), |
| 162 | + ); |
| 163 | + |
| 164 | + Widget buildAnimatedSizeWidget() => GestureDetector( |
| 165 | + onTap: widget.onTap, |
| 166 | + child: Container( |
| 167 | + margin: widget.margin ?? const EdgeInsets.all(0), |
| 168 | + padding: widget.padding ?? const EdgeInsets.all(0), |
| 169 | + color: widget.color ?? Colors.white, |
| 170 | + child: AnimatedSize( |
| 171 | + alignment: widget.alignment ?? Alignment.center, |
| 172 | + curve: widget.curve ?? Curves.linear, |
| 173 | + vsync: this, |
| 174 | + duration: widget.duration ?? const Duration(milliseconds: 2000), |
| 175 | + child: widget.child, |
| 176 | + ), |
| 177 | + ), |
| 178 | + ); |
| 179 | + |
| 180 | + Widget buildRotationTransitionWidget() => RotationTransition( |
| 181 | + turns: animation, |
| 182 | + child: widget.child, |
| 183 | + alignment: widget.alignment, |
| 184 | + ); |
| 185 | + |
| 186 | + Widget buildScaleTransitionWidget() => ScaleTransition( |
| 187 | + child: widget.child, |
| 188 | + scale: animation, |
| 189 | + alignment: widget.alignment ?? Alignment.center, |
| 190 | + ); |
| 191 | + |
| 192 | + Widget buildSlideTransitionWidget() => SlideTransition( |
| 193 | + child: widget.child, |
| 194 | + textDirection: widget.textDirection ?? TextDirection.ltr, |
| 195 | + position: widget.slidePosition ?? offsetAnimation, |
| 196 | + ); |
| 197 | + |
| 198 | + Widget buildAnimatedDefaultTextStyleWidget() => GestureDetector( |
| 199 | + onTap: () { |
| 200 | + if (mounted) { |
| 201 | + setState(() {}); |
| 202 | + } |
| 203 | + }, |
| 204 | + child: AnimatedDefaultTextStyle( |
| 205 | + maxLines: widget.maxLines, |
| 206 | + style: widget.style ?? const TextStyle(), |
| 207 | + textWidthBasis: widget.textWidthBasis ?? TextWidthBasis.parent, |
| 208 | + textAlign: widget.textAlign ?? TextAlign.start, |
| 209 | + curve: widget.curve ?? Curves.linear, |
| 210 | + duration: widget.duration ?? const Duration(seconds: 2), |
| 211 | + child: widget.child, |
| 212 | + ), |
| 213 | + ); |
| 214 | + |
| 215 | + Widget getAnimatedTypeWidget() { |
| 216 | + switch (widget.type) { |
| 217 | + case GFAnimationType.align: |
| 218 | + return buildAnimatedAlignWidget(); |
| 219 | + break; |
| 220 | + case GFAnimationType.container: |
| 221 | + return buildAnimatedContainerWidget(); |
| 222 | + break; |
| 223 | + case GFAnimationType.size: |
| 224 | + return buildAnimatedSizeWidget(); |
| 225 | + break; |
| 226 | + case GFAnimationType.rotateTransition: |
| 227 | + return buildRotationTransitionWidget(); |
| 228 | + break; |
| 229 | + case GFAnimationType.scaleTransition: |
| 230 | + return buildScaleTransitionWidget(); |
| 231 | + break; |
| 232 | + case GFAnimationType.slideTransition: |
| 233 | + return buildSlideTransitionWidget(); |
| 234 | + break; |
| 235 | + case GFAnimationType.textStyle: |
| 236 | + return buildAnimatedDefaultTextStyleWidget(); |
| 237 | + break; |
| 238 | + default: |
| 239 | + return buildAnimatedContainerWidget(); |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments