简体   繁体   English

GestureDetector 不适用于 Dismissible Widget

[英]GestureDetector is not working with Dismissible Widget

I am trying to get the drag offsets while using Dismissible widget.我试图在使用Dismissible小部件时获得拖动偏移量 So tried to wrap it with GestureDetector but its onHorizontalDragStart is not working.所以试图用GestureDetector包装它,但它的onHorizontalDragStart不起作用。
Tried the either way ie by putting GestureDetector as child of Dismissible but then Dismissible stopped working .尝试了任何一种方式,即将 GestureDetector 作为 Dismissible 的孩子,但随后Dismissible 停止工作
How to solve this?如何解决这个问题? Thnx...谢谢...

ListView.builder(
  itemCount: items.length,
  itemBuilder: (context, index) {
    var item = items[index];
    return GestureDetector(
      onHorizontalDragStart: (e) {
        print(e);
      },
      child: Dismissible(
        key: ValueKey(item),
        background: Container(
          color: Colors.teal,
        ),
        child: ListTile(
          title: Text("item $item"),
        ),
        onDismissed: (d) {
          items.remove(item);
        },
      ),
    );
  },
);

Nested Gesture Widgets嵌套手势小部件

The reason you are having this issue is because both of those widgets receive touch input and when you have two widgets that receive touch input, long story short the child wins that battle.您遇到此问题的原因是因为这两个小部件都接收触摸输入,并且当您有两个小部件接收触摸输入时,长话短说孩子赢得了这场战斗。 Here is the long story.这是长篇大论。 So both of your inputs from your Dismissible and GestureDetector are sent to what is called a GestureArena .因此,您的DismissibleGestureDetector的输入都被发送到所谓的GestureArena There the arena takes into account multiple different factors but the end of the story is the child always wins.那里的竞技场考虑了多种不同的因素,但故事的结局是孩子总是赢。 You can fix this issue by defining your own RawGestureDetector with its own GestureFactory which will change the way the arena performs.您可以通过使用自己的GestureFactory定义自己的RawGestureDetector来解决此问题,这将改变竞技场的执行方式。

ListView.builder(
      physics: AlwaysScrollableScrollPhysics(),
      itemCount: items.length,
      itemBuilder: (BuildContext context, int index) {
        var item = items[index];
        return Listener(
          child: Dismissible(
            key: ValueKey(item),
            child: ListTile(
              title: Text('This is some text'),
            ),
            onDismissed: (DismissDirection direction) {
              items.remove(index);
            },
          ),
          onPointerMove: (PointerMoveEvent move) {
            if (move.localDelta.dx > 1) {//Ideally this number whould be 
//(move.localDelta.dx != 0) but that is too sensitive so you can play with 
//this number till you find something you like. 
              print(move.position);
            }
          },
        );
      },
    );

I want to give all credit to Nash the author of Flutter Deep Dive: Gestures this is a great article I highly recommend you check it out.我想把所有的功劳归功于Flutter 的作者 Nash Deep Dive: Gestures这是一篇很棒的文章,我强烈推荐你去看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM