一个AnimationController, 两个Animation,在一个页面中。分别控制AnimatedLogo和AnimatedText。
import 'package:flutter/material.dart';
void main(List<String> args) {
runApp(
const MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
),
);
}
class AnimatedLogo extends AnimatedWidget {
const AnimatedLogo({super.key, required super.listenable});
@override
Widget build(BuildContext context) {
Animation animation = super.listenable as Animation;
return SizedBox(
width: animation.value,
height: animation.value,
child: const FlutterLogo(),
);
}
}
class AnimatedText extends AnimatedWidget {
const AnimatedText(
{super.key, required this.text, required super.listenable});
final String text;
@override
Widget build(BuildContext context) {
Animation animation = super.listenable as Animation;
return Text(
text,
style: TextStyle(fontSize: animation.value),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
late AnimationController controller;
late Animation animationLogo;
late Animation animationText;
@override
void initState() {
controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 3),
);
animationLogo = Tween<double>(begin: 0, end: 300).animate(controller);
animationText = Tween<double>(begin: 0, end: 100).animate(controller);
controller.repeat(reverse: true);
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
AnimatedLogo(listenable: animationLogo),
AnimatedText(text: "AnimatedText", listenable: animationText),
],
),
),
);
}
}