flutter_carousel_slider icon indicating copy to clipboard operation
flutter_carousel_slider copied to clipboard

autoplayDelay Can it be dynamic?

Open yj229201093 opened this issue 2 years ago • 4 comments

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

yj229201093 avatar Jul 01 '22 09:07 yj229201093

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: image Also do not forget to reset it in the onPageChanged function.

Merculiar avatar Feb 28 '23 10:02 Merculiar

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 :)

MatheusFelix1796 avatar Mar 28 '23 19:03 MatheusFelix1796

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

Danilo-Mota avatar Dec 29 '23 21:12 Danilo-Mota

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: image Also do not forget to reset it in the onPageChanged function.

this works for me!

Danilo-Mota avatar Jan 02 '24 05:01 Danilo-Mota