使用AnimatedBuilder,并且使用多个GrowTransition
import 'package:flutter/material.dart';
void main(List<String> args) {
runApp(
const MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
),
);
}
class GrowTransition extends StatelessWidget {
const GrowTransition(
{super.key, required this.widget, required this.animation});
final Widget widget;
final Animation animation;
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: animation,
builder: (context, child) {
return SizedBox(
width: animation.value,
height: animation.value,
child: widget,
);
},
);
}
}
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 animation;
@override
void initState() {
controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 3),
);
animation = Tween<double>(begin: 0, end: 300)
.chain(CurveTween(curve: Curves.fastOutSlowIn))
.animate(controller);
controller.repeat(reverse: true);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GrowTransition(
widget: const FlutterLogo(),
animation: animation,
),
GrowTransition(
widget: const FittedBox(child: Text("ABC")),
animation: animation),
],
),
),
);
}
}