控件封装为 GrowTranstion 和 LogoWidget。
import 'package:flutter/material.dart';
void main(List<String> args) => runApp(const LogoApp());
class LogoWidget extends StatelessWidget {
const LogoWidget({super.key});
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 10),
child: const FlutterLogo(),
);
}
}
class GrowTransition extends StatelessWidget {
const GrowTransition(
{super.key, required this.child, required this.animation});
final Widget child;
final Animation<double> animation;
@override
Widget build(BuildContext context) {
// 注意! 如果没有Center,就不会有动画
return Center(
child: AnimatedBuilder(
animation: animation,
builder: (context, child) {
return SizedBox(
height: animation.value,
width: animation.value,
child: child,
);
},
child: child,
),
);
}
}
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(
vsync: this,
duration: const Duration(seconds: 2),
);
animation = Tween<double>(begin: 0.0, end: 300.0)
.chain(CurveTween(curve: Curves.fastOutSlowIn))
.animate(controller);
controller.repeat(reverse: true);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return GrowTransition(
animation: animation,
child: const LogoWidget(),
);
}
}