AnimatedOpacity widget and Attributes

AnimatedOpacity widget and Attributes

ยท

2 min read

The AnimatedOpacity widget in Flutter is a powerful tool for creating animations that control the opacity of its child widget over a specified duration. This widget allows developers to smoothly transition between different opacity levels, creating visually appealing effects such as fading in or out.

Attributes of the AnimatedOpacity widget typically include:

  1. opacity: A double value between 0.0 and 1.0 that represents the opacity of the child widget. A value of 0.0 indicates complete transparency (fully invisible), while a value of 1.0 indicates full opacity (fully visible).

  2. duration: The duration of the animation, specified as a Duration object.

  3. curve: An optional parameter that specifies the curve for the animation. This parameter determines the rate of change of the animation over time, allowing for customizable easing effects.

  4. child: The child widget whose opacity is being animated.

Now, let's delve into an example of how to use the AnimatedOpacity widget in a Flutter application:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AnimatedOpacity Widget Example',
      home: AnimatedOpacityDemo(),
    );
  }
}

class AnimatedOpacityDemo extends StatefulWidget {
  @override
  _AnimatedOpacityDemoState createState() => _AnimatedOpacityDemoState();
}

class _AnimatedOpacityDemoState extends State<AnimatedOpacityDemo> {
  bool _isVisible = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimatedOpacity Widget Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AnimatedOpacity(
              opacity: _isVisible ? 1.0 : 0.0,
              duration: Duration(seconds: 1),
              curve: Curves.easeInOut,
              child: Container(
                width: 200,
                height: 200,
                color: Colors.blue,
                child: Center(
                  child: Text(
                    'AnimatedOpacity Example',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20.0,
                    ),
                  ),
                ),
              ),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  _isVisible = !_isVisible;
                });
              },
              child: Text(_isVisible ? 'Hide' : 'Show'),
            ),
          ],
        ),
      ),
    );
  }
}

In this example, we have created a Flutter application with an AnimatedOpacityDemo widget. Inside this widget, we have an AnimatedOpacity widget as its child. The opacity of the child Container widget is controlled by the _isVisible variable, which toggles between true and false states based on user interaction with the ElevatedButton. When _isVisible is true, the child widget is fully visible with an opacity of 1.0, and when _isVisible is false, the child widget is fully transparent with an opacity of 0.0. The opacity change is animated over a duration of 1 second, with an ease-in-out curve applied to the animation for smoother transitions.

This example demonstrates how the AnimatedOpacity widget can be used to create subtle and visually appealing animations that control the opacity of child widgets in a Flutter application.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย