如何响应向下和向上滚动?

문준석*_*문준석 5 dart flutter

我想知道在列表视图中向上或向下滚动时滚动的方向。但我找不到解决方案。

我尝试使用 ScrollController 来监听滚动方向,以处理向上和向下滚动等不同的工作。但是没有办法用 ScrollController 来监听它。有没有人处理过这个问题?

cre*_*not 8

您可以利用NotificationListener<ScrollNotification>. 您需要将滚动视图包装在该小部件中,然后收听UserScrollNotifications:

NotificationListener<ScrollNotification>(
  onNotification: (ScrollNotification notification) {
    if (notification is UserScrollNotification) {
      if (notification.direction == ScrollDirection.forward) {
        // Handle scroll down.
      } else if (notification.direction == ScrollDirection.reverse) {
        // Handle scroll up.
      }
    }

    // Returning null (or false) to
    // "allow the notification to continue to be dispatched to further ancestors".
    return null;
  },
  child: ListView(..), // Or whatever scroll view you use.
)
Run Code Online (Sandbox Code Playgroud)