dart - flutter :获取手势检测器的本地位置
问题描述
我无法使用平移更新在手势检测器小部件上获得真实的本地位置。
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
),
),
),
),
)
当我打印全局位置的偏移量 && 本地位置时,我对它们都有相同的值。结果它被绘制在我的 Container 小部件之外。为了获得当地职位,我有什么遗漏吗?
解决方案
我假设您正在使用这样的东西来获取本地位置:
RenderBox getBox = context.findRenderObject();
var local = getBox.globalToLocal(start.globalPosition);
这样做后您得到错误偏移量的原因与context
您用来查找本地位置的原因有关。
如果您正在使用整个小部件的上下文,那么基本上您所做的是计算整个小部件内的偏移量。IE
YourWidget <-- context
Center
Container
GestureDetector
...
假设屏幕看起来像这样:
1__________________________
| | <--- YourWidget
| 2_________ |
| | gesture | |
| | detector| |
| | 3. | |
| |_________| |
| |
|__________________________|
1 是整个小部件(和屏幕)的左上角,2 是手势检测器的左上角,3 是您点击的点。
通过使用 YourWidget 的上下文,您可以根据 1 和 3 之间的差异来计算点击的位置。如果 1 恰好在屏幕的左上角,则结果将匹配全局坐标。
通过使用手势检测器的上下文,您将改为测量 2 和 3 之间的距离,这将为您提供所需的偏移量。
有两种方法可以解决此问题 - 您可以将 GestureDetector 包装在Builder小部件中,或者您可以创建一个仅封装 GestureDetector 的新有状态/无状态小部件。我个人建议创建一个新的小部件。
推荐阅读
- python - 从破折号中的地图框获取当前缩放和中心
- c++ - txt文件中浮点数的最大值和最小值
- angular - 检查用户在 Angular Guard 中是否为空
- mediawiki-api - Mediawiki API:使用参数“pageid”从 Wikimedia Commons 查询图片
- swiftui - SwiftUI 屏幕安全区
- loops - 循环和连接表
- python - 如何使用对象数组从 json 创建 AWS Glue DynamicFrame
- android - 带参数的多模块 android 架构中的导航
- ansible - 将文件内容放在 Ansible 的电子邮件正文中
- r - 如何构建摘要数据框