You will now add an AnimatedBuilder widget to your app to optimize the movement of the ball on the screen:
- In the build method of the _ShapeAnimationState class, remove the actions parameter from the AppBar.
- In the body of the Scaffold, include the Positioned widget in an AnimatedBuilder, and in the builder parameter, add a call to the moveBall method:
return Stack(children: [AnimatedBuilder(
animation: controller,
child: Positioned(left: posLeft, top: posTop, child: Ball()),
builder: (BuildContext context, Widget child) {
moveBall();
return Positioned(left: posLeft, top: posTop, child: Ball());
})]);
- In the moveBall method, remove the setState instructions, so that it looks as shown here:
void moveBall() {
posTop = animation.value * maxTop;
posLeft = animation.value * maxLeft;
}
- In the initState method, when setting the controller AnimationController, append a repeat method, as shown here:
controller = AnimationController(
duration: const Duration...