I recently switch to GetX and want to init animation controller in GetxController
and can access it in GetView
. When app started animation gets to go without any problem but can not forward it again.
class SplashController extends GetxController with GetTickerProviderStateMixin {
var h = 0.0.obs;
late AnimationController ac;
late Animation animation;
Future<void> initAnimation() async {
ac = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
animation = Tween(begin: 0.0, end: 1.0).animate(ac);
}
forwardAnimationFromZero() {
ac.forward(from: 0);
}
@override
void onInit() {
super.onInit();
initAnimation();
forwardAnimationFromZero();
ac.addListener(() {
h.value = animation.value;
});
}
@override
void onReady() {
super.onReady();
forwardAnimationFromZero();
}
@override
void onClose() {
super.onClose();
ac.dispose();
}
}
As you see I extended GetxController with GetTickerProviderStateMixin
but The ticker not work properly.
I define var h = 0.0.obs;
as observable so can access in screen and without it animation does not tick!
class SplashPage extends GetView<SplashController> {
const SplashPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var c = Get.put(SplashController());
return Scaffold(
body: Column(
children: [
Container(
color: Colors.amber,
width: (controller.animation.value * 100) + 100,
height: (controller.animation.value * 100) + 100,
child: Center(
child: Text(
controller.animation.value.toString(),
),
),
)
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
c.ac.forward(from: 0);
},
child: Icon(Icons.refresh),
),
appBar: AppBar(
title: const Text('Splash Page'),
),
);
}
}
in this view when started animation does not react but when I hot relaod
i see it in end state.
when change the Container
widget to:
Obx(
() => Container(
color: Colors.amber,
width: (controller.animation.value * 100) + 100,
height: (controller.h.value * 100) + 100,
child: Center(
child: Text(
controller.animation.value.toString(),
),
),
),
),
respet to ac.addListener(() {h.value = animation.value;});
animation play at the beginning but can't forward again from zero when I press floatingActionButton
.
What I want:
- Why animation does not paly at the beginning without
h
observable? - How can I access animation controller functions in the view?
- When some animation controller complete I want to start another animation controller.
Use AnimatedContainer in this part. Because writing too long will slow down your work as the code grows.
Controller
Page
The following code is used to update your code when it is a StatelessWidget.
Each time you click the button, you will be able to zoom in and out.