Last active
May 27, 2024 13:08
-
-
Save ashutoshsr7/c43ba3fcbbfcd38cd204d58f08517116 to your computer and use it in GitHub Desktop.
Flutter- Explicit Animations
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'; | |
| void main() => runApp(MyApp()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| home: MyAnimationWidget(), | |
| ); | |
| } | |
| } | |
| class MyAnimationWidget extends StatefulWidget { | |
| @override | |
| _MyAnimationWidgetState createState() => _MyAnimationWidgetState(); | |
| } | |
| class _MyAnimationWidgetState extends State<MyAnimationWidget> with SingleTickerProviderStateMixin { | |
| late AnimationController _controller; | |
| late Animation<double> _sizeAnimation; | |
| late Animation<Color?> _colorAnimation; | |
| late Animation<double> _borderRadiusAnimation; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _controller = AnimationController( | |
| duration: const Duration(seconds: 2), | |
| vsync: this, | |
| ); | |
| _sizeAnimation = Tween<double>(begin: 50, end: 200) | |
| .animate(CurvedAnimation(parent: _controller, curve: Curves.easeInOut)); | |
| _colorAnimation = ColorTween(begin: Colors.blue, end: Colors.red) | |
| .animate(CurvedAnimation(parent: _controller, curve: Curves.linear)); | |
| _borderRadiusAnimation = Tween<double>(begin: 0, end: 50) | |
| .animate(CurvedAnimation(parent: _controller, curve: Curves.easeInOut)); | |
| _controller.addListener(() { | |
| setState(() {}); | |
| }); | |
| } | |
| @override | |
| void dispose() { | |
| _controller.dispose(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text('Explicit Animations in Flutter'), | |
| ), | |
| body: Center( | |
| child: AnimatedBuilder( | |
| animation: _controller, | |
| builder: (context, child) { | |
| return Container( | |
| width: _sizeAnimation.value, | |
| height: _sizeAnimation.value, | |
| decoration: BoxDecoration( | |
| color: _colorAnimation.value, | |
| borderRadius: BorderRadius.circular(_borderRadiusAnimation.value), | |
| ), | |
| ); | |
| }, | |
| ), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () { | |
| if (_controller.isAnimating) { | |
| _controller.stop(); | |
| } else { | |
| _controller.repeat(reverse: true); | |
| } | |
| }, | |
| child: Icon(Icons.play_arrow), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment