Flutter web GestureDetector:检测鼠标滚轮事件 [英] Flutter web GestureDetector: detect mouse wheel events

查看:227
本文介绍了Flutter web GestureDetector:检测鼠标滚轮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用于 Web 的 Flutter 应用程序,当我在小部件(不是滚动小部件)内滚动鼠标滚轮时,我试图执行回调方法.

I am developing a flutter app for web, and I am trying to execute a callback method when I scroll the mouse wheel inside a widget (which is not a scrolling widget).

我知道 MouseRegion 小部件,并且使用 Listener 作为它的子级我可以检测 onPointerSignal 事件.当 Listener 的孩子是一个可滚动的小部件时,这可以正常工作,例如 ListView,但如果此小部件不可滚动,则它不起作用.

I am aware of the MouseRegion widget, and using a Listener as its child I can detect onPointerSignal events. This works fine when the Listener's child is a scrollable widget, such a ListView, but it does not work if this widget is not scrollable.

我最想要的是一个类似于 GestureDetector 的小部件,其回调方法类似于 onPanStartonPanUpdateonPanEnd,而是发生在鼠标滚轮事件上(或者,以同样的方式,针对笔记本电脑上的触控板滚动事件).

What I ideally would like to have is a widget similar to GestureDetector, with callback methods that are similar to onPanStart, onPanUpdate and onPanEnd, but instead happening on mouse wheel events (or, in the same way, for trackpad scrolling events on laptops).

有人知道我怎样才能做到这一点吗?

Anybody knows how can I achieve this?

推荐答案

Listener 不需要任何父级.可以使用如下所示的小部件,它将其子部件包装在 Listener 中,并在接收到 PointerScrollEvent 时调用给定的回调:

Listener does not need any parent. A widget like the following can be used, which wraps its child in a Listener and calls the given callback when a PointerScrollEvent is received:

class ScrollDetector extends StatelessWidget {
  final void Function(PointerScrollEvent event) onPointerScroll;
  final Widget child;

  const ScrollDetector({
    Key key,
    @required this.onPointerScroll,
    @required this.child,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Listener(
      onPointerSignal: (pointerSignal) {
        if (pointerSignal is PointerScrollEvent) onPointerScroll(pointerSignal);
      },
      child: child,
    );
  }
}

这是一个关于 DartPad 的完整示例.

这篇关于Flutter web GestureDetector:检测鼠标滚轮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆