First you need to make ExploreScreen a StatefulWidget. That is because you need to preserve the state of the scroll controller.
Next add a ScrollController property in _ExploreScreenState:
ScrollController _controller;
Then, add a function called scrollListener(), which is the function callback that will listen to the scroll offsets.
void _scrollListener() {
// 1
if (_controller.offset >= _controller.position.maxScrollExtent &&
!_controller.position.outOfRange) {
print('i am at the bottom!');
}
// 2
if (_controller.offset <= _controller.position.minScrollExtent &&
!_controller.position.outOfRange) {
print('i am at the top!');
}
}
Here’s how the code works:
Check the scroll offset, and see if the position is greater than or equal to the maxScrollExtent. That means the user has scrolled to the very bottom.
Check if the scroll offset is less than or equal to the minScrollExtend. That means the user has scrolled to the very top.
Within _ExploreScreenState, override the initState() method as shown below:
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.