AnimatedBuilder helps you create smooth animations by rebuilding only parts of your UI that change. It makes animations efficient and easy to manage.
AnimatedBuilder in Flutter
AnimatedBuilder(
animation: animationController,
builder: (context, child) {
return Transform.rotate(
angle: animationController.value * 2 * 3.141592653589793,
child: child,
);
},
child: YourWidget(),
)animation: The animation object that controls the animation progress.
builder: A function that rebuilds the widget tree when the animation changes. It receives the current context and a child widget.
child: The widget that does not change during animation, passed to builder for efficiency.
AnimatedBuilder(
animation: controller,
builder: (context, child) {
return Opacity(
opacity: controller.value,
child: child,
);
},
child: Text('Fade In'),
)AnimatedBuilder(
animation: controller,
builder: (context, child) {
return Transform.scale(
scale: controller.value,
child: child,
);
},
child: Icon(Icons.star, size: 50),
)This app shows a blue square with the word "Rotate" that continuously spins in a circle using AnimatedBuilder.
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> with SingleTickerProviderStateMixin { late final AnimationController _controller; @override void initState() { super.initState(); _controller = AnimationController( duration: const Duration(seconds: 3), vsync: this, )..repeat(); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('AnimatedBuilder Example')), body: Center( child: AnimatedBuilder( animation: _controller, builder: (context, child) { return Transform.rotate( angle: _controller.value * 2 * 3.141592653589793, child: child, ); }, child: Container( width: 100, height: 100, color: Colors.blue, child: const Center( child: Text('Rotate', style: TextStyle(color: Colors.white)), ), ), ), ), ), ); } }
Use the child parameter to pass widgets that do not change during animation to improve performance.
AnimatedBuilder rebuilds only the widgets inside the builder function, not the entire widget tree.
Always dispose your AnimationController to free resources.
AnimatedBuilder helps animate parts of your UI efficiently.
Use animation, builder, and child to control what changes and what stays the same.
It improves performance by rebuilding only necessary widgets during animation.
Practice
AnimatedBuilder in Flutter?Solution
Step 1: Understand AnimatedBuilder's role
AnimatedBuilder is designed to rebuild only the widgets that depend on the animation value, improving performance.Step 2: Compare with other options
Options B, C, and D describe unrelated Flutter features like static UI, input handling, and navigation.Final Answer:
To efficiently rebuild only parts of the UI that change during animation -> Option CQuick Check:
AnimatedBuilder optimizes UI updates = A [OK]
- Thinking AnimatedBuilder rebuilds the whole UI
- Confusing AnimatedBuilder with navigation widgets
- Assuming it handles user input
AnimatedBuilder widget?Solution
Step 1: Recall AnimatedBuilder parameters
AnimatedBuilder requires an animation, a builder function with context and child, and optionally a child widget.Step 2: Match correct parameter order and names
AnimatedBuilder(animation: myAnimation, builder: (context, child) => Widget, child: childWidget) correctly uses animation, builder, and child parameters in proper order and types.Final Answer:
AnimatedBuilder(animation: myAnimation, builder: (context, child) => Widget, child: childWidget) -> Option DQuick Check:
Correct parameter names and order = D [OK]
- Mixing parameter names and order
- Passing wrong types to parameters
- Confusing builder and child roles
AnimatedBuilder:
AnimatedBuilder(
animation: animationController,
builder: (context, child) {
return Opacity(
opacity: animationController.value,
child: child,
);
},
child: const Text('Hello'),
)
What will be the visible effect when the animation runs from 0 to 1?Solution
Step 1: Analyze the builder function
The builder returns an Opacity widget with opacity set to animationController.value, which changes from 0 to 1.Step 2: Understand the effect on child
The child is the Text widget 'Hello'. Opacity changes from transparent (0) to fully visible (1), causing a fade-in effect.Final Answer:
The text 'Hello' fades in from invisible to fully visible -> Option AQuick Check:
Opacity animation from 0 to 1 = fade-in [OK]
- Confusing opacity with movement or color
- Ignoring the child parameter usage
- Assuming scaling instead of opacity change
AnimatedBuilder usage?
AnimatedBuilder(
animation: animationController,
builder: (context) {
return Transform.rotate(
angle: animationController.value,
child: Text('Rotate'),
);
},
)Solution
Step 1: Check builder function signature
The builder function must accept two parameters: context and child, but here it only has context.Step 2: Understand AnimatedBuilder usage
Missing the child parameter can cause unnecessary rebuilds and is against the recommended pattern.Final Answer:
The builder function is missing the child parameter -> Option AQuick Check:
Builder needs (context, child) parameters [OK]
- Omitting the child parameter in builder
- Misusing animation value types
- Thinking Transform.rotate needs duration
AnimatedBuilder with a Tween from 100 to 200 pixels. Which approach correctly optimizes performance by avoiding rebuilding the static child widget?Solution
Step 1: Understand AnimatedBuilder optimization
Passing the static child widget as the child parameter prevents it from rebuilding on every animation tick.Step 2: Avoid rebuilding static widgets inside builder
Creating the child inside builder causes unnecessary rebuilds, hurting performance.Final Answer:
Pass the static child widget as thechildparameter and use it inside the builder without rebuilding -> Option BQuick Check:
Use child parameter to optimize rebuilds = C [OK]
- Building static widgets inside builder repeatedly
- Using setState inside builder incorrectly
- Misunderstanding child parameter purpose
Ready to prove it?
Pass the test and this pattern moves unknown
