The FadeTransition widget in Flutter is used to apply a fading effect to a child widget. It animates the opacity of the child widget over a specified duration, creating a smooth transition between visible and invisible states.
Attributes:
opacity (Animation<double>):
- The animation controlling the opacity of the child widget. It specifies the opacity value of the child, where 0.0 means completely transparent (invisible) and 1.0 means fully opaque (visible).
alwaysIncludeSemantics (bool):
- Whether the semantics of the child widget should always be included, regardless of the opacity value. If set to true, the child widget will always be considered for accessibility purposes, even when it's invisible due to opacity.
child (Widget):
- The child widget to which the fading effect will be applied.
Example:
import 'package:flutter/material.dart';
class FadeTransitionExample extends StatefulWidget {
@override
_FadeTransitionExampleState createState() => _FadeTransitionExampleState();
}
class _FadeTransitionExampleState extends State<FadeTransitionExample> with SingleTickerProviderStateMixin {
AnimationController _controller;
Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 2),
);
_animation = Tween<double>(
begin: 0.0,
end: 1.0,
).animate(_controller);
_controller.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('FadeTransition Widget Example'),
),
body: Center(
child: FadeTransition(
opacity: _animation,
child: Container(
width: 200,
height: 200,
color: Colors.blue,
),
),
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
void main() {
runApp(MaterialApp(
home: FadeTransitionExample(),
));
}
Explanation:
In this example, a FadeTransition widget is used to apply a fading effect to a Container widget.
The opacity of the FadeTransition is controlled by an animation (_animation), which gradually changes from 0.0 to 1.0 over a duration of 2 seconds.
The child of the FadeTransition is the Container widget with a blue background color.
When you run this code, you'll see the Container widget gradually fade in from invisible (opacity = 0.0) to fully visible (opacity = 1.0) over a period of 2 seconds.