Ardhana Deya Tanaya
Ardhana Deya Tanaya

Reputation: 21

Carousel cannot Auto-Sliding in Flutter

I am trying to create carousel with auto-sliding. Before I got problem that "PageController.page cannot be accessed before a PageView is built with it"

I can solve it with reference from https://github.com/jlouage/flutter-carousel-pro/issues/21

But unfortunately the carousel cannot be auto-sliding. Please help me.

In carousel_pro.dart I have change like program in below :

final _controller = new PageController();

@override
void initState() {
super.initState();
if (_controller.hasClients) {
  if (widget.autoplay) {
    new Timer.periodic(widget.autoplayDuration, (_) {
      if (_controller.page.round() == widget.images.length-1)  {
        _controller.animateToPage(
          0,
          duration: widget.animationDuration,
          curve: widget.animationCurve,
        );
      } else {
        _controller.nextPage(
            duration: widget.animationDuration,
            curve: widget.animationCurve);
      }
    });
  }
 }
}

Upvotes: 1

Views: 1106

Answers (1)

Shubham Mishra
Shubham Mishra

Reputation: 11

You have to check whether the widget have images or not....

 if (widget.images != null && widget.images.isNotEmpty) {
  if (widget.autoplay) {
    Timer.periodic(widget.autoplayDuration, (_) {
      if (_controller.hasClients) {
        if (_controller.page.round() == widget.images.length - 1) {
          _controller.animateToPage(
            0,
            duration: widget.animationDuration,
            curve: widget.animationCurve,
          );
        } else {
          _controller.nextPage(
              duration: widget.animationDuration,
              curve: widget.animationCurve);
        }
      }
    });
  }
}

Also you have to reinstall the app. It worked for me !!!

Upvotes: 1

Related Questions