Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have an infinite loop using PageView in Flutter?

Tags:

flutter

dart

Is there a way to have an infinite loop using PageView in Flutter? For example if my PageView has 5 pages, after swiping to page 5, I would be able to swipe again in the same direction to get to Page 1.

like image 675
fadisdh Avatar asked Mar 07 '18 21:03

fadisdh


People also ask

Is there a way to use infinite loop in Pageview?

Sign in to your account Currently in PageView there is no way using infinite loop. For example, my PageView has 3 pages, after swiping to the page 3 I would be able to swipe again in the same direction to get to page 1. And if I'm on the page 1, I would be able to get to the page 3 by swiping to the left.

How to use Pageview widget in flutter?

The PageView widget allows the user to transition between different screens in their flutter application. All you need to set it up are a PageViewController and a PageView. scrollDirection: It sets the axis of scrolling ( Vertical or horizontal ).

How to control visibility of pages in A pageview widget?

Now we can use a PageController and control the visibility of pages. It means, which page is visible in the view is controlled by PageController. It has other powers as well, and we’ll discuss that in detail in the next article that covers PageView.builder constructor. To sum up, a PageView widget generates scrollable pages on the screen.

Is there a way to make the page count infinite?

It's not really infinite, but most users will not scroll 10000 pages back, so it works for my use case. As far as I know, there isn't an elegant way to make it truly infinite. You could probably set up a listener so that whenever the controller.page is about to become 0, you set it back to 10000 * pageCount or something similar.


4 Answers

By default, PageView.builder is infinite in flutter. Unless you provide an itemCount.

The following will print page from 0 to 4 infinitely

final controller = new PageController(initialPage: 999);
 ...

new PageView.builder(
      controller: controller,
      itemBuilder: (context, index) {
        return new Center(
          child: new Text('${index % 5}'),
        );
      },
)
like image 200
Rémi Rousselet Avatar answered Sep 19 '22 01:09

Rémi Rousselet


If you have a list of pre-defined Widgets, you can achieve continuous scrolling using:

return PageView.builder(
  itemBuilder: (context, index) {
    return _children[index % _children.length];
  },
  controller: pageController,
);
like image 34
Tiaan Gerber Avatar answered Sep 20 '22 01:09

Tiaan Gerber


You can achieve infinite scrolling using PageView builder, without giving value to itemCount there will be infinite pages, you just have to maintain the page index which will be painted on screen.

PageView.builder(
  controller: _pageController,
  scrollDirection: Axis.horizontal,
  onPageChanged: (index) {
    setState(() {
      _currentIndex = index % _walkthroughSlides.length;
    });
  },
  itemBuilder: (context, index) {
    return _walkthroughSlides[index % _walkthroughSlides.length];
  },
)
like image 28
Aman Ansari Avatar answered Sep 22 '22 01:09

Aman Ansari


I've found a good solution using this lib https://pub.dev/packages/infinity_page_view

Just import the lib and use InfinityPageView instead of PageView

InfinityPageView(
  controller: infinityPageController,
  itemCount: colorList.length,
  itemBuilder: (context, index) {
    return Container(
      color: colorList[index];
    );
  },
)
like image 27
Renat Fakhrutdinov Avatar answered Sep 19 '22 01:09

Renat Fakhrutdinov