text stringlengths 1 474 |
|---|
The AnimatedWidget base class allows you to separate out |
the core widget code from the animation code. |
AnimatedWidget doesn’t need to maintain a State |
object to hold the animation. Add the following AnimatedLogo class: |
<code_start>class AnimatedLogo extends AnimatedWidget { |
const AnimatedLogo({super.key, required Animation<double> animation}) |
: super(listenable: animation); |
@override |
Widget build(BuildContext context) { |
final animation = listenable as Animation<double>; |
return Center( |
child: Container( |
margin: const EdgeInsets.symmetric(vertical: 10), |
height: animation.value, |
width: animation.value, |
child: const FlutterLogo(), |
), |
); |
} |
}<code_end> |
AnimatedLogo uses the current value of the animation |
when drawing itself.The LogoApp still manages the AnimationController and the Tween, |
and it passes the Animation object to AnimatedLogo:App source: animate2<topic_end> |
<topic_start> |
Monitoring the progress of the animation |
<topic_end> |
<topic_start>What's the point? |
It’s often helpful to know when an animation changes state, |
such as finishing, moving forward, or reversing. |
You can get notifications for this with addStatusListener(). |
The following code modifies the previous example so that |
it listens for a state change and prints an update. |
The highlighted line shows the change: |
<code_start>class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin { |
late Animation<double> animation; |
late AnimationController controller; |
@override |
void initState() { |
super.initState(); |
controller = |
AnimationController(duration: const Duration(seconds: 2), vsync: this); |
animation = Tween<double>(begin: 0, end: 300).animate(controller) |
..addStatusListener((status) => print('$status')); |
controller.forward(); |
} |
// ... |
}<code_end> |
Running this code produces this output:Next, use addStatusListener() to reverse the animation |
at the beginning or the end. This creates a “breathing” effect:App source: animate3<topic_end> |
<topic_start> |
Refactoring with AnimatedBuilder |
<topic_end> |
<topic_start>What's the point? |
One problem with the code in the animate3 example, |
is that changing the animation required changing the widget |
that renders the logo. A better solution |
is to separate responsibilities into different classes:You can accomplish this separation with the help of the |
AnimatedBuilder class. An AnimatedBuilder is a |
separate class in the render tree. Like AnimatedWidget, |
AnimatedBuilder automatically listens to notifications |
from the Animation object, and marks the widget tree |
dirty as necessary, so you don’t need to call addListener().The widget tree for the animate4 |
example looks like this:Starting from the bottom of the widget tree, the code for rendering |
the logo is straightforward: |
<code_start>class LogoWidget extends StatelessWidget { |
const LogoWidget({super.key}); |
// Leave out the height and width so it fills the animating parent |
@override |
Widget build(BuildContext context) { |
return Container( |
margin: const EdgeInsets.symmetric(vertical: 10), |
child: const FlutterLogo(), |
); |
} |
}<code_end> |
The middle three blocks in the diagram are all created in the |
build() method in GrowTransition, shown below. |
The GrowTransition widget itself is stateless and holds |
the set of final variables necessary to define the transition animation. |
The build() function creates and returns the AnimatedBuilder, |
which takes the (Anonymous builder) method and the |
LogoWidget object as parameters. The work of rendering the |
transition actually happens in the (Anonymous builder) |
method, which creates a Container of the appropriate size |
to force the LogoWidget to shrink to fit.One tricky point in the code below is that the child looks |
like it’s specified twice. What’s happening is that the |
outer reference of child is passed to AnimatedBuilder, |
which passes it to the anonymous closure, which then uses |
that object as its child. The net result is that the |
AnimatedBuilder is inserted in between the two widgets |
in the render tree. |
<code_start>class GrowTransition extends StatelessWidget { |
const GrowTransition( |
{required this.child, required this.animation, super.key}); |
final Widget child; |
final Animation<double> animation; |
@override |
Widget build(BuildContext context) { |
return Center( |
child: AnimatedBuilder( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.