我正在尝试学习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(),
    );
  }
}

我已经尝试了很多,但没有成功。我还可以做些什么?

最佳答案

我认为您实际上打算实现的是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()小部件上移一级(如代码所示),以确保将整个动画 anchor 定在显示的中心-如您最初的预期。

关于android - Flutter SizeTransition无法正常工作。尺寸转换行为就像我在滑动小部件一样,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57781852/

10-10 10:43