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