Flutter中如何获取GestureDetector的x轴和y轴坐标

发布于 2025-01-10 16:46:56 字数 123 浏览 0 评论 0原文

目前,我正在构建一个 Pinterest 克隆应用程序,我真的需要您的帮助。实际上,我花了将近一个小时寻找答案,但给出的解决方案都不起作用。所以我需要的只是确定用户通过手势检测器按下的位置的 x 轴和 y 轴。谁能帮帮我,拜托...

Currently, I am building a Pinterest clone app and I really need your help. Actually, I've spent almost an hour searching for the answer but none of the solutions given worked. So what I need is just to determine the x and y axes of the place where the user pressed through the Gesture detector. Can anyone help me, please...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

能怎样 2025-01-17 16:46:56

我相信您不应该在这里使用 GestureDetector ,而是使用 Listener 小部件。作为侦听器,您可以为您提供用户按下位置的全局位置,或按下小部件开始位置的相对位置

这是一些示例代码:

import 'package:flutter/material.dart';

class GetPositionExample extends StatelessWidget {
  const GetPositionExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Listener(

      // This will report a PointerDownEvent whenever the user presses the screen.
      // If you want updates as the user moves their finger across the screen,
      // use onPointerMove instead.
      onPointerDown: (PointerDownEvent event) {
        // Global screen position.
        print("Global position x:${event.position.dx}, y:${event.position.dy}");
        // Position relative to where this widget starts.
        print("Relative position: x:${event.localPosition.dx}, y:${event.localPosition.dy}");
      },

      // You should also be aware of these Listener methods,
      // if any of them is more suited for your use-case:

      // onPointerMove: (PointerMoveEvent event) {},
      // onPointerUp: (PointerUpEvent event) {},
      // onPointerCancel: (PointerCancelEvent event) {},
      // onPointerHover: (PointerHoverEvent event) {},
      // onPointerSignal: (PointerSignalEvent event) {},

      child: Container(
        color: Colors.blue,
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height/2,
      ),
    );
  }
}

// Some code to run the above example
class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Scaffold(body: Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: const [
        GetPositionExample(),
      ],
    )));
  }
}

void main() => runApp(const App());

I believe you should not be using a GestureDetector here but a Listener widget instead. As a listener can give you the global position of where the user presser, or a relative position to where the widget pressed starts.

Here is some example code:

import 'package:flutter/material.dart';

class GetPositionExample extends StatelessWidget {
  const GetPositionExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Listener(

      // This will report a PointerDownEvent whenever the user presses the screen.
      // If you want updates as the user moves their finger across the screen,
      // use onPointerMove instead.
      onPointerDown: (PointerDownEvent event) {
        // Global screen position.
        print("Global position x:${event.position.dx}, y:${event.position.dy}");
        // Position relative to where this widget starts.
        print("Relative position: x:${event.localPosition.dx}, y:${event.localPosition.dy}");
      },

      // You should also be aware of these Listener methods,
      // if any of them is more suited for your use-case:

      // onPointerMove: (PointerMoveEvent event) {},
      // onPointerUp: (PointerUpEvent event) {},
      // onPointerCancel: (PointerCancelEvent event) {},
      // onPointerHover: (PointerHoverEvent event) {},
      // onPointerSignal: (PointerSignalEvent event) {},

      child: Container(
        color: Colors.blue,
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height/2,
      ),
    );
  }
}

// Some code to run the above example
class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Scaffold(body: Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: const [
        GetPositionExample(),
      ],
    )));
  }
}

void main() => runApp(const App());

好菇凉咱不稀罕他 2025-01-17 16:46:56

你的问题不清楚。 确定手势检测器的 x 和 y 轴是什么意思

如果您需要知道用户是否向右、向左或向下滑动,这里是一个示例

  GestureDetector(
          onVerticalDragUpdate: (details) {
    int sensitivity = 8;
            if (details.delta.dy > sensitivity) {
              // Down Swipe
              
            } else if (details.delta.dy < -sensitivity) {
              // Up Swipe
             
            }
          },
          onHorizontalDragUpdate: (details) {
            // Note: Sensitivity is integer used when you don't want to mess up vertical drag
            int sensitivity = 8;
            if (details.delta.dx > sensitivity) {
              // Right Swipe
             
            } else if (details.delta.dx < -sensitivity) {
             
              //Left Swipe
            }
          },


Your question is unclear. What do you mean by determine the x and y axes of the Gesture detector

If all you need is know if user swipped right, left or down here is an example

  GestureDetector(
          onVerticalDragUpdate: (details) {
    int sensitivity = 8;
            if (details.delta.dy > sensitivity) {
              // Down Swipe
              
            } else if (details.delta.dy < -sensitivity) {
              // Up Swipe
             
            }
          },
          onHorizontalDragUpdate: (details) {
            // Note: Sensitivity is integer used when you don't want to mess up vertical drag
            int sensitivity = 8;
            if (details.delta.dx > sensitivity) {
              // Right Swipe
             
            } else if (details.delta.dx < -sensitivity) {
             
              //Left Swipe
            }
          },


ι不睡觉的鱼゛ 2025-01-17 16:46:56

您可以根据需要使用 rect_getter 。并实现如下:

    var globalKey = RectGetter.createGlobalKey();
  Offset _tapPosition;
  var dx;   //for x-axis
  var dy;   //for y-axis

RectGetter(
              key: globalKey,
              child: new Expanded(
                child: new InkWell(
                  onTap:  _handleTapDown,
                  onTapDown: _handleTapDown,
                  child: Center(
                    child: Container(
                      child: Utility.imageFromBase64String(imagename),
                      //  backgroundColor: const Color(0xFF20283e),
                    ),
                  ),
                ),
              ),
            ),

_handleTapDown函数

void _handleTapDown(TapDownDetails details) {
final RenderBox referenceBox = context.findRenderObject();
setState(() {
  _tapPosition = referenceBox.globalToLocal(details.globalPosition);
  dx = double.parse(_tapPosition.dx.toStringAsFixed(2));
  dy = double.parse(_tapPosition.dy.toStringAsFixed(2));
  print(_tapPosition);
 
});
}

You can use rect_getter for your desire. And implement as follows:

    var globalKey = RectGetter.createGlobalKey();
  Offset _tapPosition;
  var dx;   //for x-axis
  var dy;   //for y-axis

RectGetter(
              key: globalKey,
              child: new Expanded(
                child: new InkWell(
                  onTap:  _handleTapDown,
                  onTapDown: _handleTapDown,
                  child: Center(
                    child: Container(
                      child: Utility.imageFromBase64String(imagename),
                      //  backgroundColor: const Color(0xFF20283e),
                    ),
                  ),
                ),
              ),
            ),

_handleTapDown function

void _handleTapDown(TapDownDetails details) {
final RenderBox referenceBox = context.findRenderObject();
setState(() {
  _tapPosition = referenceBox.globalToLocal(details.globalPosition);
  dx = double.parse(_tapPosition.dx.toStringAsFixed(2));
  dy = double.parse(_tapPosition.dy.toStringAsFixed(2));
  print(_tapPosition);
 
});
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文