I'm trying to implement some endless scroll pagination in a SingleChildScrollView
.
I'm setting 2 const values:
///describes how many items are loaded into right away
const int initialWidgetsLoadCount = 3;
///describes how many items are loaded after the initial load
///(when triggered by reaching the end of the SingleChildScrollView)
const int nextWidgetsLoadCount = 1;
For this minimal example I'm just filling up a Column
with Text
widgets:
SingleChildScrollView(
controller: _scrollController,
child: Column(children: _texts),
)
For adding Text
widgets I use this method:
void _addTextWidgets(int numberOfWidgets) {
setState(() {
for (int i = 0; i < numberOfWidgets; i++) {
_texts.add(Text("someText${++counter}"));
}
});
}
Now for making the pagination work, in initState()
I first add some initial widgets and start listening to the scrollbar reaching the bottom to load more:
@override
void initState() {
//this is the initial fill request
_addTextWidgets(initialWidgetsLoadCount);
_scrollController.addListener(() {
if (_scrollController.position.maxScrollExtent ==
_scrollController.offset) {
//the bottom of the scrollbar is reached
//adding more widgets
_addTextWidgets(nextWidgetsLoadCount);
}
});
super.initState();
}
The problem:
If I set the initialWidgetsLoadCount
to a high enough number, the height of the Column
will be large enough for the ScrollBar of the SingeChildScrollView
to appear. Then everything works fine.
However, if I set initialWidgetsLoadCount
to a lower number (e.g. 1), the Column
height will be too small for the ScrollBar to appear. Therefore the end of the SingeChildScrollView
cannot be reached by the user and no more items will be loaded.
I could set the initialWidgetsLoadCount
to a high enough number for my device, but there might be a device with a huge resolution.
So I'm tring to figure out a way to tell if the ScrollBar is there or not. And while the ScrollBar is not there yet I can keep loading the initialWidgetsLoadCount
of widgets.
Note: The Add-Button in this example is just for testing purposes! In a real environment I want the user to be able to load all items without having to use a FloatingActionButton
, even with very small values for initialWidgetsLoadCount
.
Nother Note: The size of the list is unknown. I keep fetching data until there is no more data.
Here is a DartPad of this example.