以下是第二个Flutter的动画示例代码,来源flutter.dev
import 'package:flutter/material.dart';
void main(List<String> args) {
runApp(const LogoApp());
}
class AnimatedLogo extends AnimatedWidget {
const AnimatedLogo({super.key, required super.listenable});
@override
Widget build(BuildContext context) {
final animation = listenable as Animation<double>;
return Center(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: animation.value,
width: animation.value,
child: const FlutterLogo(),
),
);
}
}
class LogoApp extends StatefulWidget {
const LogoApp({super.key});
@override
State<LogoApp> createState() => _LogoAppState();
}
class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
late AnimationController controller;
late Animation<double> animation;
@override
void initState() {
super.initState();
controller =
AnimationController(vsync: this, duration: const Duration(seconds: 3));
animation = Tween(begin: 0.0, end: 300.0).animate(controller);
controller.repeat(reverse: true);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => AnimatedLogo(listenable: animation);
}