text stringlengths 1 372 |
|---|
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( |
animation: animation, |
builder: (context, child) { |
return SizedBox( |
height: animation.value, |
width: animation.value, |
child: child, |
); |
}, |
child: child, |
), |
); |
} |
} |
<code_end> |
finally, the code to initialize the animation looks very |
similar to the animate2 example. the initState() |
method creates an AnimationController and a tween, |
then binds them with animate(). the magic happens in |
the build() method, which returns a GrowTransition |
object with a LogoWidget as a child, and an animation object to |
drive the transition. these are the three elements listed |
in the bullet points above. |
app source: animate4 |
<topic_end> |
<topic_start> |
simultaneous animations |
<topic_end> |
<topic_start> |
what's the point? |
in this section, you’ll build on the example from |
monitoring the progress of the animation |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.