flutter:获取手势检测器的本地位置 [英] flutter : Get Local position of Gesture Detector

查看:191
本文介绍了flutter:获取手势检测器的本地位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用平移更新来获取手势检测器小部件上的实际位置时遇到问题



这是我的示例代码

 新中心(
儿童新容器(
高度:150.0,
宽度:150.0,
装饰:新BoxDecoration(
borderRadius:新的BorderRadius.circular(5.0),
颜色:Colors.white,
),
子级:new GestureDetector(
onPanUpdate:(details)=> onPanUpdate (上下文,详细信息),
子级:新的CustomPaint(
画家:新的MyPainter(
分:积分,
极限底部):高度-125.0,
极限顶部:10.0,
限制左:5.0,
限制右:宽度-55.0
),
),
),
),

当我打印全球排名的偏移量&&当地的位置,我对他们两个都有相同的价值。结果是它被绘制在我的Container小部件外部。有什么我想得到的本地职位吗?

解决方案

我假设您正在使用类似的方法来获取本地位置:

  RenderBox getBox = context.findRenderObject(); 
var local = getBox.globalToLocal(start.globalPosition);

这样做后得到错误补偿的原因与<$ c有关$ c>上下文,您用来查找本地位置。



如果您使用的是整个小部件的上下文,本质上就是这样做是计算整个小部件内的偏移量。即

  YourWidget<-上下文
中心
容器
GestureDetector
...

假设屏幕看起来像这样:

  1 __________________________ 
| | < --- YourWidget
| 2_________ |
| |手势| |
| |检测器| |
| | 3. | |
| | _________ | |
| |
| __________________________ |

其中1是整体小部件(和屏幕)的左上角,2是左上角



通过使用YourWidget的上下文,您可以根据以下两者之间的差异计算点击位置:



1和3。如果1恰好位于屏幕的左上方,则结果将与全局坐标匹配。



通过使用手势检测器的上下文,您可以而是测量2和3之间的距离,这将为您提供所需的偏移量。



有两种方法可以解决此问题-您可以将GestureDetector包装在 Builder 小部件,也可以创建一个新的有状态/无状态小部件仅封装GestureDetector。我个人建议创建一个新的小部件。


i have problems to get real local position on gesture detector widget using pan update

here is my sample code

new Center(    
  child new Container(
       height: 150.0,
       width: 150.0,
       decoration: new BoxDecoration(
         borderRadius: new BorderRadius.circular(5.0),
         color: Colors.white,
      ),
      child: new GestureDetector(
      onPanUpdate: (details) => onPanUpdate(context, details),
      child: new CustomPaint(
         painter: new MyPainter(
             points: points,
             limitBottom:height - 125.0,
             limitTop: 10.0,
             limitLeft: 5.0,
             limitRight: width - 55.0
         ),
       ),
    ),
  ),  
)

when i print the the Offset of global position && local position, i've got same value for both of them. in Result it was painted outside of my Container widget. is there anything i missed to get local position.

解决方案

I assume you're using something like this to get the local position:

RenderBox getBox = context.findRenderObject();
var local = getBox.globalToLocal(start.globalPosition);

The reason you'd be getting the wrong offset after doing that has to do with the context you're using to find the local position.

If you're using the overall widget's context, essentially what you're doing is calculating the offset within the overall widget. i.e.

YourWidget <-- context
  Center
   Container
     GestureDetector
     ...

Let's say that the screen looks something like this:

1__________________________   
|                          |  <--- YourWidget
|       2_________         |
|       | gesture |        |
|       | detector|        |
|       |   3.    |        |
|       |_________|        |
|                          |
|__________________________|

With 1 being the top left of your overall widget (and the screen), 2 being the top left of your gesture detector, and 3 being the point where you tap.

By using the context of YourWidget, you're calculating the position of the tap based on the difference between 1 and 3. If 1 happens to be at the top left of the screen, the result will match the global coordinates.

By using the context for your gesture detector, you'd instead be measuring the distance between 2 and 3, which will give you the offset you want.

There's two ways to fix this - either you can wrap your GestureDetector in a Builder widget, or you can create a new Stateful/Stateless widget that encapsulates just the GestureDetector. I'd personally recommend creating a new widget.

这篇关于flutter:获取手势检测器的本地位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆