Flutter: GestureDetector apparently doesn't receive Drag events when it has a ListView in it

本秂侑毒 提交于 2019-12-24 10:37:45

问题


Am I missing something? The documentation says that events bubble up from the innermost child to the ancestors, but below code will not print "dragged" to the console. It does print "tapped" though. Applying NeverScrollablePhyiscs to the ListView does work, but i want to listen on the event on both levels. Applying HitTestBehavior.translucent to the GestureDetector doesn't change anything.

import "package:flutter/material.dart";

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: MyHomePage(),
      );
    }
}

class MyHomePage extends StatelessWidget {
  @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: GestureDetector(
          onVerticalDragUpdate: (DragUpdateDetails details) {
            print("dragged");
          },
          onTap: () {
            print("tapped");
          },
          child: ListView.builder(
            itemBuilder: (context, index) {
              return Container(
                padding: EdgeInsets.all(20.0),
                child: Text(
                  "The GestureDetector above me does not react to drag events. Maybe my parent is at fault?"
                )
              );
            },
          )
        )
      );
    }
}

回答1:


You can listen for raw pointer events using Listener

      return Scaffold(
            body: Listener(onPointerMove: (opm) {
              print("onPointerMove .. ${opm.position}");
        }, child: ListView.builder(
          itemBuilder: (context, index) {
            return Container(
                padding: EdgeInsets.all(20.0),
                child: Text(
                    "The GestureDetector above me does not react to drag events. Maybe my parent is at fault?"));
          },
        )));


来源:https://stackoverflow.com/questions/52910339/flutter-gesturedetector-apparently-doesnt-receive-drag-events-when-it-has-a-li

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