Skip to content

Instantly share code, notes, and snippets.

@yathit
Created March 14, 2018 01:21
Show Gist options
  • Save yathit/db6c6f0673c46e71b1b4e699e4d1ed07 to your computer and use it in GitHub Desktop.
Save yathit/db6c6f0673c46e71b1b4e699e4d1ed07 to your computer and use it in GitHub Desktop.
Flutter animated icon
import 'dart:async';
import 'package:flutter/material.dart';
class HighLightedIcon extends StatefulWidget {
final IconData icon;
final double size;
final Color color;
HighLightedIcon(
this.icon, {
Key key,
this.size = 24.0,
this.color,
})
: super(key: key);
@override
State<HighLightedIcon> createState() {
return new _AnimatedIconState();
}
}
class _AnimatedIconState extends State<HighLightedIcon>
with SingleTickerProviderStateMixin {
final double dx = 4.0;
AnimationController controller;
Animation<double> animation;
@override
initState() {
super.initState();
controller = new AnimationController(
duration: const Duration(milliseconds: 300), vsync: this);
animation = new Tween(begin: widget.size, end: widget.size + dx)
.animate(controller);
animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.reverse();
} else if (status == AnimationStatus.dismissed) {
new Future.delayed(const Duration(seconds: 2), () {
if (!mounted) return;
controller?.forward();
});
}
});
controller.forward();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new _Animator(
icon: widget.icon,
animation: animation,
color: widget.color,
size: widget.size + dx,
);
}
}
class _Animator extends AnimatedWidget {
final double size;
final IconData icon;
final Color color;
_Animator({
Key key,
this.icon,
this.size,
this.color,
Animation<double> animation,
})
: super(key: key, listenable: animation);
@override
Widget build(BuildContext context) {
final Animation<double> animation = listenable;
return new Container(
width: size,
height: size,
child: new Center(
child: new Icon(
icon,
size: animation.value,
color: color,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment