,我想得到x和y坐标,只要我的手指在容器内移动。
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final key = GlobalKey();
// ignore: non_constant_identifier_names
double X_Position = 0.00;
// ignore: non_constant_identifier_names
double Y_Position = 0.00;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: GestureDetector(
onPanStart: (details) {
RenderBox box = key.currentContext?.findRenderObject() as RenderBox;
Offset position = box.localToGlobal(Offset.zero);
setState(() {
X_Position = position.dx;
Y_Position = position.dy;
});
},
onPanUpdate: (details) {
RenderBox box = key.currentContext?.findRenderObject() as RenderBox;
Offset position = box.localToGlobal(Offset.zero);
setState(() {
X_Position = position.dx;
Y_Position = position.dy;
});
},
child: Container(
width: 500,
height: 500,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 2,
),
),
),
),
);
}
}在上面的代码,我想得到x和y坐标根据我的手指在容器内的移动。但是我得到了以下错误。你能帮忙吗?

发布于 2022-01-17 13:04:07
我更喜欢使用来自DragUpdateDetails的onPanUpdate。
body: GestureDetector(
onTap: () {},
onPanStart: (details) {
Offset position = details.localPosition;
setState(() {
X_Position = position.dx;
Y_Position = position.dy;
});
},
onPanUpdate: (DragUpdateDetails details) {
Offset position = details.localPosition;
setState(() {
X_Position = position.dx;
Y_Position = position.dy;
});
},https://stackoverflow.com/questions/70740862
复制相似问题