'How to make Pageview to show the same widget (without losing state), infinitely when scrolling - Flutter

I have a single page. I want to show the same page again and again without rebuilding it again on the next page or without losing it's state when scrolling ( infinitely)

Here's the below code, (Every time I move to the next page, the widgets[0] get to rebuild. I don't want that. I want to use the widgets[0] from the previous page without rebuilding the new page.]

   Widget widgets = [Page1()];
    PageView.builder(
            controller: PageController(),
            itemBuilder: (BuildContext context, int itemIndex) {
              return widgets[0];
            },
          ),

I have tried using AutomaticKeepAliveClientMixin but it preserves the state of the previous page when I scroll back, but it rebuilds a new widget everytime I go to a new page. I want to reuse the previous widget in the new page.



Solution 1:[1]

You can try PageView+Children and AutomaticKeepAliveClientMixin

Solution 2:[2]

You can use controller to obtain infinite pages

final INFINTE_PAGE_OFFSET = 9999;

final PageController pageController = PageController(
    initialPage: INFINITE_PAGE_OFFSET,
    keepPage: true,
  );

then in your PageView.builder()...

PageView.builder(
  controller: pageController,
  itemBuilder: (context, index) {
    int pageIndex = index - INFINITE_PAGE_OFFSET;
    return Text("Page No. $pageIndex");
  }
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ????
Solution 2 Jerin