import 'package:flutter/material.dart';
class PlayPauseButton extends StatefulWidget {
final bool isPlaying;
final VoidCallback onPressed;
PlayPauseButton({required this.isPlaying, required this.onPressed});
@override
_PlayPauseButtonState createState() => _PlayPauseButtonState();
}
class _PlayPauseButtonState extends State<PlayPauseButton> with SingleTickerProviderStateMixin {
late AnimationController _animationController;
late Animation<double> _animation;
@override
void initState() {
super.initState();
_animationController = AnimationController(
duration: Duration(milliseconds: 300),
vsync: this,
);
_animation = Tween<double>(begin: 0.0, end: 1.0).animate(_animationController);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
void didUpdateWidget(covariant PlayPauseButton oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.isPlaying) {
_animationController.forward();
} else {
_animationController.reverse();
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onPressed,
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.transparent,
),
child: Stack(
children: [
Center(
child: Container(
width: 40,
height: 40,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black,
),
),
),
Center(
child: ScaleTransition(
scale: _animation,
child: Icon(
widget.isPlaying ? Icons.pause : Icons.play_arrow,
size: 30,
color: Colors.white,
),
),
),
],
),
),
);
}
}