Sub topic 5
AnimatedOpacity, AnimatedAlign, and Other Implicit Widgets
Assume you know Dart classes, Flutter’s build method, StatefulWidget, setState, and basic layout widgets. This chapter focuses on choosing and configuring Flutter’s built-in implicit animation widgets.
AnimatedOpacity
AnimatedOpacity animates a widget’s transparency when its opacity changes. The value ranges from 0.0 for invisible to 1.0 for fully visible.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: AnimationExample()));
}
class AnimationExample extends StatefulWidget {
const AnimationExample({super.key});
@override
State<AnimationExample> createState() => _AnimationExampleState();
}
class _AnimationExampleState extends State<AnimationExample> {
bool _shown = true;
bool _rightAligned = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Implicit animations')),
body: Column(
children: [
Expanded(
child: AnimatedAlign(
alignment: _rightAligned
? Alignment.centerRight
: Alignment.centerLeft,
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOut,
child: AnimatedOpacity(
opacity: _shown ? 1.0 : 0.0,
duration: const Duration(milliseconds: 500),
child: const FlutterLogo(size: 96),
),
),
),
Padding(
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () => setState(() => _shown = !_shown),
child: const Text('Fade'),
),
ElevatedButton(
onPressed: () =>
setState(() => _rightAligned = !_rightAligned),
child: const Text('Move'),
),
],
),
),
],
),
);
}
}When _shown changes, Flutter compares the old and new opacity values. AnimatedOpacity creates the intermediate values and repaints the child during the configured duration.
An opacity of zero still keeps the widget in the layout and may leave it able to receive interactions. If an invisible widget must not respond to taps, combine the animation with IgnorePointer, or remove it after the fade completes.
AnimatedAlign and related widgets
AnimatedAlign animates a child between alignment positions without requiring an AnimationController. Its parent must provide enough space for the child to move.
Other implicit widgets follow the same pattern:
| Widget | Commonly animated property |
|---|---|
| `AnimatedPadding` | Padding around a child |
| `AnimatedPositioned` | Position inside a `Stack` |
| `AnimatedDefaultTextStyle` | Text style |
| `AnimatedSwitcher` | Replacing one child with another |
Most accept duration and curve. Some, such as AnimatedSwitcher, also use keys or transition builders to identify child changes.
How to choose
Use an implicit widget when one property change starts one straightforward transition. Prefer an explicit animation when you need playback controls, repeated motion, several effects sharing one timeline, or callbacks for animation status.
Exercise
Why might an invisible AnimatedOpacity child still need IgnorePointer?
Show answer
Opacity changes how the child is painted, but an opacity of zero does not automatically remove the child from layout or stop it from receiving pointer events.