Want to get the local position from the GestureDetector in flutter? You can use a RenderObject and then transform the global positions into a local one similar to this simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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), ), ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.