ScrollController如何检测Scroll的开始,停止和滚动? [英] ScrollController how can I detect Scroll start, stop and scrolling?
本文介绍了ScrollController如何检测Scroll的开始,停止和滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将ScrollController用于 SingleChildScrollView
小部件,我想在其中检测何时滚动开始,结束/停止以及仍在滚动?
I'm using ScrollController for SingleChildScrollView
widget where I want to detect when scroll start, end/stop and still scrolling?
如何检测到我正在使用 Listene
How can I detect, I'm using Listene
scrollController = ScrollController()
..addListener(() {
scrollOffset = _scrollController.offset;
});
也尝试使用 _scrollController.position.activity.velocity
,但对我没有帮助.
Also try with _scrollController.position.activity.velocity
but didn't help me.
也有
_scrollController.position.didEndScroll();
_scrollController.position.didStartScroll();
但是我怎么使用它呢?
推荐答案
只需将您的 SingleChildScrollView
包装到 NotificationListener
并更新您的代码,例如..
Just Wrap your SingleChildScrollView
to NotificationListener
and update your code like ..
NotificationListener<ScrollNotification>(
onNotification: (scrollNotification) {
if (scrollNotification is ScrollStartNotification) {
_onStartScroll(scrollNotification.metrics);
} else if (scrollNotification is ScrollUpdateNotification) {
_onUpdateScroll(scrollNotification.metrics);
} else if (scrollNotification is ScrollEndNotification) {
_onEndScroll(scrollNotification.metrics);
}
},
child: SingleChildScrollView(
/// YOUR OWN CODE HERE
)
)
然后声明
_onStartScroll(ScrollMetrics metrics) {
print("Scroll Start");
}
_onUpdateScroll(ScrollMetrics metrics) {
print("Scroll Update");
}
_onEndScroll(ScrollMetrics metrics) {
print("Scroll End");
}
将通过特定方法通知您.
You will be notify by particular method.
这篇关于ScrollController如何检测Scroll的开始,停止和滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文