0

I am using getx https://pub.dev/packages/get to management the flutter state, and having an infinity scroll list. when user scroll the list, load more and more articles and append to the list end. Now I am facing a problem that when load more articles, will trigger the getx update method, that's make the ListView component auto scroll to top. is it possible to keep the scroll position when load more elements? this is the code looks like:

  child: CupertinoScrollbar(
                            controller: scrollController,
                            child: SmartRefresher(
                                onRefresh: _onRefreshLoadingNewestArticle,
                                enablePullUp: true,
                                enablePullDown: true,
                                header: WaterDropMaterialHeader(),
                                controller: _refreshController,
                                onLoading: _loadingMoreArticle,
                                footer: CustomFooter(
                                  builder: (BuildContext context, LoadStatus? mode) {
                                    Widget body;
                                    if (mode == LoadStatus.idle) {
                                      body = Text("load more");
                                    } else if (mode == LoadStatus.loading) {
                                      body = CupertinoActivityIndicator();
                                    } else if (mode == LoadStatus.failed) {
                                      body = Text("click to retry");
                                    } else if (mode == LoadStatus.canLoading) {
                                      body = Text("release to load more");
                                    } else {
                                      body = Text("no more data");
                                    }
                                    return Container(
                                      height: 55.0,
                                      child: Center(child: body),
                                    );
                                  },
                                ),
                                child: CustomScrollView(
                                  key: PageStorageKey(StoriesType.subStories),
                                  controller: scrollController,
                                  slivers: <Widget>[
                                    SliverOverlapInjector(
                                      handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
                                        context,
                                      ),
                                    ),
                                    if (controller.articles.length > 0)
                                      SliverPadding(
                                        padding: const EdgeInsets.symmetric(vertical: 8.0),
                                        sliver: buildArticleList(),
                                      )
                                  ],
                                ))));

this is the load more article function looks like:

Future loadingMoreArticles() async {
    articleRequest.pageNum = articleRequest.pageNum + 1;
    List<Item> extraArticles = await Repo.getArticles(articleRequest);
    if (extraArticles.isNotEmpty) {
      articles.addAll(extraArticles);
      update();
    }
  }

what should I do to keep the scroll position when update the List element?

Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • 1
    Check out this post: https://stackoverflow.com/a/60293181/11708327. The answer may be to use AutomaticKeepAlives to never lose your scroll position (even when changing tabs) – Lars Sep 12 '22 at 13:52

0 Answers0