{"id":1068,"date":"2023-09-20T06:59:15","date_gmt":"2023-09-20T06:59:15","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1068"},"modified":"2023-09-29T07:14:47","modified_gmt":"2023-09-29T07:14:47","slug":"make-a-simple-animation-in-flutter","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/make-a-simple-animation-in-flutter\/","title":{"rendered":"Make a Simple Animation in Flutter"},"content":{"rendered":"\n<p>Flutter, developed by Google, is a powerful framework for building beautiful and engaging user interfaces (UIs) across various platforms. One of its standout features is its robust animation capabilities, allowing developers to create visually stunning and interactive experiences.<\/p>\n\n\n\n<p id=\"e437\"><strong>AnimationController:<\/strong><\/p>\n\n\n\n<p id=\"9618\">The AnimationController class is a fundamental building block for animations in Flutter. It controls the animation\u2019s duration, playback and manages the animation\u2019s current value. You can define its duration, specify the desired animation curve (e.g., linear, ease-in, ease-out), and listen to animation status changes using listeners.<\/p>\n\n\n\n<p id=\"1e4c\"><strong>Tween:<\/strong><\/p>\n\n\n\n<p id=\"46cc\">The Tween class defines the range of values an animation should interpolate between. For instance, if you want to animate an object\u2019s opacity from 0.0 to 1.0, you would use a&nbsp;<code>Tween&lt;double&gt;(begin: 0.0, end: 1.0)<\/code>. The animation controller utilizes the tween to generate interpolated values during the animation&#8217;s progress.<\/p>\n\n\n\n<p id=\"2ca2\"><strong>CurvedAnimation:<\/strong><\/p>\n\n\n\n<p id=\"b1b2\">CurvedAnimation allows you to apply custom animation curves to modify the rate of change over time. It takes an animation controller and a curve parameter, such as&nbsp;<code>Curves.easeInOut<\/code>,&nbsp;<code>Curves.fastOutSlowIn<\/code>, or custom curves created using&nbsp;<code>Cubic<\/code>&nbsp;class.<\/p>\n\n\n\n<p id=\"2210\"><strong>AnimationBuilder:<\/strong><\/p>\n\n\n\n<p id=\"a001\">The AnimationBuilder widget provides a way to build complex animations based on the current value of an animation. It allows you to define how UI components should change over time based on the interpolated animation values.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"433f\">Predefined Animations<\/h1>\n\n\n\n<p id=\"f5b9\"><strong>FadeTransition:<\/strong><\/p>\n\n\n\n<p id=\"0221\">FadeTransition widget fades its child\u2019s opacity between 0.0 and 1.0 based on the animation\u2019s progress. It\u2019s perfect for creating smooth fade-in or fade-out effects. Here\u2019s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FadeTransition(\r\n  opacity: animation,\r\n  child: YourWidget(),\r\n)\n<\/code><\/pre>\n\n\n\n<p id=\"1e6b\"><strong>ScaleTransition:<\/strong><\/p>\n\n\n\n<p id=\"4e23\">ScaleTransition widget scales its child based on the animation\u2019s value. You can define the starting and ending scale factors. This animation is useful for creating zooming or scaling effects.<\/p>\n\n\n\n<p>ScaleTransition(<br>scale: animation,<br>child: YourWidget(),<br>)<\/p>\n\n\n\n<p id=\"6cc4\"><strong>SlideTransition:<\/strong><\/p>\n\n\n\n<p id=\"7db8\">SlideTransition animates its child\u2019s position relative to its normal position. You can specify the starting and ending offset values to control the direction and distance of the slide animation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SlideTransition(\r\n  position: Tween&lt;Offset>(\r\n    begin: Offset(-1.0, 0.0),\r\n    end: Offset(0.0, 0.0),\r\n  ).animate(animation),\r\n  child: YourWidget(),\r\n)<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"a1a6\">Custom Animations<\/h1>\n\n\n\n<p id=\"3896\"><strong>Hero Animation:<\/strong><\/p>\n\n\n\n<p id=\"a0a3\">Hero animations create seamless transitions between two UI elements that share a common hero tag. It\u2019s commonly used to animate transitions between screens or when moving an element from one position to another. Here\u2019s a simplified example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ScreenA extends StatelessWidget {\r\n  @override\r\n  Widget build(BuildContext context) {\r\n    return GestureDetector(\r\n      onTap: () {\r\n        Navigator.push(\r\n          context,\r\n          MaterialPageRoute(builder:: (context) => ScreenB()),\r\n        );\r\n      },\r\n      child: Hero(\r\n        tag: \"imageTag\",\r\n        child: Image.asset(\"image.png\"),\r\n      ),\r\n    );\r\n  }\r\n}\r\n\r\nclass ScreenB extends StatelessWidget {\r\n  @override\r\n  Widget build(BuildContext context) {\r\n    return Scaffold(\r\n      body: GestureDetector(\r\n        onTap: () {\r\n          Navigator.pop(context);\r\n        },\r\n        child: Center(\r\n          child: Hero(\r\n            tag: \"imageTag\",\r\n            child: Image.asset(\"image.png\"),\r\n          ),\r\n        ),\r\n      ),\r\n    );\r\n  }\r\n}<\/code><\/pre>\n\n\n\n<p id=\"c9ed\"><strong>Staggered Animations:<\/strong><\/p>\n\n\n\n<p id=\"ae15\">Staggered animations allow you to animate multiple UI elements with a delay between each animation. It creates a visually appealing sequence of transitions. Here\u2019s an example using the\u00a0<code>AnimatedOpacity<\/code>\u00a0widget:<\/p>\n\n\n\n<p>ListView.builder(<br>itemCount: items.length,<br>itemBuilder: (context, index) {<br>return AnimatedOpacity(<br>opacity: isVisible ? 1.0 : 0.0,<br>duration: Duration(milliseconds: 500),<br>curve: Curves.easeInOut,<br>child: YourItemWidget(),<br>);<br>},<br>)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p id=\"c7f3\"><strong>Transform Animation:<\/strong><\/p>\n\n\n\n<p id=\"d958\">The Transform widget provides various transformations like scaling, rotation, translation, and skewing. By animating the transformation properties, you can achieve dynamic and visually appealing effects. Here\u2019s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Transform.rotate(\r\n  angle: animation.value * 2 * math.pi,\r\n  child: YourWidget(),\r\n)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Flutter, developed by Google, is a powerful framework for building beautiful and engaging user interfaces (UIs) across various platforms. One of its standout features is its robust&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[385,384],"class_list":["post-1068","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-animation-basics","tag-make-a-simple-animation-in-flutter"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1068"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1068\/revisions"}],"predecessor-version":[{"id":1069,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1068\/revisions\/1069"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}