Implicit animations help you change UI smoothly without writing complex code. They make your app look lively and easy to use.
Implicit animations (AnimatedContainer, AnimatedOpacity) in Flutter
AnimatedContainer( duration: Duration(milliseconds: 300), color: Colors.blue, width: 100, height: 100, child: Text('Hello'), ) AnimatedOpacity( duration: Duration(milliseconds: 500), opacity: 0.5, child: Icon(Icons.star), )
AnimatedContainer animates changes in size, color, padding, margin, and more.
AnimatedOpacity animates the transparency of a widget.
AnimatedContainer( duration: Duration(seconds: 1), width: 200, height: 200, color: Colors.red, child: SizedBox(), )
AnimatedOpacity( duration: Duration(milliseconds: 800), opacity: 0.0, child: Text('Fading out'), )
This app shows a colored box that changes size and color when you tap the button. The text below fades in and out with the box. This uses AnimatedContainer and AnimatedOpacity to animate changes smoothly without manual animation code.
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { bool _selected = false; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Implicit Animations Example')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ AnimatedContainer( width: _selected ? 200 : 100, height: _selected ? 200 : 100, color: _selected ? Colors.blue : Colors.red, alignment: Alignment.center, duration: const Duration(seconds: 1), curve: Curves.easeInOut, child: const Text('Tap me', style: TextStyle(color: Colors.white, fontSize: 18)), ), const SizedBox(height: 20), AnimatedOpacity( opacity: _selected ? 1.0 : 0.0, duration: const Duration(seconds: 1), child: const Text('Hello! I appear and disappear'), ), const SizedBox(height: 40), ElevatedButton( onPressed: () { setState(() { _selected = !_selected; }); }, child: const Text('Toggle Animation'), ), ], ), ), ), ); } }
Implicit animations automatically animate when their properties change.
Use duration to control how long the animation lasts.
Use curve to control the animation speed style (like ease-in, ease-out).
Implicit animations make UI changes smooth and easy.
AnimatedContainer animates size, color, and more.
AnimatedOpacity animates transparency.
Practice
AnimatedOpacity do in Flutter?Solution
Step 1: Understand the purpose of AnimatedOpacity
AnimatedOpacity is designed to animate changes in a widget's opacity, making it fade in or out smoothly.Step 2: Compare with other options
Options A, B, and C describe animations or changes that AnimatedOpacity does not perform.Final Answer:
It smoothly changes the transparency of a widget over time. -> Option AQuick Check:
AnimatedOpacity = smooth transparency change [OK]
- Confusing opacity with size or color animation
- Thinking it moves widgets instead of fading them
- Assuming it changes instantly without animation
AnimatedContainer with a duration of 500 milliseconds?Solution
Step 1: Check the Duration syntax in Flutter
Duration expects named parameters like milliseconds or seconds, not raw numbers.Step 2: Identify the correct Duration usage
Duration(milliseconds: 500) correctly sets 500 milliseconds. AnimatedContainer(duration: Duration(seconds: 0.5), color: Colors.red) uses seconds with a decimal, which is invalid. AnimatedContainer(duration: 500, color: Colors.red) passes an int directly, which is wrong. AnimatedContainer(duration: Duration(microseconds: 500), color: Colors.red) uses microseconds, which is too small.Final Answer:
AnimatedContainer(duration: Duration(milliseconds: 500), color: Colors.red) -> Option BQuick Check:
Duration(milliseconds: 500) is correct [OK]
- Passing raw int instead of Duration object
- Using seconds with decimal (not allowed)
- Confusing microseconds with milliseconds
class MyWidget extends StatefulWidget {
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
bool _visible = true;
@override
Widget build(BuildContext context) {
return Column(
children: [
AnimatedOpacity(
opacity: _visible ? 1.0 : 0.0,
duration: Duration(seconds: 1),
child: Container(width: 100, height: 100, color: Colors.blue),
),
ElevatedButton(
onPressed: () {
setState(() {
_visible = !_visible;
});
},
child: Text('Toggle'),
),
],
);
}
}Solution
Step 1: Understand AnimatedOpacity behavior
AnimatedOpacity animates changes in opacity smoothly over the given duration.Step 2: Analyze the button press effect
Pressing the button toggles _visible between true and false, changing opacity from 1.0 to 0.0 or vice versa, triggering the fade animation.Final Answer:
The blue box will fade in and out smoothly when the button is pressed. -> Option AQuick Check:
AnimatedOpacity toggles opacity smoothly [OK]
- Thinking opacity changes instantly without animation
- Confusing opacity with size or color changes
- Assuming missing opacity causes error
AnimatedContainer and how to fix it:AnimatedContainer( width: 100, height: 100, color: Colors.green, duration: 500, )
Solution
Step 1: Check the duration property type
AnimatedContainer's duration expects a Duration object, not a raw integer.Step 2: Correct the duration syntax
Replace duration: 500 with duration: Duration(milliseconds: 500) to fix the error.Final Answer:
The duration should be a Duration object, not an int; fix with Duration(milliseconds: 500). -> Option DQuick Check:
Duration must be Duration object [OK]
- Passing int directly to duration
- Misplacing color property (valid here though)
- Thinking child is mandatory for AnimatedContainer
Solution
Step 1: Understand capabilities of AnimatedContainer and AnimatedOpacity
AnimatedContainer animates size, color, and other box properties but not opacity. AnimatedOpacity animates transparency only.Step 2: Combine widgets for simultaneous animations
Wrapping AnimatedContainer inside AnimatedOpacity allows simultaneous size and opacity animations controlled by the same state.Step 3: Evaluate other options
Use onlyAnimatedContainerwith opacity property set to animate transparency. is incorrect because AnimatedContainer does not animate opacity. UseAnimatedOpacitywith width and height properties to animate size and opacity together. is invalid because AnimatedOpacity does not animate size. Use aContainerwith manualsetStatecalls to animate size and opacity. lacks implicit animation and requires manual animation code.Final Answer:
Wrap an AnimatedContainer inside an AnimatedOpacity, both controlled by the same state variable. -> Option CQuick Check:
Combine AnimatedContainer + AnimatedOpacity for size + fade [OK]
- Expecting AnimatedContainer to animate opacity
- Trying to animate size with AnimatedOpacity
- Using Container without animation widgets
Ready to prove it?
Pass the test and this pattern moves unknown
