我正在为我的新项目尝试 GetX,并尝试使用受到 this comment 启发的 AnimationController。使用 Tween -> blur & spread
和 AnimationController -> duration
的默认值,一切正常。
我在做什么:
1: 用对应的widget
创建一个controller
(控制器通过GetMaterialApp的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:在 widget
中使用此 view
。
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
using:
child: CustomAnimatedWidget(duration: 4, blur: 2, spread: 10);
但不知道如何将它与 _animationController
和 _animation
一起使用,因为它们是在 onInit
中调用的。
请分享您的解决方案,谢谢。