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

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];
    );
  },
)

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}'),
        );
      },
)

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,
);

Tags:

Dart

Flutter