FavAnimation.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // import 'package:flutter/material.dart';
  2. // class AnimateFav extends StatefulWidget {
  3. // AnimateFav({Key? key, this.size}) : super(key: key);
  4. // final double size;
  5. // _AnimateFavState createState() => _AnimateFavState();
  6. // }
  7. // class _AnimateFavState extends State<AnimateFav>
  8. // with TickerProviderStateMixin {
  9. // AnimationController _controller_1;
  10. // AnimationController _controller_2;
  11. // Animation<double> _animation_1;
  12. // Animation<double> _animation_2;
  13. // Animation<double> curAnimation;
  14. // double rpx;
  15. // @override
  16. // void initState() {
  17. // super.initState();
  18. // _controller_1 =
  19. // AnimationController(vsync: this, duration: Duration(milliseconds: 300));
  20. // _controller_2 =
  21. // AnimationController(vsync: this, duration: Duration(milliseconds: 200));
  22. // _animation_1 = Tween(begin: 0.0, end: 1.3).animate(_controller_1)
  23. // ..addStatusListener((status) {
  24. // if (status == AnimationStatus.completed) {
  25. // _controller_2.forward(from: 0);
  26. // setState(() {
  27. // curAnimation = _animation_2;
  28. // });
  29. // }
  30. // })
  31. // ..addListener(() {
  32. // setState(() {});
  33. // });
  34. // _animation_2 = Tween(begin: 1.3, end: 1.0).animate(_controller_2)
  35. // ..addListener(() {
  36. // setState(() {});
  37. // });
  38. // curAnimation = _animation_1;
  39. // _controller_1.forward(from: 0);
  40. // }
  41. // @override
  42. // Widget build(BuildContext context) {
  43. // rpx = MediaQuery.of(context).size.width / 750;
  44. // return Center(
  45. // child: Icon(
  46. // Icons.favorite,
  47. // size: widget.size * curAnimation.value,
  48. // color: Colors.redAccent,
  49. // ));
  50. // }
  51. // }
  52. // class AnimatedUnFav extends StatefulWidget {
  53. // AnimatedUnFav({Key? key,required this.size}) : super(key: key);
  54. // final double size;
  55. // _AnimatedUnFavState createState() => _AnimatedUnFavState();
  56. // }
  57. // class _AnimatedUnFavState extends State<AnimatedUnFav> with TickerProviderStateMixin {
  58. // AnimationController _controller_1;
  59. // AnimationController _controller_2;
  60. // Animation<double> _animation_1;
  61. // Animation<double> _animation_2;
  62. // Animation<double> curAnimation;
  63. // Color curColor;
  64. // @override
  65. // void initState() {
  66. // super.initState();
  67. // curColor=Colors.redAccent;
  68. // _controller_1 =
  69. // AnimationController(vsync: this, duration: Duration(milliseconds: 100));
  70. // _controller_2 =
  71. // AnimationController(vsync: this, duration: Duration(milliseconds: 100));
  72. // _animation_1 = Tween(begin: 1.0, end: 1.2).animate(_controller_1)
  73. // ..addStatusListener((status) {
  74. // if (status == AnimationStatus.completed) {
  75. // _controller_2.forward(from: 0);
  76. // setState(() {
  77. // curAnimation = _animation_2;
  78. // curColor=Colors.grey[100];
  79. // });
  80. // }
  81. // })
  82. // ..addListener(() {
  83. // setState(() {});
  84. // });
  85. // _animation_2 = Tween(begin: 1.2, end: 1.0).animate(_controller_2)
  86. // ..addListener(() {
  87. // setState(() {});
  88. // });
  89. // curAnimation = _animation_1;
  90. // _controller_1.forward(from: 0);
  91. // }
  92. // @override
  93. // Widget build(BuildContext context) {
  94. // // rpx = MediaQuery.of(context).size.width / 750;
  95. // return Center(
  96. // child: Icon(
  97. // Icons.favorite,
  98. // size: widget.size * curAnimation.value,
  99. // color: curColor,
  100. // ));
  101. // }
  102. // }
  103. import 'dart:async';
  104. import 'package:douyin_demo/providers/recommend_provider.dart';
  105. import 'package:flutter/material.dart';
  106. // import 'package:provider/provider.dart';
  107. // class AnimatePositiveIcon extends StatefulWidget {
  108. // AnimatePositiveIcon({Key? key, required this.size, this.callback})
  109. // : super(key: key);
  110. // final double size;
  111. // final VoidCallback callback;
  112. // _AnimatePositiveIconState createState() => _AnimatePositiveIconState();
  113. // }
  114. // class _AnimatePositiveIconState extends State<AnimatePositiveIcon>
  115. // with TickerProviderStateMixin {
  116. // AnimationController _controller1;
  117. // AnimationController _controller2;
  118. // AnimationController _controller3;
  119. // Animation<double> _animation1;
  120. // Animation<double> _animation2;
  121. // Animation<double> _animation3;
  122. // Animation<double> curAnimation;
  123. // Color curColor;
  124. // @override
  125. // void initState() {
  126. // super.initState();
  127. // _controller1 =
  128. // AnimationController(vsync: this, duration: Duration(milliseconds: 150));
  129. // _controller2 =
  130. // AnimationController(vsync: this, duration: Duration(milliseconds: 200));
  131. // _controller3 =
  132. // AnimationController(vsync: this, duration: Duration(milliseconds: 60));
  133. // curColor = Colors.grey[100];
  134. // _animation1 = Tween(begin: 1.0, end: 0.0).animate(_controller1)
  135. // ..addListener(() {
  136. // setState(() {});
  137. // })
  138. // ..addStatusListener((status) {
  139. // if (status == AnimationStatus.completed) {
  140. // _controller2.forward(from: 0);
  141. // curAnimation = _animation2;
  142. // curColor = Colors.redAccent;
  143. // }
  144. // });
  145. // _animation2 = Tween(begin: 0.0, end: 1.2).animate(_controller2)
  146. // ..addListener(() {
  147. // setState(() {});
  148. // })
  149. // ..addStatusListener((status) {
  150. // if (status == AnimationStatus.completed) {
  151. // _controller3.forward(from: 0);
  152. // curAnimation = _animation3;
  153. // }
  154. // });
  155. // _animation3 = Tween(begin: 1.2, end: 1.0).animate(_controller3)
  156. // ..addListener(() {
  157. // setState(() {});
  158. // })
  159. // ..addStatusListener((status) {
  160. // if (status == AnimationStatus.completed && widget.callback != null) {
  161. // widget.callback();
  162. // }
  163. // });
  164. // // _controller1.forward(from: 0).then((_) {
  165. // // _controller2.forward(from: 0).then((_){
  166. // // _controller3.forward(from: 0);
  167. // // });
  168. // // });
  169. // curAnimation = _animation1;
  170. // _controller1.forward(from: 0);
  171. // }
  172. // @override
  173. // Widget build(BuildContext context) {
  174. // return Container(
  175. // child: Icon(
  176. // Icons.favorite,
  177. // size: widget.size * curAnimation.value,
  178. // color: curColor,
  179. // ),
  180. // );
  181. // }
  182. // }
  183. // class AnimateNegtiveIcon extends StatefulWidget {
  184. // AnimateNegtiveIcon(
  185. // {Key? key,
  186. // required this.size,
  187. // required this.icon,
  188. // required this.startColor,
  189. // required this.endColor,
  190. // this.callback})
  191. // : super(key: key);
  192. // final double size;
  193. // final IconData icon;
  194. // final VoidCallback callback;
  195. // final Color startColor;
  196. // final Color endColor;
  197. // _AnimateNegtiveIconState createState() => _AnimateNegtiveIconState();
  198. // }
  199. // class _AnimateNegtiveIconState extends State<AnimateNegtiveIcon>
  200. // with TickerProviderStateMixin {
  201. // AnimationController _controller1;
  202. // AnimationController _controller2;
  203. // Animation<double> _animation1;
  204. // Animation<double> _animation2;
  205. // Animation<double> curAnimation;
  206. // Color curColor;
  207. // @override
  208. // void initState() {
  209. // super.initState();
  210. // _controller1 =
  211. // AnimationController(vsync: this, duration: Duration(milliseconds: 200));
  212. // _controller2 =
  213. // AnimationController(vsync: this, duration: Duration(milliseconds: 200));
  214. // curColor = widget.startColor;
  215. // _animation1 = Tween(begin: 1.0, end: 1.2).animate(_controller1)
  216. // ..addListener(() {
  217. // setState(() {});
  218. // })
  219. // ..addStatusListener((status) {
  220. // if (status == AnimationStatus.completed) {
  221. // _controller2.forward(from: 0);
  222. // curAnimation = _animation2;
  223. // curColor = widget.endColor;
  224. // }
  225. // });
  226. // _animation2 = Tween(begin: 1.2, end: 1.0).animate(_controller2)
  227. // ..addListener(() {
  228. // setState(() {});
  229. // })
  230. // ..addStatusListener((status) {
  231. // if (status == AnimationStatus.completed && widget.callback != null) {
  232. // Timer(Duration(milliseconds: 100), () {
  233. // widget.callback();
  234. // });
  235. // }
  236. // });
  237. // // _controller1.forward(from: 0).then((_) {
  238. // // _controller2.forward(from: 0).then((_){
  239. // // _controller3.forward(from: 0);
  240. // // });
  241. // // });
  242. // curAnimation = _animation1;
  243. // }
  244. // @override
  245. // Widget build(BuildContext context) {
  246. // return IconButton(
  247. // padding: EdgeInsets.all(0),
  248. // onPressed: () {
  249. // _controller1.forward(from: 0);
  250. // },
  251. // icon: Icon(
  252. // widget.icon,
  253. // semanticLabel: "label",
  254. // size: widget.size * curAnimation.value,
  255. // color: curColor,
  256. // ));
  257. // }
  258. // }
  259. class AnimatedIconWidget extends StatefulWidget {
  260. AnimatedIconWidget(
  261. {Key? key,
  262. required this.animationList,
  263. required this.icon,
  264. required this.size,
  265. required this.callback,
  266. required this.callbackDelay,
  267. required this.provider})
  268. : super(key: key);
  269. final List<IconAnimationStage> animationList;
  270. final IconData icon;
  271. final VoidCallback callback;
  272. final double size;
  273. final RecommendProvider provider;
  274. final Duration callbackDelay;
  275. _AnimatedIconWidgetState createState() => _AnimatedIconWidgetState();
  276. }
  277. class _AnimatedIconWidgetState extends State<AnimatedIconWidget>
  278. with TickerProviderStateMixin {
  279. List<IconAnimationStage> anis = <IconAnimationStage>[];
  280. List<AnimationController> controllers = <AnimationController>[];
  281. List<Animation<double>> animations = <Animation<double>>[];
  282. late Animation<double> curAnim;
  283. late Color curColor;
  284. int curIndex = 0;
  285. List<bool> ifAdded = <bool>[];
  286. late double curSize;
  287. bool ifInit = true;
  288. loopAnimation(index) {
  289. curColor = curColor == null ? anis.first.color : curColor;
  290. if (index < controllers.length - 1) {
  291. if (!ifAdded[index]) {
  292. animations[index] = animations[index]
  293. ..addStatusListener((status) {
  294. if (status == AnimationStatus.completed) {
  295. curAnim = animations[index + 1];
  296. curColor = anis[index + 1].color;
  297. controllers[index + 1].forward(from: 0);
  298. loopAnimation(index + 1);
  299. }
  300. })
  301. ..addListener(() {
  302. setState(() {});
  303. });
  304. ifAdded[index] = true;
  305. }
  306. } else if (index == controllers.length - 1) {
  307. if (!ifAdded[index]) {
  308. animations[index] = animations[index]
  309. ..addStatusListener((status) {
  310. curColor = anis[index].color;
  311. if (status == AnimationStatus.completed) {
  312. if (widget.callbackDelay == null) {
  313. widget.callback();
  314. } else {
  315. Timer(widget.callbackDelay, () {
  316. widget.callback();
  317. });
  318. }
  319. }
  320. })
  321. ..addListener(() {
  322. setState(() {});
  323. });
  324. ifAdded[index] = true;
  325. }
  326. }
  327. }
  328. @override
  329. void initState() {
  330. super.initState();
  331. anis = widget.animationList;
  332. List.generate(anis.length, (index) {
  333. var curAni = anis[index];
  334. ifAdded.add(false);
  335. controllers.add(
  336. AnimationController(vsync: this, duration: anis[index].duration));
  337. animations.add(Tween(begin: curAni.start, end: curAni.end)
  338. .animate(controllers[index]));
  339. });
  340. curAnim = animations.first;
  341. loopAnimation(0);
  342. }
  343. @override
  344. Widget build(BuildContext context) {
  345. return IconButton(
  346. padding: EdgeInsets.all(0),
  347. onPressed: () {
  348. controllers.first.forward(from: 0);
  349. },
  350. icon: Icon(
  351. widget.icon,
  352. size: widget.size * curAnim.value,
  353. color: curColor,
  354. ),
  355. );
  356. }
  357. }
  358. class IconAnimationStage {
  359. double start;
  360. double end;
  361. Color color;
  362. Duration duration;
  363. IconAnimationStage(
  364. {required this.color,
  365. required this.duration,
  366. required this.end,
  367. required this.start});
  368. }