如何使用另一个 GestureDetector 包装 GestureDetector 并在各处获取事件

问题描述

如果我有一个带有内部 GestureDetectorGestureDetector,我该如何设置它以便两个检测器都接收点击事件?

你可以在这里看到运行代码https://dartpad.dev/37807a51a48e52eda81c24cf67260c33

GestureDetector(
      onTap: () => print("Log 1"),child: GestureDetector(
        onTap: () => print("Log 2"),child: Text("CLICK ME")
      )
);
  • 每次我点击文本时,它都会打印 Log 2

  • 我已经尝试设置 HitTestBehavior.traslucent 和所有这些,但没有成功。

  • 我希望它打印 Log 1Log 2

解决方法

    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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...