flutter_carousel_slider
flutter_carousel_slider copied to clipboard
How to set dynamic time for every page
final List
class ImageSliderDemo extends StatefulWidget { @override State<ImageSliderDemo> createState() => _ImageSliderDemoState(); }
class _ImageSliderDemoState extends State<ImageSliderDemo> { int currentTime = imgList[0]["time"]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Image slider demo')), body: Container( child: CarouselSlider( options: CarouselOptions( autoPlay: true, autoPlayInterval: Duration(seconds: currentTime), onPageChanged: (index, reason) { log("index: $index, time: ${imgList[index]["time"]}"); setState(() { currentTime = imgList[index]["time"]; }); }, ), items: imgList .map((item) => Container( width: MediaQuery.of(context).size.width, child: Center( child: Image.network(item['img'], fit: BoxFit.cover, width: MediaQuery.of(context).size.width,)), )) .toList(), )), ); } }
Hi I am handling it like this , Hope this helps
bro , i also handles same, Actually Carousel Uses Timer, we cannot change time dynamically in Timer but if u want to change you need to rerender whole component
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: myValue = 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! :)