Created
April 12, 2019 16:01
-
-
Save kazhuyo/f61e4cc6fd6efd385f2d49579c9952e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| class AnimatedFab extends StatefulWidget { | |
| final Function() onPressed; | |
| final String tooltip; | |
| final IconData icon; | |
| AnimatedFab({this.onPressed, this.tooltip, this.icon}); | |
| @override | |
| _AnimatedFabState createState() => _AnimatedFabState(); | |
| } | |
| class _AnimatedFabState extends State<AnimatedFab> | |
| with SingleTickerProviderStateMixin { | |
| bool isOpened = false; | |
| AnimationController _animationController; | |
| Animation<Color> _buttonColor; | |
| Animation<double> _animateIcon; | |
| Animation<double> _translateButton; | |
| 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); | |
| _buttonColor = ColorTween( | |
| begin: Colors.blue[400], | |
| end: Colors.red, | |
| ).animate(CurvedAnimation( | |
| parent: _animationController, | |
| curve: Interval( | |
| 0.00, | |
| 1.00, | |
| curve: Curves.linear, | |
| ), | |
| )); | |
| _translateButton = Tween<double>( | |
| begin: 0, | |
| end: -60, | |
| ).animate(CurvedAnimation( | |
| parent: _animationController, | |
| curve: Interval( | |
| 0.0, | |
| 0.75, | |
| curve: _curve, | |
| ), | |
| )); | |
| super.initState(); | |
| } | |
| @override | |
| dispose() { | |
| _animationController.dispose(); | |
| super.dispose(); | |
| } | |
| animate() { | |
| if (!isOpened) { | |
| _animationController.forward(); | |
| } else { | |
| _animationController.reverse(); | |
| } | |
| isOpened = !isOpened; | |
| } | |
| Widget add() { | |
| return FloatingActionButton( | |
| shape: new BeveledRectangleBorder( | |
| borderRadius: new BorderRadius.circular(50.0) | |
| ), | |
| onPressed: null, | |
| heroTag: null, | |
| tooltip: 'Add', | |
| child: Icon(Icons.add), | |
| ); | |
| } | |
| Widget image() { | |
| return FloatingActionButton( | |
| shape: new BeveledRectangleBorder( | |
| borderRadius: new BorderRadius.circular(50.0) | |
| ), | |
| onPressed: null, | |
| heroTag: null, | |
| tooltip: 'Image', | |
| child: Icon(Icons.image), | |
| ); | |
| } | |
| Widget inbox() { | |
| return FloatingActionButton( | |
| shape: new BeveledRectangleBorder( | |
| borderRadius: new BorderRadius.circular(50.0) | |
| ), | |
| onPressed: null, | |
| heroTag: null, | |
| tooltip: 'Inbox', | |
| child: Icon(Icons.inbox), | |
| ); | |
| } | |
| Widget toggle() { | |
| return FloatingActionButton( | |
| shape: new BeveledRectangleBorder( | |
| borderRadius: new BorderRadius.circular(50.0) | |
| ), | |
| backgroundColor: _buttonColor.value, | |
| onPressed: animate, | |
| tooltip: 'Toggle', | |
| heroTag: null, | |
| child: AnimatedIcon( | |
| icon: AnimatedIcons.menu_close, | |
| color: Colors.white, | |
| progress: _animateIcon, | |
| ), | |
| ); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Stack( | |
| children: <Widget>[ | |
| Transform( | |
| transform: Matrix4.translationValues( | |
| 0.0, | |
| _translateButton.value * 1.8, | |
| 0.0, | |
| ), | |
| child: add(), | |
| ), | |
| Transform( | |
| transform: Matrix4.translationValues( | |
| _translateButton.value * 0.8, | |
| _translateButton.value * 1.0, | |
| 0.0, | |
| ), | |
| child: image(), | |
| ), | |
| Transform( | |
| transform: Matrix4.translationValues( | |
| _translateButton.value * 0.0, | |
| _translateButton.value * 1.0, | |
| 0.0, | |
| ), | |
| child: inbox(), | |
| ), | |
| toggle(), | |
| ], | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment