Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AnimatedWidget and AnimatedBuilder in Flutter

Hi everyone ,I have a problem, I don’t understand the difference between AnimatedWidget and AnimatedBuilder. The comments in the source code are as follows:

AnimatedWidget:

/// For more complex case involving additional state, consider using
/// [AnimatedBuilder].

AnimatedBuilder:

/// For simple cases without additional state, consider using
/// [AnimatedWidget].

I want to know how to choose between them, because I don't quite understand the documentation, thanks!

like image 362
Liu Silong Avatar asked Jan 03 '23 01:01

Liu Silong


1 Answers

There's no real difference between them besides the syntax needed to use it.

To be clear, this is the code of AnimatedBuilder :

class AnimatedBuilder extends AnimatedWidget {
  const AnimatedBuilder({
    Key key,
    @required Listenable animation,
    @required this.builder,
    this.child,
  }) : assert(builder != null),
      super(key: key, listenable: animation);

  final TransitionBuilder builder;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return builder(context, child);
  }
}

...Yup, does nothing

From this code we can clearly see that AnimatedBuilder is just a different syntax of using AnimatedWidget. Since AnimatedBuilder is an AnimatedWidget that delegate all the layout logic to a callback

So in the end, it's really up to you. Both do the same thing. Use what makes it more readable for you

like image 161
Rémi Rousselet Avatar answered Jan 24 '23 17:01

Rémi Rousselet