I am trying for a while to create a widget that can loop between different widgets on a timed basis, and each of the widgets can have its own time to be shown on the screen. This is how far I got already, only the variable timing cannot be fit into this way of working.
This is an implementation of the State of a StatefulWidget. This works, but all of the widgets (from (widget.sponsorWidgets) are shown the _timeoutValue amount of time, while I want to be able to set the time for each widget individually. Kind of like a kiosk where you show some interesting stuff for 30 seconds, and then show an ad for 2 seconds, to turn back to the interesting widget for another 30 seconds.
I was trying with dart.io sleep method, or with await Future.delayed(const Duration(seconds: 1)); But I dont seem to find the right way to do this. Could anyone assit with this please.
Thanks a lot
late int _selectedListIndex;
late int _counterForNonZeroIndex;
late int _timeoutValue;
late Timer _scrollingThing;
final ItemScrollController horizontalItemScrollController =
ItemScrollController();
final ItemPositionsListener itemPositionsListener =
ItemPositionsListener.create();
@override
void initState() {
_selectedListIndex = 0;
_counterForNonZeroIndex = 1;
_timeoutValue = 30;
super.initState();
startScroll();
}
@override
void dispose() {
_scrollingThing.cancel();
super.dispose();
}
void startScroll() {
// Autoscrolling listview code
if (horizontalItemScrollController.isAttached) {
horizontalItemScrollController.scrollTo(
index: _selectedListIndex,
duration: Duration(milliseconds: 300),
curve: Curves.
easeInOutCubic
);
if (!mounted) {
return;
}
}
if (mounted) {
// For changing the list item that is currently scrolled to
_scrollingThing =
Timer.periodic(Duration(seconds: _timeoutValue), (Timer t) {
if (_selectedListIndex == 0) {
_counterForNonZeroIndex += 1;
if (_counterForNonZeroIndex >= widget.sponsorWidgets.length) {
_counterForNonZeroIndex = 1;
}
_selectedListIndex = _counterForNonZeroIndex;
} else {
_timeoutValue = 5;
_selectedListIndex = 0;
}
setState(() {
horizontalItemScrollController.jumpTo(
index: _selectedListIndex,
);
});
});
}
}
``