my_like_button.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:flutter/material.dart';
  3. import 'my_load_image.dart';
  4. class MyLikeButton extends StatefulWidget {
  5. bool isLiked;
  6. bool isCustomIcon;
  7. final String? customIconActiveAssets;
  8. final String? customIconUnActiveAssets;
  9. double customIconWidth;
  10. double customIconHeight;
  11. final VoidCallback? onLike;
  12. final double size;
  13. final Color likedColor;
  14. final Color unlikedColor;
  15. final IconData activeIcon;
  16. final IconData unActiveIcon;
  17. MyLikeButton({
  18. Key? key,
  19. this.isLiked = false,
  20. this.isCustomIcon = false,
  21. this.customIconActiveAssets = Assets.communityLikeActive,
  22. this.customIconUnActiveAssets = Assets.communityLike,
  23. this.customIconWidth = 16.0,
  24. this.customIconHeight = 16.0,
  25. this.onLike,
  26. this.size = 24.0,
  27. this.likedColor = Colors.red,
  28. this.unlikedColor = Colors.grey,
  29. this.activeIcon = Icons.favorite,
  30. this.unActiveIcon = Icons.favorite_border,
  31. }) : super(key: key);
  32. @override
  33. MyLikeButtonState createState() => MyLikeButtonState();
  34. }
  35. class MyLikeButtonState extends State<MyLikeButton> with SingleTickerProviderStateMixin {
  36. late AnimationController _controller;
  37. late Animation<double> _scaleAnimation;
  38. @override
  39. void initState() {
  40. super.initState();
  41. _controller = AnimationController(
  42. duration: const Duration(milliseconds: 150),
  43. vsync: this,
  44. );
  45. _scaleAnimation = Tween<double>(begin: 1.0, end: 1.2).animate(_controller)
  46. ..addStatusListener((status) {
  47. if (status == AnimationStatus.completed) {
  48. _controller.reverse();
  49. }
  50. });
  51. }
  52. @override
  53. void dispose() {
  54. _controller.dispose();
  55. super.dispose();
  56. }
  57. void _handleTap() {
  58. setState(() {
  59. widget.isLiked = !widget.isLiked;
  60. });
  61. _controller.forward(from: 0.0);
  62. widget.onLike?.call();
  63. }
  64. void triggerTap(){
  65. _handleTap();
  66. }
  67. @override
  68. Widget build(BuildContext context) {
  69. return GestureDetector(
  70. onTap: _handleTap,
  71. child: AnimatedBuilder(
  72. animation: _scaleAnimation,
  73. builder: (context, child) {
  74. return Transform.scale(
  75. scale: _scaleAnimation.value,
  76. child: widget.isCustomIcon ?
  77. MyAssetImage(
  78. widget.isLiked ? widget.customIconActiveAssets??'': widget.customIconUnActiveAssets??'',
  79. width: widget.customIconWidth,
  80. height: widget.customIconHeight,
  81. ) : Icon(
  82. widget.isLiked ? widget.activeIcon : widget.unActiveIcon,
  83. size: widget.size,
  84. color: widget.isLiked ? widget.likedColor : widget.unlikedColor,
  85. ),
  86. );
  87. },
  88. ),
  89. );
  90. }
  91. }