我需要在里面找到可拖动的位置DragTarget.onMove
。但是DragTargetDetails.offset
我们有一些奇怪的价值观。在源代码中,我看到它设置为avatar._lastOffset
which is globalPosition - dragStartPoint
。但是我无权访问其中任何一个来计算 DragTarget 中的全局位置或本地位置。文档说DragTargetDetails.offset
是“在可拖动对象上发生特定指针事件时的全局位置。” 但实际上它是一个全局位置减去开始用药点,这似乎没用,我该如何使用该值?
我准备了一个简单的示例,您可以在其中看到偏移量Draggable.onDragUpdate
(这是真正的全局位置)和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),
),
),
),
),
),
],
),
);
}
}