translations.dart阅读
AnimatedWidget
- 它是一个
StatefulWidget
, 初始化传入一个Listenable - 通过
Listenable
内部绑定了setState方法去触发子类的 build方法. - 子类通过继承
AnimatedWidget
,通过传入的Animation
就能触发build方法 - 以AnimateBuilder为例:
_AnimatedState
....
@override
Widget build(BuildContext context) => widget.build(context);
AnimatedWidget
....
@protected
Widget build(BuildContext context);
class AnimatedBuilder extends AnimatedWidget
....
@override
Widget build(BuildContext context) {
return builder(context, child);
}
总结: 子类重写父类的build
方法,父类完成 animtaion value change事件的绑定,子类在 build方法触发时根据当前的animation value来更新界面。 基于这个套路, 还有如下的相关Widget:
/// ## Common animated widgets
///
/// A number of animated widgets ship with the framework. They are usually named
/// FooTransition
, where Foo
is the name of the non-animated
/// version of that widget. The subclasses of this class should not be confused
/// with subclasses of [ImplicitlyAnimatedWidget] (see above), which are usually
/// named AnimatedFoo
. Commonly used animated widgets include:
///
/// * [AnimatedBuilder], which is useful for complex animation use cases and a
/// notable exception to the naming scheme of [AnimatedWidget] subclasses.
/// * [AlignTransition], which is an animated version of [Align].
/// * [DecoratedBoxTransition], which is an animated version of [DecoratedBox].
/// * [DefaultTextStyleTransition], which is an animated version of
/// [DefaultTextStyle].
/// * [PositionedTransition], which is an animated version of [Positioned].
/// * [RelativePositionedTransition], which is an animated version of
/// [Positioned].
/// * [RotationTransition], which animates the rotation of a widget.
/// * [ScaleTransition], which animates the scale of a widget.
/// * [SizeTransition], which animates its own size.
/// * [SlideTransition], which animates the position of a widget relative to
/// its normal position.
/// * [FadeTransition], which is an animated version of [Opacity].
/// * [AnimatedModalBarrier], which is an animated version of [ModalBarrier].
- 从上面的名字上来看,基本都是对Widget进行了一次包装, 命名上在原来的Widget后面加上了Translation.