如何解决AnimatedSize或其他需要垂直同步的内置动画与 GetX
到目前为止,在依赖注入和状态管理方面,GetX 一直是我们的福音。但是,我们遇到了一个问题,即我们无法让内置动画小部件正常工作,
这些是需要 vsync 值的内置小部件。在我们的例子中,一个 AnimatedSize,但也可以是其他的。
不需要 vsync 属性的那些,例如 AnimatedContainer,可以与如下所示的控制器配合使用:
class BottomNavigationController extends GetxController with SingleGetTickerProviderMixin {
final height = 0.0.obs;
...
}
然后像这样使用它...
final BottomNavigation controller = Get.put(BottomNavigationController());
...
AnimatedContainer(
duration: Duration(seconds: 1),child: PopUpMenu(),height: controller.height.value
)
但在例如 AnimatedSize 的情况下,小部件需要一个 vsync 值...关于我将如何做到这一点的任何想法?
if (navigationController.isMenuOpen.value)
AnimatedSize(
vsync: **value here can't be null**,duration: Duration(seconds: 1),)
解决方法
如果您有一个需要 vsync 的小部件,只需传入整个控制器。因为对象本身就是提供 TickerProvider
的东西。就像您在有状态小部件的 AnimationController
中初始化 initState
一样,当您传入 SingleTickerProviderMixin
时,扩展 TickerProvider
的小部件本身会提供 this
在vsync
。
if (navigationController.isMenuOpen.value)
AnimatedSize(
vsync: controller,// full GetX controller
duration: Duration(seconds: 1),child: PopUpMenu(),
如果您使用的东西需要动画控制器,则需要声明并初始化要在 GetX 类中使用的动画控制器。
class BottomNavigationController extends GetxController
with SingleGetTickerProviderMixin {
final height = 0.0.obs;
AnimationController animationController;
@override
void onInit() {
super.onInit();
animationController =
AnimationController(vsync: this,duration: Duration(seconds: 1)); // or whatever your desired duration
}
}
然后,如果您在需要传入动画控制器的小部件中
controller.animationController
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。