MaiaraM / banner_carousel

Package Flutter to Banner Carousel
MIT License
17 stars 6 forks source link

AutoPlay banner not working #5

Open sohailmahmud opened 3 years ago

fredgrott commented 2 years ago

Hello, I might as well answer this:

  1. Switch to stateful

  2. Code this: `@override void initState() { super.initState(); _pageController = PageController( viewportFraction: 0.9, initialPage: 0, ); _timer = Timer.periodic( const Duration(seconds: 5), // ignore: prefer-extracting-callbacks (Timer timer) { _pageController.nextPage( duration: const Duration(milliseconds: 350), curve: Curves.easeIn, ); }, ); }

    @override void dispose() { super.dispose(); _timer.cancel(); }`

  3. Add the pageController parameter to your Carousel

wagnervf commented 5 days ago

One solution to this is to create an autoplay. Example for 3 images. late PageController? _pageController = PageController(); var _timer;

@override void initState() { super.initState(); startBanner(); }

void startBanner() { WidgetsBinding.instance.addPostFrameCallback((_) { _pageController = PageController( viewportFraction: 0.9, initialPage: 0, );

  _timer = Timer.periodic(
    const Duration(seconds: 5),
    (Timer timer) {
      _pageController?.nextPage(
        duration: const Duration(milliseconds: 350),
        curve: Curves.easeIn,
      );

      if (_pageController?.page == 2.0) {
        _pageController?.jumpToPage(0);
      }
    },
  );
});

}

@override void dispose() { super.dispose(); _timer.cancel(); }