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

4,264

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?"));
          },
        )));
Share:
4,264
footurist
Author by

footurist

Updated on December 07, 2022

Comments

  • footurist
    footurist over 1 year

    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?"
                    )
                  );
                },
              )
            )
          );
        }
    }
    
  • footurist
    footurist over 5 years
    Thanks alot. Following your answer, I dug up the part of the docs for raw pointer events and now understand that the bubbling applies to them, not to the gesture, am I right? But why does it work then with the GridView inside the GestureDetector, which is also a scrollable?
  • Trần Trung Hiếu
    Trần Trung Hiếu over 2 years
    Thanks, i struggled for 2 weeks. You are my savior