接收“ onVerticalDragUpdate”在嵌套的“ GestureDetectors”上在Flutter [英] Receive "onVerticalDragUpdate" on nested "GestureDetectors" in Flutter

查看:417
本文介绍了接收“ onVerticalDragUpdate”在嵌套的“ GestureDetectors”上在Flutter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段只是我实际情况的最小化版本。在我的实际情况中,这些 GestureDetectors 位于不同的小部件中。我的问题是, onVerticalDragUpdate 事件仅由内部GestureDetector接收。我什至将内部 GestureDetector 行为设置为 HitTestBehavior.translucent ,这意味着事件应该冒泡到父窗口小部件。还是我弄错了?

The following snippet is just a minimalized version of my real situation. In my real situation these GestureDetectors are inside different widgets. My problem is, that the onVerticalDragUpdate event is only received by the inner GestureDetector. I even set the behavior of the inner GestureDetector to HitTestBehavior.translucent, which means that the event should bubble up to parent widgets. Or am I getting there something wrong?

void main() {
  debugPaintPointersEnabled = true;
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onVerticalDragUpdate: (details) {
          var test = "test";
        },
        child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onVerticalDragUpdate: (details) {
            var test = "test";
          },
          child: Container(height: 100, width: 100, color: Colors.red),
        ));
  }
}


推荐答案

每个有兴趣的人,这就是我的解决方法:

For everyone who is interested, this was how I solved it:

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return RawGestureDetector(
        gestures: {
          AllowMultipleVerticalDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<
              AllowMultipleVerticalDragGestureRecognizer>(
            () => AllowMultipleVerticalDragGestureRecognizer(),
            (AllowMultipleVerticalDragGestureRecognizer instance) {
              instance..onEnd = (_) => print("test1");
            },
          )
        },
        child: RawGestureDetector(
          gestures: {
            AllowMultipleVerticalDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<
                AllowMultipleVerticalDragGestureRecognizer>(
              () => AllowMultipleVerticalDragGestureRecognizer(),
              (AllowMultipleVerticalDragGestureRecognizer instance) {
                instance..onEnd = (_) => print("test2");
              },
            )
          },
          child: Container(color: Colors.red),
        ));
  }
}

class AllowMultipleVerticalDragGestureRecognizer extends VerticalDragGestureRecognizer{
  @override
  void rejectGesture(int pointer) {
    acceptGesture(pointer);
  }
}

贷方: https://gist.github.com/Nash0x7E2/08acca529096d93f3df0f60f9c034056

这篇关于接收“ onVerticalDragUpdate”在嵌套的“ GestureDetectors”上在Flutter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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