gpt4 book ai didi

android - Flutter SizeTransition 无法正常工作。Size transition 的行为就像我在滑动小部件一样

转载 作者:行者123 更新时间:2023-11-29 22:52:16 25 4
gpt4 key购买 nike

我正在尝试学习 Flutter SizeTransition。我使用 SizeTransition 并提供 sizeFactor 作为动画并提供从 0 到 1 的补间。我在构建中执行了一个函数,该函数在几秒钟后执行,我期望的是当动画分别向前和向后时 Logo 的大小会增加和减小.但是我注意到 Logo 先向下移动然后又向上移动。(就像幻灯片过渡一样)

用于测试 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

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() 小部件向上移动一层(如代码中所示)以确保整个动画锚定在显示的中心 - 正如您最初预期的那样成为。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com