简体   繁体   English

Flutter 中 ListView.builder 上的 GestureDetector

[英]GestureDetector on ListView.builder in Flutter

I am trying to add a GestureDetector on my ListView widget which contains Images but it does not seem to work.我正在尝试在我的包含图像的 ListView 小部件上添加一个 GestureDetector,但它似乎不起作用。 A similar question has been asked about it before but that did not help either.之前有人问过类似的问题,但这也没有帮助。

Here is my code:这是我的代码:

ListView.builder(
          scrollDirection: Axis.horizontal,
          itemBuilder: (BuildContext ctx, int index) {
            return Container(
                width: 160,
                child: Card(
                 child: Wrap(
                  children: <Widget>[
                     GestureDetector( onTap: () => Print_on_layout(index),
                      child: Image.network(a[index], fit: BoxFit.fill,)
                     )
                  ],
                ),
              ),
            );
          },
          itemCount: a.length,
        ),

I suppose this has something to do with the wrap Widget, which might be messing with the space where the tap can be detected.我想这与 wrap Widget 有关,它可能会弄乱可以检测到点击的空间。 Try removing it.尝试删除它。 If that doesn't work, please tell us if you get an error.如果这不起作用,请告诉我们您是否收到错误。

Use gesturedetector as a parent of the container not on image.使用 gesturedetector 作为不在图像上的容器的父级。 as in the list you are only returning a single widgit so you should container with gesture detector.正如在列表中一样,您只返回一个 widgit,因此您应该使用手势检测器作为容器。 Try this code:试试这个代码:

ListView.builder(
            scrollDirection: Axis.horizontal,
              itemBuilder: (BuildContext ctx, int index) {
                return
                  GestureDetector(onTap: () => Print_on_layout(index),
    
                  child: Container(
                    width: 160,
                    child: Card(
                      child: Wrap(
                        children: <Widget>[
                          Image.network(a[index], fit: BoxFit.fill,)
                        ],
                      ),
                    ),
                  ),
                );
              },
              itemCount: a.length,
            ),

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

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