I need to get position of draggable inside DragTarget.onMove
. But in DragTargetDetails.offset
we have some weird values.
In source code I see that it is set to avatar._lastOffset
which is globalPosition - dragStartPoint
. But I don't have access to any of those to be able to calculate global position or local position inside DragTarget.
Documentation sais that DragTargetDetails.offset
is 'The global position when the specific pointer event occurred on the draggable.' but in reality it is a global position minus start drugging point which seems to be useless, how I can use that value?
I prepared a simple example, where you can see offsets from Draggable.onDragUpdate
(which is real global position) and from DragTarget.onMove
.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Offset _dragUpdateOffset = Offset.zero;
Offset _moveOffset = Offset.zero;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Draggable(
data: "Test Data",
child: const Text("Drag me"),
feedback: const Material(child:Text("Draggable")),
childWhenDragging: Container(),
onDragUpdate: (details) {
setState(() {
_dragUpdateOffset = details.globalPosition;
});
},
),
),
body: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text("dragUpdate: ${_dragUpdateOffset.dx},${_dragUpdateOffset.dy}"),
const Text("vs"),
Text("onMove: ${_moveOffset.dx},${_moveOffset.dy}")
],),
Expanded(
child: Center(
child: DragTarget(
onMove: (details) {
_moveOffset = details.offset;
},
onWillAccept: (data) => true,
builder: (context, candidateData, rejectedData) => SizedBox(
width: 100,
height: 100,
child: Container(
child: const Text("DragTarget"),
decoration: const BoxDecoration(color: Colors.green),
),
),
),
),
),
],
),
);
}
}