在 Flutter 中的嵌套 "GestureDetectors" 上接收 "onVerticalDragUpdate"

Receive "onVerticalDragUpdate" on nested "GestureDetectors" in Flutter

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

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),
        ));
  }
}

嗯,这是你需要的吗?

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

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

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

顶部的小部件是垂直的 'test1',内部的小部件是横向的 'test2'。

希望对尼克拉斯有所帮助 ;-)

对于所有感兴趣的人,我是这样解决的:

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