简体   繁体   English

Flutter AnimatedContainer vs AnimationController

[英]Flutter AnimatedContainer vs AnimationController

I can't find the difference between the two, and why you would want to use one above the other.我找不到两者之间的区别,以及为什么要在另一个之上使用一个。

The way I see it, both of them pretty much does the same thing.在我看来,他们俩几乎都在做同样的事情。

Why and where would you use one above the other?为什么以及在哪里使用一个高于另一个?

AnimatedContainer is used for getting things done fast and for simple uses because it uses implicit animations to animate the change in its properties like a change in color or size (width,height) So it makes the animation for you and you don't need to worry or write boilerplate AnimatedContainer用于快速完成工作并用于简单用途,因为它使用隐式动画来动画其属性的变化,例如颜色或大小(宽度,高度)的变化所以它为您制作 animation,您不需要担心或写样板

However, AnimationController is used when you need more control on the animation you want to create and when the animation is complex in this case you will need to do an explicit animation( ie using a controller and animation object explicitly and not implicitly as in case of AnimatedContainer ) now I could provide you with an example code but it would be better if you check this series of videos from official flutter team that explains the difference and when to use each one However, AnimationController is used when you need more control on the animation you want to create and when the animation is complex in this case you will need to do an explicit animation( ie using a controller and animation object explicitly and not implicitly as in case of AnimatedContainer )现在我可以为您提供一个示例代码,但如果您查看官方 flutter 团队的这一系列视频会更好,这些视频解释了差异以及何时使用每个视频

its like comparing TextFormField and TextEditingController就像比较TextFormFieldTextEditingController

they are not the same, they cannot be used to achieve the same goal它们不一样,它们不能用于实现相同的目标

Let's say you'd like an animation to complete first before executing a function.假设您希望先完成 animation,然后再执行 function。 There is no explicit way to monitor when an AnimatedContainer is finished animating.AnimatedContainer完成动画时,没有明确的方法可以监控。

An AnimationController does allow you to monitor this kind of thing with methods like yourAnimatedController.addStatusListner((status) =>...) , as nicely explained here https://stackoverflow.com/a/50473876/6941972 AnimationController确实允许您使用诸如yourAnimatedController.addStatusListner((status) =>...)之类的方法来监视此类事情,正如此处https://stackoverflow.com/a/50473876/6941972所解释的那样

There are use cases for this.有这方面的用例。 For example, you might have a list of items with animations on each tile.例如,您可能在每个图块上都有一个带有动画的项目列表。 If you need to remove an animated tile from the list, you'll need to make sure that whatever animations are running on that list stop first.如果您需要从列表中删除动画图块,则需要确保该列表上运行的任何动画首先停止。 That's not always easy to do with an AnimatedContainer .使用AnimatedContainer并不总是那么容易做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM