I want to impeliment animation in flutter with GetxController and GetView to controll my animation in every widget I want and this is my Getx Controller:
class TestAnimationController extends GetxController
with GetTickerProviderStateMixin {
late AnimationController anim;
late Animation<double> animation;
@override
void onInit() {
super.onInit();
initAnimation();
anim.forward();
}
void initAnimation() {
anim = AnimationController(
duration: Duration(seconds: 1),
vsync: this,
);
animation = Tween<double>(begin: 1, end: 0).animate(
CurvedAnimation(
parent: anim,
curve: Interval(0.0, 1.0, curve: Curves.linear),
),
);
}
}
and my GetView class is:
class TestAnimationHome extends GetView<TestAnimationController> {
const TestAnimationHome({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: controller.animation.value + 100,
height: controller.animation.value + 100,
color: Colors.amber,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
controller.anim.forward();
},
child: Icon(
Icons.play_circle,
),
),
);
}
}
I google a lot and do every step that mentioned but my animation doesn’t start and I don’t know where I did wrong!
2
Answers
In addition what I learned from @cavin-macwan 's answer, I want to mention some of my mistakes to anyone has same problem to undrestand better.
first of all, we need
GetBiulder
to track any update in controller and when you 'addListener' to your animation and update the controller with it, TheGetBuilder
will update the UI properly.another mistake was
Tween<double>(begin: 1, end: 0)
that cause thewidth:
ofContainer
change just 1px so you can't see the changes that acually accure. changeTween<double>(begin: 1, end: 0)
toTween<double>(begin: 0, end: 100)
or change the dimentions ofContainer
fromwidth: c.animation.value + 100,
towidth: c.animation.value * 100,
.No need
controller.update();
infloatingActionButton
becauseaddListener
do it after any AnimationController tickes in the controller.Instead of using
GetBuilder
infloatingActionButton
I prefer usingextends GetView<TestAnimationController>
becasue less boiler plate code.You are facing this problem because you are not telling the UI to explicitly draw (e.g setstate). You can see the below implementation in GetX.
I have used addListener function along with GetBuilder in the UI part
Hope it answers your question 😄