|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:getwidget/types/gf_animation_type.dart'; |
| 3 | + |
| 4 | +class GFAnimation extends StatefulWidget { |
| 5 | + const GFAnimation({ |
| 6 | + Key key, |
| 7 | + this.duration, |
| 8 | + this.alignment, |
| 9 | + this.child, |
| 10 | + this.curve, |
| 11 | + this.type, |
| 12 | + this.width, |
| 13 | + this.height, |
| 14 | + this.activeColor, |
| 15 | + this.color, |
| 16 | + this.padding, |
| 17 | + this.activeAlignment, |
| 18 | + this.onTap, |
| 19 | + this.margin, |
| 20 | + this.turnsAnimation, |
| 21 | + this.scaleAnimation, |
| 22 | + this.controller, |
| 23 | + this.textDirection, |
| 24 | + this.slidePosition, |
| 25 | + this.style, |
| 26 | + this.textAlign, |
| 27 | + this.textOverflow, |
| 28 | + this.maxLines, |
| 29 | + this.textWidthBasis, |
| 30 | + this.fontSize, |
| 31 | + this.fontWeight, |
| 32 | + this.changedWidth, |
| 33 | + this.changedHeight, |
| 34 | + }) : super(key: key); |
| 35 | + |
| 36 | + /// The duration for animations of the [Decoration]. |
| 37 | + final Duration duration; |
| 38 | + |
| 39 | + /// Defines how the animated widget is aligned within the Animation. |
| 40 | + final Alignment alignment; |
| 41 | + |
| 42 | + /// Defines how the animated widget is aligned(after the onTap) within the Animation. |
| 43 | + final Alignment activeAlignment; |
| 44 | + |
| 45 | + /// The child of type [Widget] to display animation effect. |
| 46 | + final Widget child; |
| 47 | + |
| 48 | + /// Determines the animation curve physics. Defaults to [Curves.linear]. |
| 49 | + final Curve curve; |
| 50 | + |
| 51 | + ///type of [GFAnimation] which takes the type ie, align, size, container, rotateTransition, scaleTransition, slideTransition, and textStyle for the [GFAnimation] |
| 52 | + final GFAnimationType type; |
| 53 | + |
| 54 | + /// [AnimatedContainer] initial width |
| 55 | + final double width; |
| 56 | + |
| 57 | + /// [AnimatedContainer] changed width |
| 58 | + final double changedWidth; |
| 59 | + |
| 60 | + /// [AnimatedContainer] initial height |
| 61 | + final double height; |
| 62 | + |
| 63 | + /// [AnimatedContainer] changed height |
| 64 | + final double changedHeight; |
| 65 | + |
| 66 | + /// defines the color of items when onTap triggers |
| 67 | + final Color activeColor; |
| 68 | + |
| 69 | + /// defines the color of items |
| 70 | + final Color color; |
| 71 | + |
| 72 | + /// The empty space that surrounds the animation. Defines the animation outer [Container.padding].. |
| 73 | + final EdgeInsetsGeometry padding; |
| 74 | + |
| 75 | + /// The empty space that surrounds the animation. Defines the animation outer [Container.margin]. |
| 76 | + final EdgeInsetsGeometry margin; |
| 77 | + final Function onTap; |
| 78 | + |
| 79 | + /// Here's an illustration of the [RotationTransition] widget, with it's [turnsAnimation] |
| 80 | + /// animated by a [Tween] set to [animate]: |
| 81 | + final Animation<double> turnsAnimation; |
| 82 | + |
| 83 | + /// Here's an illustration of the [ScaleTransition] widget, with it's [scaleAnimation] |
| 84 | + /// animated by a [CurvedAnimation] set to [Curves.linear]: |
| 85 | + final Animation<double> scaleAnimation; |
| 86 | + |
| 87 | + final AnimationController controller; |
| 88 | + |
| 89 | + ///direction of the [AnimatedDefaultTextStyle] TextDirection for [ltr,rtl] |
| 90 | + final TextDirection textDirection; |
| 91 | + |
| 92 | + /// * [ScaleTransition], which animates the scale of a widget. |
| 93 | + final Animation<Offset> slidePosition; |
| 94 | + final TextStyle style; |
| 95 | + final TextAlign textAlign; |
| 96 | + final TextOverflow textOverflow; |
| 97 | + |
| 98 | + /// [AnimatedDefaultTextStyle] maxlines |
| 99 | + final int maxLines; |
| 100 | + final TextWidthBasis textWidthBasis; |
| 101 | + final double fontSize; |
| 102 | + final FontWeight fontWeight; |
| 103 | + |
| 104 | + @override |
| 105 | + _GFAnimationState createState() => _GFAnimationState(); |
| 106 | +} |
| 107 | + |
| 108 | +class _GFAnimationState extends State<GFAnimation> |
| 109 | + with SingleTickerProviderStateMixin { |
| 110 | + bool selected = false; |
| 111 | + |
| 112 | + AnimationController controller; |
| 113 | + Animation<double> animation; |
| 114 | + Animation<Offset> offsetAnimation; |
| 115 | + |
| 116 | + @override |
| 117 | + void initState() { |
| 118 | + if (widget.type == GFAnimationType.rotateTransition) { |
| 119 | + controller = widget.controller ?? |
| 120 | + AnimationController( |
| 121 | + duration: widget.duration ?? const Duration(seconds: 2), |
| 122 | + vsync: this); |
| 123 | + animation = widget.turnsAnimation ?? |
| 124 | + Tween<double>(begin: 0, end: 20).animate(controller); |
| 125 | + if (widget.turnsAnimation == null) { |
| 126 | + controller.forward(); |
| 127 | + } |
| 128 | + } else if (widget.type == GFAnimationType.scaleTransition) { |
| 129 | + controller = widget.controller ?? |
| 130 | + AnimationController( |
| 131 | + vsync: this, |
| 132 | + duration: widget.duration ?? const Duration(seconds: 2)); |
| 133 | + animation = widget.scaleAnimation ?? |
| 134 | + CurvedAnimation( |
| 135 | + parent: controller, curve: widget.curve ?? Curves.ease); |
| 136 | + controller.forward(); |
| 137 | + } else if (widget.type == GFAnimationType.slideTransition) { |
| 138 | + controller = AnimationController( |
| 139 | + duration: widget.duration ?? const Duration(seconds: 2), |
| 140 | + vsync: this, |
| 141 | + )..repeat(reverse: true); |
| 142 | + offsetAnimation = Tween<Offset>( |
| 143 | + begin: Offset.zero, |
| 144 | + end: const Offset(1.5, 0), |
| 145 | + ).animate(CurvedAnimation( |
| 146 | + parent: controller, |
| 147 | + curve: Curves.linear, |
| 148 | + )); |
| 149 | + } |
| 150 | + super.initState(); |
| 151 | + } |
| 152 | + |
| 153 | + @override |
| 154 | + void dispose() { |
| 155 | + controller.dispose(); |
| 156 | + super.dispose(); |
| 157 | + } |
| 158 | + |
| 159 | + @override |
| 160 | + Widget build(BuildContext context) => getAnimatedTypeWidget(); |
| 161 | + |
| 162 | + Widget buildAnimatedContainerWidget() => GestureDetector( |
| 163 | + onTap: () { |
| 164 | + if (mounted) { |
| 165 | + setState(() { |
| 166 | + selected = !selected; |
| 167 | + }); |
| 168 | + } |
| 169 | + }, |
| 170 | + child: AnimatedContainer( |
| 171 | + margin: widget.margin ?? const EdgeInsets.all(0), |
| 172 | + padding: widget.padding ?? const EdgeInsets.all(8), |
| 173 | + width: |
| 174 | + selected ? widget.changedWidth ?? 200.0 : widget.width ?? 100.0, |
| 175 | + height: |
| 176 | + selected ? widget.changedHeight ?? 200.0 : widget.height ?? 100.0, |
| 177 | + color: selected |
| 178 | + ? widget.activeColor ?? Colors.red |
| 179 | + : widget.color ?? Colors.blue, |
| 180 | + alignment: selected |
| 181 | + ? widget.activeAlignment ?? Alignment.center |
| 182 | + : widget.alignment ?? Alignment.center, |
| 183 | + duration: widget.duration ?? const Duration(milliseconds: 2000), |
| 184 | + curve: widget.curve ?? Curves.linear, |
| 185 | + child: widget.child, |
| 186 | + ), |
| 187 | + ); |
| 188 | + |
| 189 | + Widget buildAnimatedAlignWidget() => GestureDetector( |
| 190 | + onTap: () { |
| 191 | + if (widget.onTap == null) { |
| 192 | + if (mounted) { |
| 193 | + setState(() { |
| 194 | + selected = !selected; |
| 195 | + }); |
| 196 | + } |
| 197 | + } else { |
| 198 | + widget.onTap(); |
| 199 | + } |
| 200 | + }, |
| 201 | + child: Container( |
| 202 | + margin: widget.margin ?? const EdgeInsets.all(0), |
| 203 | + padding: widget.padding ?? const EdgeInsets.all(0), |
| 204 | + child: AnimatedAlign( |
| 205 | + curve: widget.curve ?? Curves.linear, |
| 206 | + alignment: selected |
| 207 | + ? widget.alignment ?? Alignment.center |
| 208 | + : Alignment.topCenter, |
| 209 | + duration: widget.duration ?? const Duration(seconds: 2), |
| 210 | + child: widget.child, |
| 211 | + ), |
| 212 | + ), |
| 213 | + ); |
| 214 | + |
| 215 | + Widget buildAnimatedSizeWidget() => GestureDetector( |
| 216 | + onTap: widget.onTap, |
| 217 | + child: Container( |
| 218 | + margin: widget.margin ?? const EdgeInsets.all(0), |
| 219 | + padding: widget.padding ?? const EdgeInsets.all(0), |
| 220 | + color: widget.color ?? Colors.white, |
| 221 | + child: AnimatedSize( |
| 222 | + alignment: widget.alignment ?? Alignment.center, |
| 223 | + curve: widget.curve ?? Curves.linear, |
| 224 | + vsync: this, |
| 225 | + duration: widget.duration ?? const Duration(milliseconds: 2000), |
| 226 | + child: widget.child, |
| 227 | + ), |
| 228 | + ), |
| 229 | + ); |
| 230 | + |
| 231 | + Widget buildRotationTransitionWidget() => RotationTransition( |
| 232 | + turns: animation, |
| 233 | + child: widget.child, |
| 234 | + alignment: widget.alignment, |
| 235 | + ); |
| 236 | + |
| 237 | + Widget buildScaleTransitionWidget() => ScaleTransition( |
| 238 | + child: widget.child, |
| 239 | + scale: animation, |
| 240 | + alignment: widget.alignment ?? Alignment.center, |
| 241 | + ); |
| 242 | + |
| 243 | + Widget buildSlideTransitionWidget() => SlideTransition( |
| 244 | + child: widget.child, |
| 245 | + textDirection: widget.textDirection ?? TextDirection.ltr, |
| 246 | + position: widget.slidePosition ?? offsetAnimation, |
| 247 | + ); |
| 248 | + |
| 249 | + Widget buildAnimatedDefaultTextStyleWidget() => GestureDetector( |
| 250 | + onTap: widget.onTap, |
| 251 | + child: AnimatedDefaultTextStyle( |
| 252 | + maxLines: widget.maxLines, |
| 253 | + style: widget.style ?? |
| 254 | + TextStyle( |
| 255 | + fontWeight: widget.fontWeight ?? FontWeight.normal, |
| 256 | + fontSize: widget.fontSize ?? 16, |
| 257 | + color: widget.color ?? Colors.blue), |
| 258 | + textWidthBasis: widget.textWidthBasis ?? TextWidthBasis.parent, |
| 259 | + textAlign: widget.textAlign ?? TextAlign.start, |
| 260 | + curve: widget.curve ?? Curves.linear, |
| 261 | + duration: widget.duration ?? const Duration(seconds: 2), |
| 262 | + child: widget.child, |
| 263 | + ), |
| 264 | + ); |
| 265 | + |
| 266 | + Widget getAnimatedTypeWidget() { |
| 267 | + switch (widget.type) { |
| 268 | + case GFAnimationType.align: |
| 269 | + return buildAnimatedAlignWidget(); |
| 270 | + break; |
| 271 | + case GFAnimationType.container: |
| 272 | + return buildAnimatedContainerWidget(); |
| 273 | + break; |
| 274 | + case GFAnimationType.size: |
| 275 | + return buildAnimatedSizeWidget(); |
| 276 | + break; |
| 277 | + case GFAnimationType.rotateTransition: |
| 278 | + return buildRotationTransitionWidget(); |
| 279 | + break; |
| 280 | + case GFAnimationType.scaleTransition: |
| 281 | + return buildScaleTransitionWidget(); |
| 282 | + break; |
| 283 | + case GFAnimationType.slideTransition: |
| 284 | + return buildSlideTransitionWidget(); |
| 285 | + break; |
| 286 | + case GFAnimationType.textStyle: |
| 287 | + return buildAnimatedDefaultTextStyleWidget(); |
| 288 | + break; |
| 289 | + default: |
| 290 | + return buildAnimatedContainerWidget(); |
| 291 | + } |
| 292 | + } |
| 293 | +} |
0 commit comments