Flutter Animation
This section will introduce the implementation of Animation in Flutter, including Implicit Animation and Explicit Animation.\\n\\n* * *\\n\\n## Implicit Animation\\n\\nImplicit AnimationAn Animation Widget whose transition effects are automatically handled by Flutter.\\n\\n## Example: AnimatedContainer\\n\\nclass AnimatedContainerExample extends StatefulWidget {\\n\\nconst AnimatedContainerExample({super.key});\\n\\n@override\\n\\n State createState()=>\\n\\n _AnimatedContainerExampleState();\\n\\n}\\n\\nclass _AnimatedContainerExampleState\\n\\nextends State{\\n\\nbool _isLarge =false;\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn Column(\\n\\n children:[\\n\\n// Tap to toggle size\\n\\n GestureDetector(\\n\\n onTap:()=> setState(()=> _isLarge =!_isLarge),\\n\\n child: AnimatedContainer(\\n\\n duration:const Duration(milliseconds:500),// AnimationDuration\\n\\n curve: Curves.easeInOut,// AnimationCurve\\n\\n width: _isLarge ?200:100,\\n\\n height: _isLarge ?200:100,\\n\\n color: _isLarge ? Colors.blue: Colors.red,\\n\\n child: Center(\\n\\n child: Text(\\n\\n _isLarge ?'Da':'Small',\\n\\n style:const TextStyle(color: Colors.white, fontSize:24),\\n\\n),\\n\\n),\\n\\n),\\n\\n),\\n\\nconst SizedBox(height:20),\\n\\n ElevatedButton(\\n\\n onPressed:()=> setState(()=> _isLarge =!_isLarge),\\n\\n child:const Text('Switch'),\\n\\n),\\n\\n],\\n\\n);\\n\\n}\\n\\n}\\n\\n### Common Implicit Animation Widgets\\n\\n| Widget | Description |\\n| --- | --- |\\n| AnimatedContainer | Animation for changes in container properties like size and color |\\n| AnimatedOpacity | Animation for opacity changes |\\n| AnimatedPadding | Animation for padding changes |\\n| AnimatedAlign | Animation for alignment changes |\\n| AnimatedSwitcher | SwitchExample Widget Animation when |\\n| AnimatedDefaultTextStyle | Animation for text style changes |\\n\\n* * *\\n\\n## Explicit Animation\\n\\nExplicit AnimationUse AnimationController to fully control Animation.\\n\\n## Example: Rotation Animation\\n\\nclass RotationAnimation extends StatefulWidget {\\n\\nconst RotationAnimation({super.key});\\n\\n@override\\n\\n State createState()=> _RotationAnimationState();\\n\\n}\\n\\nclass _RotationAnimationState extends State\\n\\nwith SingleTickerProviderStateMixin {\\n\\n late AnimationController _controller;\\n\\n late Animation _animation;\\n\\n@override\\n\\nvoid initState(){\\n\\nsuper.initState();\\n\\n// Create Animation Controller\\n\\n _controller = AnimationController(\\n\\n duration:const Duration(seconds:2),\\n\\n vsync:this,\\n\\n);\\n\\n// Create Rotation Animation (maps 0 to 1 to 0 to 2Ο)\\n\\n _animation = Tween(begin:0, end:6.28).animate(\\n\\n CurvedAnimation(parent: _controller, curve: Curves.linear),\\n\\n);\\n\\n// Start Animation\\n\\n _controller.repeat();\\n\\n}\\n\\n@override\\n\\nvoid dispose(){\\n\\n _controller.dispose();\\n\\nsuper.dispose();\\n\\n}\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn Center(\\n\\n child: AnimatedBuilder(\\n\\n animation: _animation,\\n\\n builder:(context, child){\\n\\nreturn Transform.rotate(\\n\\n angle: _animation.value,// Rotation Angle\\n\\n child:const Icon(Icons.refresh, size:100),\\n\\n);\\n\\n},\\n\\n),\\n\\n);\\n\\n}\\n\\n}\\n\\n* * *\\n\\n## Staggered Animation\\n\\n## Example: Staggered Animation\\n\\nclass StaggeredAnimation extends StatefulWidget {\\n\\nconst StaggeredAnimation({super.key});\\n\\n@override\\n\\n State createState()=> _StaggeredAnimationState();\\n\\n}\\n\\nclass _StaggeredAnimationState extends State\\n\\nwith TickerProviderStateMixin {\\n\\n late AnimationController _controller;\\n\\n// Individual Animations\\n\\n late Animation _fadeAnimation;\\n\\n late Animation _scaleAnimation;\\n\\n late Animation _slideAnimation;\\n\\n@override\\n\\nvoid initState(){\\n\\nsuper.initState();\\n\\n _controller = AnimationController(\\n\\n duration:const Duration(milliseconds:1500),\\n\\n vsync:this,\\n\\n);\\n\\n// Fade-in Animation (0-0.5οΌ\\n\\n _fadeAnimation = Tween(begin:0, end:1).animate(\\n\\n CurvedAnimation(\\n\\n parent: _controller,\\n\\n curve:const Interval(0,0.5, curve: Curves.easeOut),\\n\\n),\\n\\n);\\n\\n// Scale Animation (0.3-0.7οΌ\\n\\n _scaleAnimation = Tween(begin:0.5, end:1).animate(\\n\\n CurvedAnimation(\\n\\n parent: _controller,\\n\\n curve:const Interval(0.3,0.7, curve: Curves.elasticOut),\\n\\n),\\n\\n);\\n\\n// Slide-in Animation (0.5-1.0οΌ\\n\\n _slideAnimation = Tween(\\n\\n begin:const Offset(0,0.5),\\n\\n end: Offset.zero,\\n\\n).animate(\\n\\n CurvedAnimation(\\n\\n parent: _controller,\\n\\n curve:const Interval(0.5,1, curve: Curves.easeOutCubic),\\n\\n),\\n\\n);\\n\\n_controller.forward();\\n\\n}\\n\\n@override\\n\\nvoid dispose(){\\n\\n _controller.dispose();\\n\\nsuper.dispose();\\n\\n}\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn AnimatedBuilder(\\n\\n animation: _controller,\\n\\n builder:(context, child){\\n\\nreturn FadeTransition(\\n\\n opacity: _fadeAnimation,\\n\\n child: ScaleTransition(\\n\\n scale: _scaleAnimation,\\n\\n child: SlideTransition(\\n\\n position: _slideAnimation,\\n\\n child:const Card(\\n\\n child: Padding(\\n\\n padding: EdgeInsets.all(24),\\n\\n child: Text('Welcome'),\\n\\n),\\n\\n),\\n\\n),\\n\\n),\\n\\n);\\n\\n},\\n\\n);\\n\\n}\\n\\n}\\n\\n* * *\\n\\n## Hero Animation\\n\\nHero AnimationUsed to create shared element transition effects when switching pages.\\n\\n## Example: Hero Animation\\n\\n// Home - Image with Hero\\n\\nclass HomePage extends StatelessWidget {\\n\\nconst HomePage({super.key});\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn Scaffold(\\n\\n appBar: AppBar(title:const Text('Home')),\\n\\n body: Center(\\n\\n child: GestureDetector(\\n\\n onTap:()=> Navigator.push(\\n\\n context,\\n\\n MaterialPageRoute(builder:(_)=>const DetailPage()),\\n\\n),\\n\\n child: Hero(\\n\\n tag:'myHero',// Same tag\\n\\n child: Image.network(\\n\\n'https://picsum.photos/200',\\n\\n width:200,\\n\\n height:200,\\n\\n),\\n\\n),\\n\\n),\\n\\n),\\n\\n);\\n\\n}\\n\\n}\\n\\n// Detail Page - Image with Hero\\n\\nclass DetailPage extends StatelessWidget {\\n\\nconst DetailPage({super.key});\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn Scaffold(\\n\\n appBar: AppBar(title:const Text('Details')),\\n\\n body: Center(\\n\\n child: Hero(\\n\\n tag:'myHero',// Same tag\\n\\n child: Image.network(\\n\\n'https://picsum.photos/200',\\n\\n width:300,\\n\\n height:300,\\n\\n),\\n\\n),\\n\\n),\\n\\n);\\n\\n}\\n\\n}\\n\\n> Implicit AnimationSuitable for simple scenarios, while Explicit Animation is suitable for complex control.\\n> \\n> \\n> Hero AnimationThe best choice for page transitions.\\n\\nThis section introduces animation implementation in Flutter, including implicit animations and explicit animations.\\n\\n* * *\\n\\n## Implicit Animations\\n\\nImplicit animations are animation widgets where Flutter automatically handles the transition effects.\\n\\n## Example: AnimatedContainer\\n\\nclass AnimatedContainerExample extends StatefulWidget {\\n\\nconst AnimatedContainerExample({super.key});\\n\\n@override\\n\\n State createState()=>\\n\\n _AnimatedContainerExampleState();\\n\\n}\\n\\nclass _AnimatedContainerExampleState\\n\\nextends State{\\n\\nbool _isLarge =false;\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn Column(\\n\\n children:[\\n\\n// Tap to toggle size\\n\\n GestureDetector(\\n\\n onTap:()=> setState(()=> _isLarge =!_isLarge),\\n\\n child: AnimatedContainer(\\n\\n duration:const Duration(milliseconds:500),// Animation duration\\n\\n curve: Curves.easeInOut,// Animation curve\\n\\n width: _isLarge ?200:100,\\n\\n height: _isLarge ?200:100,\\n\\n color: _isLarge ? Colors.blue: Colors.red,\\n\\n child: Center(\\n\\n child: Text(\\n\\n _isLarge ?'Da':'Small',\\n\\n style:const TextStyle(color: Colors.white, fontSize:24),\\n\\n),\\n\\n),\\n\\n),\\n\\n),\\n\\nconst SizedBox(height:20),\\n\\n ElevatedButton(\\n\\n onPressed:()=> setState(()=> _isLarge =!_isLarge),\\n\\n child:const Text('Switch'),\\n\\n),\\n\\n],\\n\\n);\\n\\n}\\n\\n}\\n\\n### Commonly Used Implicit Animation Widgets\\n\\n| Widget | Description |\\n| --- | --- |\\n| AnimatedContainer | Animation when container size, color, and other properties change |\\n| AnimatedOpacity | Opacity change animation |\\n| AnimatedPadding | Padding change animation |\\n| AnimatedAlign | Alignment change animation |\\n| AnimatedSwitcher | Animation when switching child widgets |\\n| AnimatedDefaultTextStyle | Text style change animation |\\n\\n* * *\\n\\n## Explicit Animations\\n\\nExplicit animations use AnimationController to fully control the animation.\\n\\n## Example: Rotation Animation\\n\\nclass RotationAnimation extends StatefulWidget {\\n\\nconst RotationAnimation({super.key});\\n\\n@override\\n\\n State createState()=> _RotationAnimationState();\\n\\n}\\n\\nclass _RotationAnimationState extends State\\n\\nwith SingleTickerProviderStateMixin {\\n\\n late AnimationController _controller;\\n\\n late Animation _animation;\\n\\n@override\\n\\nvoid initState(){\\n\\nsuper.initState();\\n\\n// Create animation controller\\n\\n _controller = AnimationController(\\n\\n duration:const Duration(seconds:2),\\n\\n vsync:this,\\n\\n);\\n\\n// Create rotation animation (0 to 1 maps to 0 to 2Ο)\\n\\n _animation = Tween(begin:0, end:6.28).animate(\\n\\n CurvedAnimation(parent: _controller, curve: Curves.linear),\\n\\n);\\n\\n// Start animation\\n\\n _controller.repeat();\\n\\n}\\n\\n@override\\n\\nvoid dispose(){\\n\\n _controller.dispose();\\n\\nsuper.dispose();\\n\\n}\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn Center(\\n\\n child: AnimatedBuilder(\\n\\n animation: _animation,\\n\\n builder:(context, child){\\n\\nreturn Transform.rotate(\\n\\n angle: _animation.value,// Rotation angle\\n\\n child:const Icon(Icons.refresh, size:100),\\n\\n);\\n\\n},\\n\\n),\\n\\n);\\n\\n}\\n\\n}\\n\\n* * *\\n\\n## Staggered Animations\\n\\n## Example: Staggered Animation\\n\\nclass StaggeredAnimation extends StatefulWidget {\\n\\nconst StaggeredAnimation({super.key});\\n\\n@override\\n\\n State createState()=> _StaggeredAnimationState();\\n\\n}\\n\\nclass _StaggeredAnimationState extends State\\n\\nwith TickerProviderStateMixin {\\n\\n late AnimationController _controller;\\n\\n// Individual animations\\n\\n late Animation _fadeAnimation;\\n\\n late Animation _scaleAnimation;\\n\\n late Animation _slideAnimation;\\n\\n@override\\n\\nvoid initState(){\\n\\nsuper.initState();\\n\\n _controller = AnimationController(\\n\\n duration:const Duration(milliseconds:1500),\\n\\n vsync:this,\\n\\n);\\n\\n// Fade in animation (0-0.5)\\n\\n _fadeAnimation = Tween(begin:0, end:1).animate(\\n\\n CurvedAnimation(\\n\\n parent: _controller,\\n\\n curve:const Interval(0,0.5, curve: Curves.easeOut),\\n\\n),\\n\\n);\\n\\n// Scale animation (0.3-0.7)\\n\\n _scaleAnimation = Tween(begin:0.5, end:1).animate(\\n\\n CurvedAnimation(\\n\\n parent: _controller,\\n\\n curve:const Interval(0.3,0.7, curve: Curves.elasticOut),\\n\\n),\\n\\n);\\n\\n// Slide in animation (0.5-1.0)\\n\\n _slideAnimation = Tween(\\n\\n begin:const Offset(0,0.5),\\n\\n end: Offset.zero,\\n\\n).animate(\\n\\n CurvedAnimation(\\n\\n parent: _controller,\\n\\n curve:const Interval(0.5,1, curve: Curves.easeOutCubic),\\n\\n),\\n\\n);\\n\\n_controller.forward();\\n\\n}\\n\\n@override\\n\\nvoid dispose(){\\n\\n _controller.dispose();\\n\\nsuper.dispose();\\n\\n}\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn AnimatedBuilder(\\n\\n animation: _controller,\\n\\n builder:(context, child){\\n\\nreturn FadeTransition(\\n\\n opacity: _fadeAnimation,\\n\\n child: ScaleTransition(\\n\\n scale: _scaleAnimation,\\n\\n child: SlideTransition(\\n\\n position: _slideAnimation,\\n\\n child:const Card(\\n\\n child: Padding(\\n\\n padding: EdgeInsets.all(24),\\n\\n child: Text('Welcome'),\\n\\n),\\n\\n),\\n\\n),\\n\\n),\\n\\n);\\n\\n},\\n\\n);\\n\\n}\\n\\n}\\n\\n* * *\\n\\n## Hero Animations\\n\\nHero animations are used to create shared element transition effects during page transitions.\\n\\n## Example: Hero Animation\\n\\n// Home page - image with Hero\\n\\nclass HomePage extends StatelessWidget {\\n\\nconst HomePage({super.key});\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn Scaffold(\\n\\n appBar: AppBar(title:const Text('Home')),\\n\\n body: Center(\\n\\n child: GestureDetector(\\n\\n onTap:()=> Navigator.push(\\n\\n context,\\n\\n MaterialPageRoute(builder:(_)=>const DetailPage()),\\n\\n),\\n\\n child: Hero(\\n\\n tag:'myHero',// Same tag\\n\\n child: Image.network(\\n\\n'https://picsum.photos/200',\\n\\n width:200,\\n\\n height:200,\\n\\n),\\n\\n),\\n\\n),\\n\\n),\\n\\n);\\n\\n}\\n\\n}\\n\\n// Detail page - image with Hero\\n\\nclass DetailPage extends StatelessWidget {\\n\\nconst DetailPage({super.key});\\n\\n@override\\n\\n Widget build(BuildContext context){\\n\\nreturn Scaffold(\\n\\n appBar: AppBar(title:const Text('Details')),\\n\\n body: Center(\\n\\n child: Hero(\\n\\n tag:'myHero',// Same tag\\n\\n child: Image.network(\\n\\n'https://picsum.photos/200',\\n\\n width:300,\\n\\n height:300,\\n\\n),\\n\\n),\\n\\n),\\n\\n);\\n\\n}\\n\\n}\\n\\n> Implicit animations are suitable for simple scenarios, while explicit animations are suitable for complex control.\\n> \\n> \\n> Hero animations are the best choice for page transitions.
YouTip