我试图在我的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
中创建了动画控制器后添加了侦听器,则侦听器将起作用。像这样:
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();
}