Receive “onVerticalDragUpdate” on nested “GestureDetectors” in Flutter

烈酒焚心 提交于 2020-03-25 17:25:52

问题


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

回答1:


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

Credit: https://gist.github.com/Nash0x7E2/08acca529096d93f3df0f60f9c034056




回答2:


mmm is this what you need?

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

The top widget is the vertical 'test1', the inner widget is draghoritzontal 'test2'.

I hope it helps Niklas ;-)



来源:https://stackoverflow.com/questions/58138114/receive-onverticaldragupdate-on-nested-gesturedetectors-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!