Skip to content

Instantly share code, notes, and snippets.

@miguelpruivo
Created April 30, 2019 10:18
Show Gist options
  • Save miguelpruivo/2b3143e7e403f8c903d2a991e092ffd7 to your computer and use it in GitHub Desktop.
Save miguelpruivo/2b3143e7e403f8c903d2a991e092ffd7 to your computer and use it in GitHub Desktop.
An animated clock toggle widget for setting alarms in lists or similliar.
class AnimatedClock extends StatefulWidget {
final bool isToggled;
const AnimatedClock({
Key key,
this.isToggled,
}) : super(key: key);
@override
_AnimatedClockState createState() => _AnimatedClockState();
}
class _AnimatedClockState extends State<AnimatedClock> with SingleTickerProviderStateMixin {
AnimationController _animationController;
Animation<double> _animation;
@override
void initState() {
super.initState();
_animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 300));
_animation = Tween(begin: 0.0, end: 10.0).chain(CurveTween(curve: Curves.elasticIn)).animate(_animationController)
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
_animationController.reverse();
}
});
}
@override
void didUpdateWidget(AnimatedClock oldWidget) {
super.didUpdateWidget(oldWidget);
if (!oldWidget.isToggled && widget.isToggled) {
_animationController.forward();
}
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (BuildContext context, child) {
return Padding(
padding: EdgeInsets.only(left: _animation.value + 10.0, right: 10.0 - _animation.value),
child: Icon(
widget.isToggled ? Icon.alarm_on : Icon.alarm_off,
size: 20.0,
),
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment