【问题标题】:How to get exact tap position of GestureDetector如何获得 GestureDetector 的准确点击位置
【发布时间】:2020-12-13 19:46:18
【问题描述】:

GestureDetector 总是为点击的任何图像返回相同的位置。

while (i<numImages) {
    GestureDetector(
    onTap:(){
      _onImageTapped(i);
    },

   child: FadeInImage(
      fit: BoxFit.cover,
      image: AssetImage(urls[i%5]),
       placeholder: AssetImage(urls[i%5]),
  
  )));
images.add(carImage);
i++;
 }
}

刷卡功能如下:

_onImageTapped(int position) {
        print('$position tapped');
   }

【问题讨论】:

  • 该代码不会按照您发布的方式编译或执行任何操作。请发布minimal reproducible example,发布您所做的事情、您期望发生的事情以及发生的事情。

标签: image flutter flutter-layout


【解决方案1】:

您想使用 GestureDetector 的 onTapUp 或 onTapDown 属性,这样您就可以获得具有位置详细信息的 TapDownDetails 或 TapUpDetails 等,使用如下:

GestureDetector g = new GestureDetector(
    onTapDown: (details) 
    {
      print(details.globalPosition);
      print(details.localPosition);
    },
  );

见:https://api.flutter.dev/flutter/gestures/TapUpDetails-class.html

【讨论】:

    【解决方案2】:

    我做到了

     int index=i;
    

       _onImageTapped(index);
    

    我得到了用户点击的图像索引。

    【讨论】:

      猜你喜欢
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多