@@ -69,6 +69,7 @@ class ListViewCore extends StatefulWidget {
69
69
this .loadingBuilder,
70
70
this .errorBuilder,
71
71
this .separatorBuilder,
72
+ this .onScroll,
72
73
});
73
74
74
75
final bool shrinkWrap;
@@ -89,20 +90,27 @@ class ListViewCore extends StatefulWidget {
89
90
final WidgetBuilder ? errorBuilder;
90
91
final IndexedWidgetBuilder ? separatorBuilder;
91
92
final ItemBuilder itemBuilder;
93
+ final void Function (double )? onScroll;
92
94
93
95
@override
94
96
State <ListViewCore > createState () => _ListViewCoreState ();
95
97
}
96
98
97
99
class _ListViewCoreState extends State <ListViewCore > {
98
100
late final Debouncer debounce;
101
+ late final Debouncer _scrollDebouce;
102
+ late final ScrollController _scrollController;
99
103
100
104
int ? _lastFetchedIndex;
101
105
102
106
@override
103
107
void initState () {
104
108
super .initState ();
105
109
debounce = Debouncer (widget.debounceDuration);
110
+ _scrollDebouce = Debouncer (const Duration (milliseconds: 15 ));
111
+ _scrollController = widget.scrollController ?? ScrollController ();
112
+ _scrollController.addListener (_onScroll);
113
+
106
114
attemptFetch ();
107
115
}
108
116
@@ -116,8 +124,13 @@ class _ListViewCoreState extends State<ListViewCore> {
116
124
117
125
@override
118
126
void dispose () {
119
- super .dispose ();
127
+ _scrollController.removeListener (_onScroll);
128
+ if (widget.scrollController == null ) {
129
+ _scrollController.dispose ();
130
+ }
120
131
debounce.cancel ();
132
+ _scrollDebouce.cancel ();
133
+ super .dispose ();
121
134
}
122
135
123
136
void attemptFetch () {
@@ -135,6 +148,14 @@ class _ListViewCoreState extends State<ListViewCore> {
135
148
}
136
149
}
137
150
151
+ void _onScroll () {
152
+ if (widget.onScroll != null ) {
153
+ _scrollDebouce.run (() {
154
+ widget.onScroll? .call (_scrollController.position.pixels);
155
+ });
156
+ }
157
+ }
158
+
138
159
WidgetBuilder get loadingBuilder =>
139
160
widget.loadingBuilder ??
140
161
(context) => const Center (child: flutter.CircularProgressIndicator ());
@@ -162,7 +183,7 @@ class _ListViewCoreState extends State<ListViewCore> {
162
183
scrollDirection: widget.scrollDirection,
163
184
reverse: widget.reverse,
164
185
shrinkWrap: widget.shrinkWrap,
165
- controller: widget.scrollController ,
186
+ controller: _scrollController ,
166
187
physics: widget.physics,
167
188
cacheExtent: widget.cacheExtent,
168
189
slivers: [
0 commit comments