Android Flatter SizeTransition无法正常工作。大小转换的行为类似于滑动小部件

Android Flatter SizeTransition无法正常工作。大小转换的行为类似于滑动小部件,android,flutter,dart,Android,Flutter,Dart,我试着学习颤振大小的转换。我使用了SizeTransition并提供了sizeFactor作为动画,还提供了介于0和1之间的tween。我在构建中执行了一个函数,该函数在几秒钟后执行,我所期望的是,当动画分别向前和向后时,徽标的大小将增加和减少。但我注意到徽标先向下移动,然后再向上移动(就像幻灯片过渡) 用于测试SizeTransition的小部件 import 'dart:async'; import 'package:flutter/material.dart'; class Logo

我试着学习颤振大小的转换。我使用了SizeTransition并提供了sizeFactor作为动画,还提供了介于0和1之间的tween。我在构建中执行了一个函数,该函数在几秒钟后执行,我所期望的是,当动画分别向前和向后时,徽标的大小将增加和减少。但我注意到徽标先向下移动,然后再向上移动(就像幻灯片过渡)

用于测试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();
    });
  }
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
类LogoApp扩展StatefulWidget{
_LogoAppState createState()=>\u LogoAppState();
}
类_LogoAppState使用TickerProviderStateMixin扩展状态{
AnimationController _AnimationController;
动画(动画),;
@凌驾
void initState(){
super.initState();
_动画控制器=
AnimationController(vsync:this,duration:duration(秒数:4));
_animation=_animationController.drive(Tween(开始:0,结束:1));
}
int ctr=0;
@凌驾
小部件构建(构建上下文){
ctr+=1;
打印(“构建$ctr”);
execute();//执行animationController的forward()/reverse()方法的函数
返回大小转换(
sizeFactor:_动画,
儿童:中心(
child:logo(),
),
);
}
void execute()异步{
未来。延迟(常数持续时间(秒:2),(){
_animationController.forward();
});
未来。延迟(常数持续时间(秒:4),(){
_animationController.reverse();
});
}
}
主飞镖
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:LogoApp(),
);
}
}

我试了很多,但没有成功。我还能做什么呢?

我想你实际上想要实现的是
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()
小部件向上移动一级(如代码中所示),以确保整个动画定位到显示屏的中心-正如您最初预期的那样

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(),
    ),
  );
}