dart - ScrollController 如何检测滚动开始、停止和滚动?
问题描述
我正在将 ScrollController 用于SingleChildScrollView
要检测滚动开始、结束/停止和仍在滚动的小部件?
我如何检测,我正在使用Listene
scrollController = ScrollController()
..addListener(() {
scrollOffset = _scrollController.offset;
});
也尝试_scrollController.position.activity.velocity
但没有帮助我。
还有
_scrollController.position.didEndScroll();
_scrollController.position.didStartScroll();
但是我该如何使用它呢?
解决方案
只需将您的 to 包装SingleChildScrollView
起来NotificationListener
并更新您的代码,例如 ..
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");
}
您将通过特定方法收到通知。
推荐阅读
- javascript - TypeScript 编译器是否存在关于泛型的错误,或者我遗漏了什么?
- firebase - 在 Firebase 实时数据库上进行公共读取访问是否安全?
- angular - Angular 访问 dom 元素
- java - 使用 CriteriaBuilder 的 JPA 计数(*)
- php - 服务器端更改以禁用 Firefox Race Cache with Network (RCWN)?
- .net - grpc 服务可以托管在 .Net Framework 或 .Net Standard 中吗
- css - 如何为 4 个项目增加这个 css 动画?
- gnuplot - 在 GnuPlot 中将两个不同大小的图合二为一
- python-3.x - Python unittest.mock google storage - 如何实现 exceptions.NotFound 作为副作用
- flutter - 如何在列表视图项中独立切换布尔值?