简体   繁体   中英

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

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

its like comparing TextFormField and TextEditingController

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. There is no explicit way to monitor when an AnimatedContainer is finished animating.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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