Opacity widget and Attributes

Opacity widget and Attributes

ยท

2 min read

The Opacity widget in Flutter is used to make its child partially or fully transparent. It's commonly used to control the transparency of UI elements, such as images, text, or containers.

Attributes:

  1. opacity (double):

    • The opacity value of the child. It ranges from 0.0 (fully transparent) to 1.0 (fully opaque). The default value is 1.0.
  2. alwaysIncludeSemantics (bool):

    • Whether the child's semantics are always included, regardless of the opacity value. When set to true, even if the opacity is 0.0, the child's semantics will be included. The default value is false.

Example:

import 'package:flutter/material.dart';

class OpacityExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Opacity Widget Example'),
      ),
      body: Center(
        child: Opacity(
          opacity: 0.5,
          child: Container(
            width: 200,
            height: 200,
            color: Colors.blue,
            child: Center(
              child: Text(
                'Transparent Container',
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: OpacityExample(),
  ));
}

Explanation:

  • In this example, an Opacity widget is used to make a container partially transparent.

  • The opacity attribute is set to 0.5, making the container 50% transparent.

  • Inside the Opacity widget, there's a Container with a blue background color.

  • The text "Transparent Container" is centered within the container, and its color is set to white.

  • When you run this code, you'll see a blue container with white text that is partially transparent, allowing you to see through it to some extent. Adjusting the opacity value will change the level of transparency accordingly.

Did you find this article valuable?

Support Vinit Mepani by becoming a sponsor. Any amount is appreciated!

ย