我正在尝试学习Flutter SizeTransition。我使用SizeTransition并提供了sizeFactor作为动画,并提供了介于0到1之间的补间。我在构建中执行了一个函数,该函数在几秒钟后被执行。我期望的是徽标的大小将分别在动画正向和反向时增加和减少。 。但是我注意到徽标首先向下移动,然后向上返回。(例如幻灯片过渡)
小工具来测试SizeTransition
import 'dart:async';
import 'package:flutter/material.dart';
class LogoApp extends StatefulWidget {
_LogoAppState createState() => _LogoAppState();
}
class _LogoAppState extends State<LogoApp> with TickerProviderStateMixin {
AnimationController _animationController;
Animation<double> _animation;
@override
void initState() {
super.initState();
_animationController =
AnimationController(vsync: this, duration: Duration(seconds: 4));
_animation = _animationController.drive(Tween(begin: 0, end: 1));
}
int ctr = 0;
@override
Widget build(BuildContext context) {
ctr += 1;
print("build$ctr");
execute(); //function that executes forward()/reverse() methods of animationController
return SizeTransition(
sizeFactor: _animation,
child: Center(
child: FlutterLogo(),
),
);
}
void execute() async {
Future.delayed(const Duration(seconds: 2), () {
_animationController.forward();
});
Future.delayed(const Duration(seconds: 4), () {
_animationController.reverse();
});
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: LogoApp(),
);
}
}
我尝试了很多,但没有成功。我还能做什么?
答案 0 :(得分:1)
我认为您真正要实现的是ScaleTransition()
而不是SizeTransition()
。
这是一个非常简单的解决方法:
int ctr = 0;
@override
Widget build(BuildContext context) {
ctr += 1;
print("build$ctr");
execute(); //function that executes forward()/reverse() methods of animationController
return Center(
child: ScaleTransition(
scale: _animation,
child: FlutterLogo(),
),
);
}
您还需要将Center()
小部件上移一级(如代码所示),以确保将整个动画锚定在显示的中心-如您最初的预期。