Skip to content

Feat: Add advanced animation system to LikeButton component #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 86 additions & 22 deletions lib/views/widgets/like_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,110 @@ class LikeButton extends StatefulWidget {

class _LikeButtonState extends State<LikeButton>
with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
duration: const Duration(milliseconds: 200), vsync: this, value: 1.0);

late final AnimationController _controller;
late Animation<double> _scaleAnimation;
late Animation<Color?> _colorAnimation;
late bool _isFavorite;

@override
void initState() {
super.initState();
_isFavorite = widget.isLikedByUser;

_controller = AnimationController(
duration: const Duration(milliseconds: 400),
vsync: this,
);

// Bounce animation sequence
_scaleAnimation = TweenSequence<double>(
[
TweenSequenceItem(
tween: Tween(begin: 1.0, end: 1.2),
weight: 40,
), // Scale up
TweenSequenceItem(
tween: Tween(begin: 1.2, end: 0.9),
weight: 30,
), // Overshoot
TweenSequenceItem(
tween: Tween(begin: 0.9, end: 1.0),
weight: 30,
), // Settle back
],
).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
),
);

// Color transition animation
_colorAnimation = ColorTween(
begin: Colors.grey,
end: widget.tintColor,
).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.5), // Color change happens in first half
),
);

// Initialize animation state based on initial favorite status
if (_isFavorite) {
_controller.value = 1.0;
}

}

@override
void didUpdateWidget(LikeButton oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.isLikedByUser != oldWidget.isLikedByUser) {
setState(() {
_isFavorite = widget.isLikedByUser;
});
if (_isFavorite) {
_controller.forward();
} else {
_controller.reverse();
}
}
}

@override
void dispose() {
super.dispose();
_controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () async {
setState(() {
_isFavorite = !_isFavorite;
});
_controller.reverse().then((value) => _controller.forward());
await widget.onLiked(_isFavorite);
setState(() =>
_isFavorite = !_isFavorite);

if (_isFavorite) {
await _controller.forward(from: 0.0);
} else {
await _controller.reverse();
}

widget.onLiked(_isFavorite);
},
child: ScaleTransition(
scale: Tween(begin: 0.7, end: 1.0).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeOut)),
child: _isFavorite
? Icon(
Icons.favorite,
size: 40,
color: widget.tintColor,
)
: const Icon(
Icons.favorite_border,
size: 40,
),
child: AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Transform.scale(
scale: _scaleAnimation.value,
child: Icon(
_isFavorite ? Icons.favorite : Icons.favorite_border,
size: 40,
color: _colorAnimation.value,
),
);
},
),
);
}
Expand Down