我正在使用GestureDetector使用以下方法获取此事件:
onHorizontalDragStart: _onDragStart, onHorizontalDragUpdate: _onDragUpdate,
但是,我如何才能将全球地位转变成本地人呢?被移动的孩子是一个容器。
您可以使用a RenderObject,然后将全局位置转换为本地位置,类似于此简单示例:
RenderObject
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), ), ), ); } }