gpt4 book ai didi

flutter - 如何在 GetX 中使用 AnimationController 而不是使用 StatefulWidget 和 SingleTickerProviderStateMixin

转载 作者:行者123 更新时间:2023-12-04 07:38:22 50 4
gpt4 key购买 nike

我有一个 initState()方法并包含 AnimationController如以下代码:

_controller = AnimationController(
vsync: this,
duration: const Duration(
milliseconds: 2500,
),
);
例如我想使用以下方式:
import 'package:get/get.dart';
import 'package:flutter/material.dart';

class ControllerViewModel extends GetxController {
AnimationController _controller;
@override
void onInit() {
// TODO: implement onInit
super.onInit();
_controller = AnimationController(
vsync: this,
duration: const Duration(
milliseconds: 2500,
),
);
}
}
但当然我发现了 The argument type 'ControllerViewModel' can't be assigned to the parameter type 'TickerProvider'. 的错误
所以有一种方法可以使用这个 AnimationController在 GetX 状态管理方式?

最佳答案

我找到了一个解决方案,只需添加 with SingleGetTickerProviderMixin完整代码如下:

import 'package:get/get.dart';
import 'package:flutter/material.dart';

class ControllerViewModel extends GetxController with SingleGetTickerProviderMixin {
AnimationController _controller;
@override
void onInit() {
// TODO: implement onInit
super.onInit();
_controller = AnimationController(
vsync: this,
duration: const Duration(
milliseconds: 2500,
),
);
}
}

关于flutter - 如何在 GetX 中使用 AnimationController 而不是使用 StatefulWidget 和 SingleTickerProviderStateMixin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67628795/

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