How To Create a Smooth Fade Effect with AnimatedOpacity Widget in Flutter

Ajiyemi Michael
3 min readMar 27, 2023

--

Flutter is a powerful framework for creating beautiful and dynamic user interfaces. Similar to the AnimatedContainer widget, AnimatedOpacity has special properties like opacity, curve, duration, and child. By setting the opacity property, you can determine the opacity of the child widget, while the curve property controls the animation curve. Additionally, the duration property allows you to set the duration of the animation, and a child of cause which takes in a type widget. now how do we animate the child widget inside the AnimatedOpacity widget basically this widget is able to animate its child’s opacity so how do we accomplish this?

To achieve the animation of opacity in the child widget using AnimatedOpacity, we need to create a stateful widget that can toggle between two states: fully opaque or not. By creating a stateful widget, we can dynamically update the UI and change the opacity of the child widget whenever needed.

In addition to creating a stateful widget, we also need a Boolean value to check whether the element is opaque or not. This Boolean value acts as a switch that toggles the opacity of the child widget between two states, making the animation possible.

To update the opacity of the child widget using the Boolean value, we can access the opacity property of the AnimatedOpacity widget and use a ternary operator to check the value of the Boolean. If the Boolean is true, we set the opacity to 1.0, otherwise, we set it to 0.0. Here’s an example:

AnimatedOpacity(
opacity: visible ? 1.0 : 0.0,
duration: Duration(seconds: 1),
curve: Curves.easeInOut,
child: // your child widget here
),

To trigger the opacity animation, we can create a button inside the child widget that has access to the onPressed method. Inside the onPressed method, we can use setState to flip the Boolean value to its opposite. This way, each time the button is clicked, the opacity of the child widget toggles between fully opaque and not opaque at all. Here’s an example:

bool visible = true;

FloatingActionButton(
onPressed: () {
setState(() {
_visibile = !_visibile;
});
},
tooltip: 'Toggle Opacity',
child: const Icon(Icons.flip),
),

Now that we have covered all the necessary steps, let’s bring them all together to create an AnimatedOpacity widget that toggles the opacity of its child widget.

First, we create a stateful widget that has a Boolean value to control the opacity of the child widget. Then, we use the AnimatedOpacity widget to animate the opacity of the child widget using the Boolean value as a switch. Finally, we create a button inside the child widget that toggles the Boolean value when pressed, triggering the opacity animation.

Here’s an example code snippet that brings everything together:

class FadeInFadeOut extends StatefulWidget {
final String title;
const FadeInFadeOut({Key? key, required this.title}) : super(key: key);

@override
State<FadeInFadeOut> createState() => _FadeInFadeOutState();
}

class _FadeInFadeOutState extends State<FadeInFadeOut> {
bool _visibile = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: AnimatedOpacity(
opacity: _visibile ? 1.0 : 0.0,
duration: const Duration(milliseconds: 500),
curve: Curves.easeIn,
child: Container(
width: 200.0,
height: 200.0,
color: Colors.green,
child: FloatingActionButton(
onPressed: () {
setState(() {
_visibile = !_visibile;
});
},
tooltip: 'Toggle Opacity',
child: const Icon(Icons.flip),
),
),
),
),
);
}
}

By using this code as a starting point, you can easily create stunning UI animations in your Flutter app that respond to user input and enhance the overall user experience. I’m excited to see the amazing animations you will be able to create using the AnimatedOpacity widget! With its ability to animate the opacity of child elements, you can add a whole new level of visual interest and interaction to your Flutter apps. By leveraging its special properties such as curve and duration, you can create smooth and fluid animations that enhance the user experience and make your app stand out. So go ahead and explore the possibilities with AnimatedOpacity — the sky’s the limit!

--

--

Ajiyemi Michael
Ajiyemi Michael

Written by Ajiyemi Michael

i AM a flutter developer, i write because it is my way of giving back to the community, to share my knowledge and experiences with others

No responses yet