|
| 1 | +import 'dart:math'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:getflutter/types/gf_loader_type.dart'; |
| 4 | + |
| 5 | +class GFLoader extends StatefulWidget { |
| 6 | + const GFLoader( |
| 7 | + {Key key, |
| 8 | + this.loaderColorOne = Colors.redAccent, |
| 9 | + this.loaderColorTwo = Colors.green, |
| 10 | + this.loaderColorThree = Colors.blueAccent, |
| 11 | + this.duration = const Duration(milliseconds: 1000), |
| 12 | + this.loaderType = LoaderDotType.circle, |
| 13 | + this.loaderIcon = const Icon(Icons.blur_on)}) |
| 14 | + : super(key: key); |
| 15 | + |
| 16 | + final Color loaderColorOne; |
| 17 | + final Color loaderColorTwo; |
| 18 | + final Color loaderColorThree; |
| 19 | + final Duration duration; |
| 20 | + final LoaderDotType loaderType; |
| 21 | + final Icon loaderIcon; |
| 22 | + |
| 23 | + @override |
| 24 | + _GFLoaderState createState() => _GFLoaderState(); |
| 25 | +} |
| 26 | + |
| 27 | +class _GFLoaderState extends State<GFLoader> |
| 28 | + with SingleTickerProviderStateMixin { |
| 29 | + Animation<double> animation_1; |
| 30 | + Animation<double> animation_2; |
| 31 | + Animation<double> animation_3; |
| 32 | + AnimationController controller; |
| 33 | + |
| 34 | + @override |
| 35 | + void initState() { |
| 36 | + super.initState(); |
| 37 | + |
| 38 | + controller = AnimationController(duration: widget.duration, vsync: this); |
| 39 | + |
| 40 | + animation_1 = Tween(begin: 0, end: 1.0).animate( |
| 41 | + CurvedAnimation( |
| 42 | + parent: controller, |
| 43 | + curve: const Interval( |
| 44 | + 0, |
| 45 | + 0.70, |
| 46 | + curve: Curves.linear, |
| 47 | + ), |
| 48 | + ), |
| 49 | + ); |
| 50 | + |
| 51 | + animation_2 = Tween(begin: 0, end: 1.0).animate( |
| 52 | + CurvedAnimation( |
| 53 | + parent: controller, |
| 54 | + curve: const Interval( |
| 55 | + 0.1, |
| 56 | + 0.80, |
| 57 | + curve: Curves.linear, |
| 58 | + ), |
| 59 | + ), |
| 60 | + ); |
| 61 | + |
| 62 | + animation_3 = Tween(begin: 0, end: 1.0).animate( |
| 63 | + CurvedAnimation( |
| 64 | + parent: controller, |
| 65 | + curve: const Interval( |
| 66 | + 0.2, |
| 67 | + 0.90, |
| 68 | + curve: Curves.linear, |
| 69 | + ), |
| 70 | + ), |
| 71 | + ); |
| 72 | + |
| 73 | + controller.addListener(() { |
| 74 | + setState(() { |
| 75 | + //print(animation_1.value); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + controller.repeat(); |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + Widget build(BuildContext context) => Container( |
| 84 | + child: Row( |
| 85 | + mainAxisAlignment: MainAxisAlignment.center, |
| 86 | + children: <Widget>[ |
| 87 | + Opacity( |
| 88 | + opacity: animation_1.value <= 0.4 |
| 89 | + ? 2.5 * animation_1.value |
| 90 | + : (animation_1.value > 0.40 && animation_1.value <= 0.60) |
| 91 | + ? 1.0 |
| 92 | + : 2.5 - (2.5 * animation_1.value), |
| 93 | +// opacity: (animation_1.value <= 0.4 ? 2.5 * animation_1.value : (animation_1.value > 0.40 && animation_1.value <= 0.60) ? 1.0 : 2.5 - (2.5 * animation_1.value)), |
| 94 | + child: Padding( |
| 95 | + padding: const EdgeInsets.only(right: 8), |
| 96 | + child: Dot( |
| 97 | + radius: 10, |
| 98 | + color: widget.loaderColorOne, |
| 99 | + type: widget.loaderType, |
| 100 | + icon: widget.loaderIcon, |
| 101 | + ), |
| 102 | + ), |
| 103 | + ), |
| 104 | + Opacity( |
| 105 | + opacity: animation_2.value <= 0.4 |
| 106 | + ? 2.5 * animation_2.value |
| 107 | + : (animation_2.value > 0.40 && animation_2.value <= 0.60) |
| 108 | + ? 1.0 |
| 109 | + : 2.5 - (2.5 * animation_2.value), |
| 110 | + child: Padding( |
| 111 | + padding: const EdgeInsets.only(right: 8), |
| 112 | + child: Dot( |
| 113 | + radius: 10, |
| 114 | + color: widget.loaderColorTwo, |
| 115 | + type: widget.loaderType, |
| 116 | + icon: widget.loaderIcon, |
| 117 | + ), |
| 118 | + ), |
| 119 | + ), |
| 120 | + Opacity( |
| 121 | + opacity: animation_3.value <= 0.4 |
| 122 | + ? 2.5 * animation_3.value |
| 123 | + : (animation_3.value > 0.40 && animation_3.value <= 0.60) |
| 124 | + ? 1.0 |
| 125 | + : 2.5 - (2.5 * animation_3.value), |
| 126 | + child: Padding( |
| 127 | + padding: const EdgeInsets.only(right: 8), |
| 128 | + child: Dot( |
| 129 | + radius: 10, |
| 130 | + color: widget.loaderColorThree, |
| 131 | + type: widget.loaderType, |
| 132 | + icon: widget.loaderIcon, |
| 133 | + ), |
| 134 | + ), |
| 135 | + ), |
| 136 | + ], |
| 137 | + ), |
| 138 | + ); |
| 139 | + |
| 140 | + @override |
| 141 | + void dispose() { |
| 142 | + controller.dispose(); |
| 143 | + super.dispose(); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +class Dot extends StatelessWidget { |
| 148 | + const Dot({Key key, this.radius, this.color, this.type, this.icon}) |
| 149 | + : super(key: key); |
| 150 | + |
| 151 | + final double radius; |
| 152 | + final Color color; |
| 153 | + final LoaderDotType type; |
| 154 | + final Icon icon; |
| 155 | + |
| 156 | + @override |
| 157 | + Widget build(BuildContext context) => Center( |
| 158 | + child: type == LoaderDotType.icon |
| 159 | + ? Icon( |
| 160 | + icon.icon, |
| 161 | + color: color, |
| 162 | + size: 1.3 * radius, |
| 163 | + ) |
| 164 | + : Transform.rotate( |
| 165 | + angle: type == LoaderDotType.diamond ? pi / 4 : 0.0, |
| 166 | + child: Container( |
| 167 | + width: radius, |
| 168 | + height: radius, |
| 169 | + decoration: BoxDecoration( |
| 170 | + color: color, |
| 171 | + shape: type == LoaderDotType.circle |
| 172 | + ? BoxShape.circle |
| 173 | + : BoxShape.rectangle), |
| 174 | + ), |
| 175 | + ), |
| 176 | + ); |
| 177 | +} |
0 commit comments