我有一个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'.的一个错误
所以有一种方法可以在GetX状态管理中使用这个AnimationController吗?
发布于 2021-05-21 06:58:34
我找到了一个解决方案,只需添加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,
),
);
}
}发布于 2022-01-05 16:30:42
这就是我对这个问题的解决方案--感谢@Mahmoud Harooney
class CartController extends GetxController with GetSingleTickerProviderStateMixin {
final ICartService cartService;
final IOrdersService ordersService;
late AnimationController _badgeShopCartAnimationController;
late Animation badgeShopCartAnimation;
CartController({required this.cartService, required this.ordersService});
@override
void onInit() {
super.onInit();
_badgeShopCartAnimationSetup();
}
void addCartItem(Product product) {
cartService.addCartItem(product);
_badgeShopCartAnimationPlay();
}
void _badgeShopCartAnimationSetup() {
_badgeShopCartAnimationController =
AnimationController(duration: const Duration(milliseconds: 225), vsync: this);
badgeShopCartAnimation = Tween<Offset>(
begin: const Offset(0, 0.0),
end: const Offset(.2, 0.0),
).animate(
CurvedAnimation(
parent: _badgeShopCartAnimationController,
curve: Curves.elasticIn,
),
);
}
void _badgeShopCartAnimationPlay() async {
await _badgeShopCartAnimationController.forward();
await _badgeShopCartAnimationController.reverse();
}
}https://stackoverflow.com/questions/67628795
复制相似问题