简体   繁体   English

Flutter GetX:Animation 与 GetxController

[英]Flutter GetX: Animation with GetxController

I'm trying GetX for my new project and tried to use AnimationController which is inspired this comment .我正在为我的新项目尝试 GetX,并尝试使用受此评论启发的 AnimationController。 Everything works fine with default value for Tween -> blur & spread & AnimationController -> duration . Tween -> blur & spread & AnimationController -> duration的默认值一切正常。

What I'm doing:我在做什么:

1: Created an widget with corresponding controller (controller is binded through initialBinding of GetMaterialApp). 1:创建一个对应controllerwidget (控制器通过initialBinding的initialBinding绑定)。

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 . 2:在view中使用了这个widget

child: CustomAnimatedWidget();

Till now, everything is working fine.到目前为止,一切正常。 But I want to update blur, spread & duration , with: updating CustomAnimatedWidget :但我想更新blur, spread & duration ,其中:更新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 :更新AnimationController

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

and using/calling the CustomAnimationWidget using:并使用/调用CustomAnimationWidget使用:

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 .但不知道如何将它与_animationController & _animation一起使用,因为它们在onInit中被调用。

Please share your solutions, thanks.请分享您的解决方案,谢谢。

I Extended GetxController with GetTickerProviderStateMixin and then make a AnimationController and CurvedAnimation by code below:我使用GetTickerProviderStateMixin扩展GetxController ,然后通过以下代码制作了AnimationControllerCurvedAnimation

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());之后,我刚刚创建了我的 CustomGetxController 的 object,就像这样 final _customController = Get.put(CustomGetxController()); then i called it like this:然后我这样称呼它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter animation controller 使用 GetX - Flutter animation controller using GetX 使用英雄 animation 与 GetX Flutter - Using hero animation with GetX Flutter Flutter 在构建过程中调用了 setState() 或 markNeedsBuild() 错误。 当使用 Getx GetxController 更新 - Flutter error setState() or markNeedsBuild() called during build. when use Getx GetxController update 颤振:GetX。 使用 GetxController 将表单输入数据传递到下一页 - Flutter: GetX. Passing form input data to the next page Using GetxController 如何在 Flutter 中使用 GetX 观察 GetxController 类中的值变化并触发 statelessWidget 类中的导航? - How do I observe a value change in a GetxController class and trigger navigation in the statelessWidget class using GetX in Flutter? 如果我没有 add.obs,如何在 GetxController 中使用 ever() 和 Flutter getx 变量 - How to use ever() with Flutter getx variable in GetxController if I didn't add .obs Flutter 中 GetxController 的用户界面 - UI to GetxController in Flutter Flutter:如何通过 GetxController 控制 PageView? - Flutter: How to control a PageView by GetxController? 如何在颤动中清除getxcontroller中的值? - how to clear value in getxcontroller in flutter? 如何在 flutter 和 getxcontroller 中本地化 CupertinoDatePicker - How to localise CupertinoDatePicker in flutter and getxcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM