I am facing some issue with the silver list + ListView.builder. The idea is to build the list view as SilverList as it is needed for the SilverStickyHeader. When the user scrolls to the end (position), the next page of the list will be loaded. somehow this kind of code works without SilverList, but not with it. looks the listener of scrollController is not triggered.
and in the ListView.builder method, I have always the initial 20 items. console output has been attached in the end.
@override
Widget build(BuildContext context) {
return SliverStickyHeader(
header: new Container(
height: 40.0,
color: Colors.lightBlue,
padding: EdgeInsets.symmetric(horizontal: 16.0),
alignment: Alignment.centerLeft,
child: new Text(
'Header #0',
style: const TextStyle(color: Colors.white),
),
),
sliver: SliverList(
delegate: SliverChildBuilderDelegate((context, index) {
return ListView.builder(
padding: EdgeInsets.only(left: 16.0, right: 16.0, top: topPadding),
itemBuilder: (BuildContext context, int index) {
print("current index: " + index.toString());
if (list.length == 0 || index == list.length) {
return progressIndicator;
}
if (index > 0) {
return Column(
children: [Divider(), MyListItem(list[index])]
);
}
return MyListItem(list[index]);
},
controller: scrollController,
itemCount: list.length,
shrinkWrap: true
);
}),
)
);
}
@override
void initState() {
super.initState();
print("initState()");
if (list == null || list.length == 0) {
list = this.doInitialize();
}
scrollController.addListener(() {
double maxScroll = scrollController.position.maxScrollExtent;
double currentScroll = scrollController.position.pixels;
double delta = 200.0;
print("maxScroll: " + maxScroll.toString());
print("currentScroll: " + currentScroll.toString());
if ( maxScroll - currentScroll <= delta) {
fetchNextPage();
}
});
}
Future fetchNextPage() async {
print("Fetch the next page");
if (list != null && list.length > 5000) {
print("No more data");
return;
}
if (!isLoading && this.mounted) {
isLoading = true;
final List<MyListDto> result = await doFetchData(page);
print("Found list");
if (mounted) {
page++;
setState(() {
if (result != null) {
if (result.length > 0) {
mergeNewResult(result);
}
} else {
//TODO show notification
}
});
}
isLoading = false;
} else if (isLoading) {
}
}
Future doFetchData(final int page) async {
print("Start to fetch page: " + page.toString());
final list = await MyRepository.instance.getLatestItems(page);
return list.items;
}
void mergeNewResult(List<MyListDto> result) {
print("Merged result, len: " + result.length.toString());
list.addAll(result);
print("Merged result, list len: " + list.length.toString());
}
console output:
I/flutter (15159): Account data loaded.
I/flutter (15159): Building content
I/flutter (15159): current index: 0
I/flutter (15159): current index: 1
I/flutter (15159): current index: 2
I/flutter (15159): current index: 3
I/flutter (15159): current index: 4
I/flutter (15159): current index: 5
I/flutter (15159): current index: 6
I/flutter (15159): current index: 7
I/flutter (15159): current index: 8
I/flutter (15159): current index: 9
I/flutter (15159): current index: 10
I/flutter (15159): current index: 11
I/flutter (15159): current index: 12
I/flutter (15159): current index: 13
I/flutter (15159): current index: 14
I/flutter (15159): current index: 15
I/flutter (15159): current index: 16
I/flutter (15159): current index: 17
I/flutter (15159): current index: 18
I/flutter (15159): current index: 19
I/flutter (15159): Accont loaded
Reloaded 47 of 769 libraries in 1,022ms.
I/flutter (15159): Building content
I/flutter (15159): current index: 0
I/flutter (15159): current index: 1
I/flutter (15159): current index: 2
I/flutter (15159): current index: 3
I/flutter (15159): current index: 4
I/flutter (15159): current index: 5
I/flutter (15159): current index: 6
I/flutter (15159): current index: 7
I/flutter (15159): current index: 8
I/flutter (15159): current index: 9
I/flutter (15159): current index: 10
I/flutter (15159): current index: 11
I/flutter (15159): current index: 12
I/flutter (15159): current index: 13
I/flutter (15159): current index: 14
I/flutter (15159): current index: 15
I/flutter (15159): current index: 16
I/flutter (15159): current index: 17
I/flutter (15159): current index: 18
I/flutter (15159): current index: 19
I/flutter (15159): current index: 0
I/flutter (15159): current index: 1
I/flutter (15159): current index: 2
I/flutter (15159): current index: 3
I/flutter (15159): current index: 4
I/flutter (15159): current index: 5
I/flutter (15159): current index: 6
I/flutter (15159): current index: 7
I/flutter (15159): current index: 8
I/flutter (15159): current index: 9
I/flutter (15159): current index: 10
I/flutter (15159): current index: 11
I/flutter (15159): current index: 12
I/flutter (15159): current index: 13
I/flutter (15159): current index: 14
I/flutter (15159): current index: 15
I/flutter (15159): current index: 16
I/flutter (15159): current index: 17
I/flutter (15159): current index: 18
I/flutter (15159): current index: 19
I/flutter (15159): current index: 0
I/flutter (15159): current index: 1
I/flutter (15159): current index: 2
I/flutter (15159): current index: 3
I/flutter (15159): current index: 4
I/flutter (15159): current index: 5
I/flutter (15159): current index: 6
I/flutter (15159): current index: 7
I/flutter (15159): current index: 8
I/flutter (15159): current index: 9
I/flutter (15159): current index: 10
I/flutter (15159): current index: 11
I/flutter (15159): current index: 12
I/flutter (15159): current index: 13
I/flutter (15159): current index: 14
I/flutter (15159): current index: 15
I/flutter (15159): current index: 16
I/flutter (15159): current index: 17
I/flutter (15159): current index: 18
I/flutter (15159): current index: 19