concept AnimatedWidget in category flutter

appears as: AnimatedWidget, n AnimatedWidget
Flutter in Action

This is an excerpt from Manning's book Flutter in Action.

The last thing an animation needs, because otherwise it would be useless, is the widget that you actually want to animate. The widget you want to animate isn’t the same widget that extends TickerStateProviderMixin, but rather is the child (or children) of that widget. The concepts discussed previously in this chapter are all about setting up an animation. In short, they all work together to get a new value from a tween at every frame change. That value needs to be given to a widget, though. For example, if you’re animating an object’s color from yellow to red, the concepts discussed so far are about calculating the updated color value at every frame change. Now you need to give that color value to a widget. This can be done most simply with AnimatedWidget, a built-in object in the Flutter SDK.

AnimatedWidget is a class that extends StatefulWidget and has a bit more functionality. I encourage you to look at the source code for the AnimatedWidget class on GitHub (http://mng.bz/6w4G); you may be surprised how simple it is.

// weather_app/lib/widget/color_transition_box.dart
class ColorTransitionBox extends AnimatedWidget {    #1
  final Widget child;
 
  ColorTransitionBox({
    this.child,
    Key key,
    Animation<Color> animation,                      #2
    }) : super(key: key, listenable: animation);
 
  @override
  Widget build(BuildContext context) {
    final Animation<Color> animation = listenable;   #3
    return DecoratedBox(
      decoration: BoxDecoration(
        color: animation.value,                      #4
      ),
      child: child,
    );
  }
}