What is the difference between AnimatedBuilder and AnimatedWidget?
TL;DR: AnimatedBuilder is a general-purpose widget that rebuilds its builder on every animation tick. AnimatedWidget is a base class for creating reusable animated widgets that subscribe to a Listenable internally.
Full Answer
Both serve the same purpose โ rebuilding part of the UI on every animation frame โ but differ in how you use them.
AnimatedBuilder
Use AnimatedBuilder inline when the animation is local to one build() method. Pass a child for the non-animating subtree to avoid rebuilding it.
AnimatedWidget
Subclass AnimatedWidget when you want a reusable, self-contained animated widget. Override build() and use widget.listenable.value (or cast to Animation<T>) directly.
AnimatedWidget is cleaner for library-style widgets. AnimatedBuilder is simpler for one-off animations inside a build method.
Code Examples
// FlutterLogo spins continuously
Common Mistakes
- โForgetting to call super(listenable: animation) in AnimatedWidget
- โNot using the child optimization in AnimatedBuilder โ causes expensive subtrees to rebuild every frame
Interview Tip
AnimatedWidget is essentially a ListenableBuilder specialized for animations. Point out that many built-in Flutter widgets (FadeTransition, ScaleTransition) are subclasses of AnimatedWidget.