flutter_carousel_slider
flutter_carousel_slider copied to clipboard
autoplayDelay Can it be dynamic?
autoplayDelay Can it be dynamic? There are 5 images. The first one shows 3 seconds, the second chapter shows 5 seconds, and the third one shows 10 seconds. AutoplayDelay is dynamic. Currently, it looks like you can only set it once
The only way to achieve this is to set autoloop to false and manually do all the autoplay staff by resetting/starting your own timer.
You can do it like this:
Also do not forget to reset it in the onPageChanged function.
I got the exact same problem and found a solution:
On your state management you should have a variable, let's say "myValue" which will determine your carousel autoPlayInterval, and you would update its value (and then the widget will rebuild itself) whenever the page changed.
options: CarouselOptions(
//other options here
carouselController: carouselController,
autoPlayInterval: Duration(seconds: myValue),
onPageChanged: (index, reason) {
//set your new value here, for example: item[index].time
if (reason == CarouselPageChangedReason.controller) {
//if you are changing pages via controller.nextPage() or controller.previousPage() then it works without problem. I was having problem with manual scrolling or after it changed automatically when the current item's autoPlayInterval time has elapsed, which are the CarouselPageChangedReason.timed and .manual reasons of page changed.
}
else {
//we call animateToPage(index) to the new index (it's an animate to the same position, the same index which now is the current one after the page changed and it works!)
carouselController!.animateToPage(index);
}
},
),
Hope it helps :)
I got the exact same problem and found a solution:
On your state management you should have a variable, let's say "myValue" which will determine your carousel autoPlayInterval, and you would update its value (and then the widget will rebuild itself) whenever the page changed.
options: CarouselOptions( //other options here carouselController: carouselController, autoPlayInterval: Duration(seconds: myValue), onPageChanged: (index, reason) { //set your new value here, for example: item[index].time if (reason == CarouselPageChangedReason.controller) { //if you are changing pages via controller.nextPage() or controller.previousPage() then it works without problem. I was having problem with manual scrolling or after it changed automatically when the current item's autoPlayInterval time has elapsed, which are the CarouselPageChangedReason.timed and .manual reasons of page changed. } else { //we call animateToPage(index) to the new index (it's an animate to the same position, the same index which now is the current one after the page changed and it works!) carouselController!.animateToPage(index); } }, ),
Hope it helps :)
the auto play delay worked, but this caused a problem with the image position because of the rebuild
The only way to achieve this is to set autoloop to false and manually do all the autoplay staff by resetting/starting your own timer. You can do it like this:
Also do not forget to reset it in the onPageChanged function.
this works for me!