Skip to content

Instantly share code, notes, and snippets.

@agungsb
Last active May 31, 2018 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agungsb/e85005b0e430781cd1e65829601542d7 to your computer and use it in GitHub Desktop.
Save agungsb/e85005b0e430781cd1e65829601542d7 to your computer and use it in GitHub Desktop.
Create A Simple Animated FloatingActionButton in Flutter
import 'package:flutter/material.dart';
class FancyFab extends StatefulWidget {
@override
_FancyFabState createState() => _FancyFabState();
}
class _FancyFabState extends State<FancyFab>
with SingleTickerProviderStateMixin {
bool isOpened = false;
AnimationController _animationController;
Animation<Color> _animateColor;
Animation<double> _animateIcon;
Curve _curve = Curves.easeOut;
@override
initState() {
_animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500))
..addListener(() {
setState(() {});
});
_animateIcon =
Tween<double>(begin: 0.0, end: 1.0).animate(_animationController);
_animateColor = ColorTween(
begin: Colors.blue,
end: Colors.red,
).animate(CurvedAnimation(
parent: _animationController,
curve: Interval(
0.00,
1.00,
curve: _curve,
),
));
super.initState();
}
@override
dispose() {
_animationController.dispose();
super.dispose();
}
animate() {
if (!isOpened) {
_animationController.forward();
} else {
_animationController.reverse();
}
isOpened = !isOpened;
}
Widget toggle() {
return FloatingActionButton(
backgroundColor: _animateColor.value,
onPressed: animate,
tooltip: 'Toggle',
child: AnimatedIcon(
icon: AnimatedIcons.menu_close,
progress: _animateIcon,
),
);
}
@override
Widget build(BuildContext context) {
return toggle();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment