0

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.

Amirhossein Shahbazi
  • 1,082
  • 1
  • 6
  • 13
Allan
  • 110
  • 9
  • see https://medium.com/flutter/flutter-dont-fear-the-garbage-collector-d69b3ff1ca30 - they say: *"It’s not uncommon to see new Flutter developers create references to widgets they know will not change over time, and place them in state so that they won’t be destroyed and rebuilt. Don’t do this."* – pskink Aug 22 '21 at 06:08
  • @pskink I can't comprehend the statement and the article too. – Allan Aug 22 '21 at 06:19
  • basically you should not worry about the fact that `build()` method is called every time you move to the next page – pskink Aug 22 '21 at 06:24
  • @pskink I've created and destroyed widget on new page. But the thing is my app becomes too janky when I swipe to a different page due to the creation of heavy widgets when swiping to new page. I've optimized to page to be created to the most. But the widgets in the new pages are too demanding. That's why I decided to reuse the widget(InAppwebView) and feed link to it when new page get's changed. My app is too too janky. – Allan Aug 22 '21 at 06:25
  • how come `build` method takes that long? are you building 1000s widgets there? note that `build` should not contain any side effects code - it should just build and return some widgets – pskink Aug 23 '21 at 10:24
  • @pskink Please Check this, this is my post. https://stackoverflow.com/questions/68847830/how-to-make-pageview-builder-scrolling-animation-from-laggy-to-extremely-smoot . I've included an image in this post. You can see pageview itself is not so stable even with small widget building, it gets janky. – Allan Aug 23 '21 at 14:09

2 Answers2

0

You can try PageView+Children and AutomaticKeepAliveClientMixin

  • I can't make the infinite scrolling work in pageView. Is there any workaround to do that? – Allan Aug 22 '21 at 09:24
  • If I use the Children method, I have no good way. If you are using the Builder method, you can set itemCount to a larger value. – 魔咔啦咔 Aug 23 '21 at 07:58
0

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");
  }
)
Jerin
  • 688
  • 1
  • 9
  • 21