如何使用 Flutter 检测用户在我的屏幕上点击的位置?
How do detect where a user tapped on my screen, with Flutter?
我想知道屏幕上发生点击的位置的 x/y 坐标。我找到了 GestureDetector,但它似乎没有告诉我点击的确切位置。
到目前为止,这是我的代码:
import 'package:flutter/material.dart';
void main() {
runApp(new MyWidget());
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: () => print('tapped!')
);
}
}
哪个注册了点击...但是我如何找到点击从哪里开始的?
观察 onTapDown 和 onTapUp,它们提供了更多细节供您使用。
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: () => print('tapped!'),
onTapDown: (TapDownDetails details) => _onTapDown(details),
onTapUp: (TapUpDetails details) => _onTapUp(details),
);
}
_onTapDown(TapDownDetails details) {
var x = details.globalPosition.dx;
var y = details.globalPosition.dy;
// or user the local position method to get the offset
print(details.localPosition);
print("tap down " + x.toString() + ", " + y.toString());
}
_onTapUp(TapUpDetails details) {
var x = details.globalPosition.dx;
var y = details.globalPosition.dy;
// or user the local position method to get the offset
print(details.localPosition);
print("tap up " + x.toString() + ", " + y.toString());
}
}
我想知道屏幕上发生点击的位置的 x/y 坐标。我找到了 GestureDetector,但它似乎没有告诉我点击的确切位置。
到目前为止,这是我的代码:
import 'package:flutter/material.dart';
void main() {
runApp(new MyWidget());
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: () => print('tapped!')
);
}
}
哪个注册了点击...但是我如何找到点击从哪里开始的?
观察 onTapDown 和 onTapUp,它们提供了更多细节供您使用。
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: () => print('tapped!'),
onTapDown: (TapDownDetails details) => _onTapDown(details),
onTapUp: (TapUpDetails details) => _onTapUp(details),
);
}
_onTapDown(TapDownDetails details) {
var x = details.globalPosition.dx;
var y = details.globalPosition.dy;
// or user the local position method to get the offset
print(details.localPosition);
print("tap down " + x.toString() + ", " + y.toString());
}
_onTapUp(TapUpDetails details) {
var x = details.globalPosition.dx;
var y = details.globalPosition.dy;
// or user the local position method to get the offset
print(details.localPosition);
print("tap up " + x.toString() + ", " + y.toString());
}
}