1

I understand how to apply keepAlive in PageView. But when I come to flutter_pagewise ( https://pub.dev/packages/flutter_pagewise ), I was confused about how to implement to keep the state for PagewiseListView since when changing the page in PageView caused this PagewiseListView to build again. Anyone can tell me a solution?

My PageView is:

 PageView(
                children: <Widget>[
                    CardListView(),
                    Container(
                        color: Colors.cyan,
                    ),
                ],

            ),

My CardListView is:

class CardListView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _CardListViewState();
}

class _CardListViewState extends State<CardListView>
    with AutomaticKeepAliveClientMixin<CardListView> {
  static const int PAGE_SIZE = 10;

  var postsMap = Map<int, List<PostModel>>();

  @override
  Widget build(BuildContext context) {

    super.build(context);

    void updatePostsMap(int pageIndex, List<PostModel> posts) {
      setState(() {
        postsMap[pageIndex] = posts;
      });
    }

    return PagewiseListView(
        pageSize: PAGE_SIZE,
        itemBuilder: this._itemBuilder,
        pageFuture: (pageIndex) {
          if (postsMap.containsKey(pageIndex)) {
            print('found');
            return Future.value(postsMap[pageIndex]);
          } else {
            print('load');
            return BackendService.getPosts(pageIndex * PAGE_SIZE, PAGE_SIZE)
                .then((List<PostModel> posts){
                  updatePostsMap(pageIndex, posts);
                  return posts;
            });
          }
        }
    );

  }
George Zhang
  • 371
  • 1
  • 2
  • 9
  • 1
    there is must be one overriden method in CardListViewState class @override bool get wantKeepAlive => true; –  Dec 04 '19 at 05:54
  • Thanks Eugene! It's my bad to copy and paste the method to assign false value. Now it is working. – George Zhang Dec 04 '19 at 17:20

0 Answers0