Anu Tig3r
Anu Tig3r

Reputation: 165

Flutter GetX: Animation with GetxController

I'm trying GetX for my new project and tried to use AnimationController which is inspired this comment. Everything works fine with default value for Tween -> blur & spread & AnimationController -> duration.

What I'm doing:

1: Created an widget with corresponding controller (controller is binded through initialBinding of GetMaterialApp).

GetMaterialApp(
  ...
  initialBinding: AnimationBinding()
  ...
);

class AnimationBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<AnimationController>(
      () => AnimationController(),
    );
  }
}

class CustomAnimatedWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<LogoAnimationController>(
      builder: (_) {
        return Container(
          width: 150,
          height: 150,
          child: Text('for demo only, originally its not text widget'),
          ...
          remaining code that uses `_.animation.value`
          ...
          ),
        );
      },
    );
  }
}

class AnimationController extends GetxController with SingleGetTickerProviderMixin {
  Animation animation;
  AnimationController _animationController;

  @override
  void onInit() {
    super.onInit();

    _animationController = AnimationController(
        vsync: this, duration: Duration(seconds: 2));
    _animationController.repeat(reverse: true);

    animation = Tween(begin: 2.0, end: 15.0)
        .animate(_animationController)
          ..addListener(() => update());
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();

    _animationController.dispose();
  }
}

2: Used this widget inside view.

child: CustomAnimatedWidget();

Till now, everything is working fine. But I want to update blur, spread & duration, with: updating CustomAnimatedWidget:

final double blur;
  final double spread;
  final int duration;
  CustomAnimatedWidget({this.blur, this.spread, this.duration});

  ...
      builder: (_) => ...
      ...
      initState: (s) {
        _.blur.value = blur;
        _.spread.value = spread;
        _.duration.value = duration;
      },
  ...

updating AnimationController:

Rx<double> blur = 2.0.obs;
  Rx<double> spread = 15.0.obs;
  Rx<int> duration = 2.obs;

and using/calling the CustomAnimationWidget using:

child: CustomAnimatedWidget(duration: 4, blur: 2, spread: 10);

but don't know how to use it with _animationController & _animation because they are called in onInit.

Please share your solutions, thanks.

Upvotes: 4

Views: 11994

Answers (1)

Syed Daniyal Ali
Syed Daniyal Ali

Reputation: 449

I Extended GetxController with GetTickerProviderStateMixin and then make a AnimationController and CurvedAnimation by code below:

late final AnimationController _controller = AnimationController(
    duration: const Duration(seconds: 2),
    vsync: this,
  )..repeat(reverse: false);

  late final Animation<double> animation = CurvedAnimation(
    parent: _controller,
    curve: Curves.ease,
  );

After that i just created object of my CustomGetxController like this final _customController = Get.put(CustomGetxController()); then i called it like this:

 FadeTransition(
            opacity: driverController.animation,
            child:const Text('My Flashing Text')
          ),

Upvotes: 3

Related Questions