Flutter:从GestureDetector获取本地位置 [英] Flutter: Get the local position from the GestureDetector

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

问题描述

我正在使用GestureDetector通过以下方式获取此事件:

I'm using the GestureDetector to get this events using:

onHorizontalDragStart: _onDragStart,
onHorizontalDragUpdate: _onDragUpdate,

但是我怎样才能将全球地位转变为地方呢?被移动的孩子是一个容器.

But how can I transform the global position into local? The child that is moved is a Container.

推荐答案

您可以使用RenderObject,然后将全局位置转换为本地位置,类似于此简单示例:

You can use a RenderObject and then transform the global positions into a local one similar to this simple example:

import "package:flutter/material.dart";

class Test extends StatefulWidget {
  @override
  _TestState createState() => new _TestState();
}

class _TestState extends State<Test> {
  _onDragStart(BuildContext context, DragStartDetails start) {
    print(start.globalPosition.toString());
    RenderBox getBox = context.findRenderObject();
    var local = getBox.globalToLocal(start.globalPosition);
    print(local.dx.toString() + "|" + local.dy.toString());
  }

  _onDragUpdate(BuildContext context, DragUpdateDetails update) {
    //print(update.globalPosition.toString());
    RenderBox getBox = context.findRenderObject();
    var local = getBox.globalToLocal(update.globalPosition);
    //print(local.dx.toString() + "|" + local.dy.toString());
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new GestureDetector(
          child: new Text("Drag"),
          onHorizontalDragStart: (DragStartDetails start) =>
              _onDragStart(context, start),
          onHorizontalDragUpdate: (DragUpdateDetails update) =>
              _onDragUpdate(context, update),
        ),
      ),
    );
  }
}

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

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