gpt4 book ai didi

flutter - Flutter AnimationController监听器未触发

转载 作者:行者123 更新时间:2023-12-03 03:58:10 24 4
gpt4 key购买 nike

我试图在我的Bloc中使用AnimationController将图像每隔几秒钟发送到有状态小部件。以下代码段显示了设置。尽管触发了动画自动收报器(可以通过自动收报器打印语句看到),但侦听器从不执行。

有任何想法吗?

 class RaspDataBloc extends Bloc<RaspDataEvent, RaspDataState>
implements TickerProvider {
...
void startImageAnimation() {
_forecastImageAnimationController = AnimationController(
value: 0,
duration: Duration(milliseconds: 15000),
lowerBound: 0,
upperBound: _forecastTimes.length.toDouble(),
vsync: this)
..repeat()
..addListener(() {
_postForecastImageSet(_forecastImageAnimationController.value); <<< doesn't execute
});
_forecastImageAnimationController.forward();
}

@override
Ticker createTicker(onTick) { <<<< This executes
print('Creating Ticker');
return Ticker(tickerDuration);
}

tickerDuration(Duration elapsed) {
print('Ticker duration: $elapsed.inMilliseconds'); <<<< This fires regularly as expected
}

Stream<RaspDataState> _postForecastImageSet(double value) async* {
print("Animation value: $_forecastImageAnimationController.value");
var imageIndex = value.toInt();
if (imageIndex < _imageSets.length) {
print("Posting imageSet[$imageIndex]");
yield new RaspForecastImageDisplay(_imageSets[imageIndex]);
}
}

最佳答案

您使用TickerProvider的原因是否特殊?通常,使用AnimationController可以使用SingleTickerProviderStateMixin。如果这样做,并在initState中创建了动画 Controller 后添加了侦听器,则您的侦听器将起作用。像这样:

class _MyStatefulWidgetState extends State<MyStatefulWidget> with SingleTickerProviderStateMixin {

AnimationController _animationController;

@override
initState(){
_animationController = AnimationController(
vsync: this,
duration: Duration(seconds: 2)
);
_animationController.addListener((){
print('Animation Controller Listener');
});
super.initState();
}

关于flutter - Flutter AnimationController监听器未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59482042/

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