<p>In this post, we are going to see how to create an AnimatedIcon in Flutter as below.</p> <p>,</p> <p>However, the trickiest part of this example is to add the controller.</p> <p>AnimationController is a special Animation object that generates a new value whenever the hardware is ready for a new frame. By default, an AnimationController linearly produces the numbers from 0.0 to 1.0 during a given duration, (but it can be out of range in few cases). The generation of numbers is tied to the screen refresh, so typically 60 numbers are generated per second.</p> <p>When creating an AnimationController, you pass it a vsync argument. The presence of vsync prevents offscreen animations from consuming unnecessary resources. You can use your stateful object as the vsync by adding SingleTickerProviderStateMixin to the class definition.</p> <p>Here is the code from main.dart file.</p> <p>runApp(LogoApp());</p> <p>class LogoApp extends StatefulWidget { _LogoAppState createState() => _LogoAppState(); }</p> <p>class _LogoAppState extends State with SingleTickerProviderStateMixin { bool isPlaying = false;</p> <p>Animation animation;</p> <p>AnimationController controller;</p> <p>@override void initState() { super.initState();</p> <pre>controller = AnimationController( duration: const Duration(milliseconds: 500), vsync: this); </pre> <p>}</p> <p>@override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: IconButton( iconSize: 70, icon: AnimatedIcon( icon: AnimatedIcons.play_pause, progress: controller, ), onPressed: () => _onpressed(), )), ), ); }</p> <p>@override void dispose() { controller.dispose();</p> <pre>super.dispose(); </pre> <p>}</p> <p>_onpressed() { setState(() { isPlaying = !isPlaying;</p> <pre> isPlaying ? controller.forward() : controller.reverse(); }); </pre> <p>} }</p> <p>` Hope this is helpful to you. Thanks, Srikanth</p>
AnimatedIcon Example in Flutter
This Article is posted by seven.srikanth at 11/25/2019 6:06:56 PM
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter
Tags: AnimatedIcon in Flutter; Animate a Icon;
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter