text
stringlengths
1
372
(animate3), which used AnimatedWidget
to animate in and out continuously. consider the case
where you want to animate in and out while the
opacity animates from transparent to opaque.
info note
this example shows how to use multiple tweens on the same animation
controller, where each tween manages a different effect in
the animation. it is for illustrative purposes only.
if you were tweening opacity and size in production code,
you’d probably use FadeTransition and SizeTransition
instead.
each tween manages an aspect of the animation. for example:
<code_start>
controller =
AnimationController(duration: const duration(seconds: 2), vsync: this);
sizeAnimation = tween<double>(begin: 0, end: 300).animate(controller);
opacityAnimation = tween<double>(begin: 0.1, end: 1).animate(controller);
<code_end>
you can get the size with sizeAnimation.value and the opacity
with opacityAnimation.value, but the constructor for AnimatedWidget
only takes a single animation object. to solve this problem,
the example creates its own tween objects and explicitly calculates the
values.
change AnimatedLogo to encapsulate its own tween objects,
and its build() method calls tween.evaluate()
on the parent’s animation object to calculate
the required size and opacity values.
the following code shows the changes with highlights:
<code_start>
class AnimatedLogo extends AnimatedWidget {
const AnimatedLogo({super.key, required animation<double> animation})
: super(listenable: animation);
// make the tweens static because they don't change.
static final _opacityTween = tween<double>(begin: 0.1, end: 1);
static final _sizeTween = tween<double>(begin: 0, end: 300);
@override
widget build(BuildContext context) {
final animation = listenable as animation<double>;
return center(
child: opacity(
opacity: _opacityTween.evaluate(animation),
child: container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: _sizeTween.evaluate(animation),
width: _sizeTween.evaluate(animation),
child: const FlutterLogo(),
),
),
);
}
}
class LogoApp extends StatefulWidget {
const LogoApp({super.key});
@override
State<LogoApp> createState() => _LogoAppState();
}
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 = CurvedAnimation(parent: controller, curve: Curves.easeIn)
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.reverse();
} else if (status == AnimationStatus.dismissed) {
controller.forward();
}
});
controller.forward();
}
@override
widget build(BuildContext context) => AnimatedLogo(animation: animation);
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
<code_end>
app source: animate5
<topic_end>
<topic_start>
next steps
this tutorial gives you a foundation for creating animations in
flutter using tweens, but there are many other classes to explore.
you might investigate the specialized tween classes,
animations specific to material design,
ReverseAnimation,
shared element transitions (also known as hero animations),
physics simulations and fling() methods.
see the animations landing page
for the latest available documents and examples.
<topic_end>
<topic_start>
implicit animations
with flutter’s animation library,