Flutter:如何区分长按和长按并拖动

gorkyyrv  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(156)

如何区分长按与长按结合拖动(先长按,然后拖动,中间不抬起手指)?我特别好奇如何用手势检测器来做这个?

9wbgstp7

9wbgstp71#

当你点击其中一个时,你会在另一个不同的位置抽出你的手。

double? xStart;
      double? yStart;
    
      @override
      Widget build(BuildContext context) {
        return GestureDetector(
          onLongPressStart: (details) {
            xStart = details.globalPosition.dx;
            yStart = details.globalPosition.dy;
          },
          onLongPressEnd: (details) {
            double xCurrent = details.globalPosition.dx;
            double yCurrent = details.globalPosition.dy;
            double threshold = 10.0;
    
            if ((xStart! - xCurrent).abs() < threshold &&
                (yStart! - yCurrent).abs() < threshold) {
              print('Long press detected');
            } else {
              print('Drag detected');
            }
          },
          child:
              Center(child: Container(color: Colors.blue, width: 300, height: 300)),
        );

相关问题