Using AnimatedBuilder
Looking at the code that we wrote in the last section, there is nothing wrong with it— it's neither too complex nor too big. However, looking closely, we can see a small problem with it—our button animation is mixed up with other widgets. As long as our code does not scale and get more complex, this is fine, but we know this is not the case most of the time, so we might have a real problem.
The AnimatedBuilder
class can help us with the task of separating responsibilities. Our widget, whether it is ElevatedButton
or anything else, does not need to know it is rendered in animation, and breaking down the build
method to widgets that each have a single responsibility can be seen as one of the fundamental concepts in the Flutter framework. Let's look at the AnimatedBuilder
class and then revisit our animation with our new knowledge.
The AnimatedBuilder class
The AnimatedBuilder
widget exists so that we can build complex widgets that...