gpt4 book ai didi

flutter GetX : Animation with GetxController

转载 作者:行者123 更新时间:2023-12-05 06:00:55 26 4
gpt4 key购买 nike

我正在为我的新项目尝试 GetX,并尝试使用受启发的 AnimationController this comment . Tween -> blur & spread & AnimationController -> duration 的默认值一切正常。

我在做什么:

1: 创建了一个widget和对应的controller(controller是通过GetMaterialApp的initialBinding绑定(bind)的)

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

child: 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;
},
...

更新AnimationController:

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

并使用/调用 CustomAnimationWidget 使用:

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

但不知道如何将它与 _animationController_animation 一起使用,因为它们在 onInit 中被调用。

请分享您的解决方案,谢谢。

最佳答案

我扩展了 GetxControllerGetTickerProviderStateMixin然后做一个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,
);

在那之后,我就像这个最终的 _customController = Get.put(CustomGetxController()); 创建了我的 CustomGetxController 的对象然后我这样调用它:

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

关于 flutter GetX : Animation with GetxController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67438919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com