Flutter:当GestureDetector中具有ListView时,显然没有收到Drag事件 [英] Flutter: GestureDetector apparently doesn't receive Drag events when it has a ListView in it

查看:819
本文介绍了Flutter:当GestureDetector中具有ListView时,显然没有收到Drag事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我错过了什么吗?该文档说,事件从最内层的孩子到祖先冒泡,但是下面的代码不会在控制台上打印".它确实打印点击". 将NeverScrollablePhyiscs应用于ListView确实可以,但是我想在两个级别上监听该事件.将HitTestBehavior.translucent应用于GestureDetector不会改变任何内容.

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

推荐答案

您可以使用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?"));
          },
        )));

这篇关于Flutter:当GestureDetector中具有ListView时,显然没有收到Drag事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆