In this tutorial , we will learn how to get or listen to a Scroll position change and its current position offset on Scrolling widgets like SingleChildScrollView(), ListView().
ScrollController scrollController = ScrollController();
Set this controller to SingleChildScrollView() or ListView():
SingleChildScrollView(
controller: scrollController
)
ListView(
controller: scrollController
)
Get Current Scroll Position Offset:
print(scrollController.offset);
//.offset is double value
/* Outputs --------
I/flutter (26854): 32.349831321022975
I/flutter (26854): 33.07679332386385
I/flutter (26854): 33.80375532670473
I/flutter (26854): 34.530717329545666
I/flutter (26854): 35.2576793323866
I/flutter (26854): 35.62113813920479
I/flutter (26854): 35.984641335227536
I/flutter (26854): 36.34810014204572
I/flutter (26854): 36.71160333806847
I/flutter (26854): 37.07506214488666
I/flutter (26854): 37.80202414772759
I/flutter (26854): 38.16552734375034
*/
ScrollController scrollController = ScrollController();
scrollController.addListener(() { //listener
print(scrollController.offset);
//.offset is double value
/* Outputs --------
I/flutter (26854): 35.984641335227536
I/flutter (26854): 36.34810014204572
I/flutter (26854): 36.71160333806847
I/flutter (26854): 37.07506214488666
I/flutter (26854): 37.80202414772759
I/flutter (26854): 38.16552734375034
*/
});
In this way, you can fetch the current scroll position of SingleChildScrollView() or ListView() or set Scroll Position change Listener in Flutter App.