如何获取点击位置 (GestureDetector)?
How do I get tap locations (GestureDetector)?
我正在尝试在我的 zoomable_images plugin but the GestureTapCallback 中实现双击缩放,但未提供点击位置信息。
理想情况下,偏移量将由回调返回。还有另一个 API 吗?
你可以提供一个GestureTapDownCallback
callback as the onTapDown
argument of the GestureDetector
constructor. The GestureTapDownCallback
takes a TapDownDetails
argument that includes the global position of the tap. You can then convert it to relative coordinates using BuildContext.findRenderObject
and RenderBox.globalToLocal
:
Offset _tapPosition;
void _handleTapDown(TapDownDetails details) {
final RenderBox referenceBox = context.findRenderObject();
setState(() {
_tapPosition = referenceBox.globalToLocal(details.globalPosition);
});
}
@override
Widget build(BuildContext context) {
return new GestureDetector(
/* ... */
onTapDown: _handleTapDown,
);
}
在您的 onDoubleTap
处理程序中,您可以参考 _tapPosition
以找出最近点击的位置。
有关此操作的示例,请参阅 InkWell
。
截至 [✓] Flutter (Channel stable, 2.5.3)
GestureDetector(
onTapDown: (details) {
var position = details.globalPosition;
// you can also check out details.localPosition;
if (position.dx < MediaQuery.of(context).size.width / 2){
// tap left side
} else {
// tap rigth size
}
},
child: SomeChildWidget(),
),
如果你想处理双击,你需要存储来自onDoubleTapDown
的点击位置,然后使用onDoubleTap
:
late Offset _doubleTapPosition;
...
onDoubleTap: () {
//do your stuff with _doubleTapPosition here
},
onDoubleTapDown: (details) {
final RenderBox box = context.findRenderObject() as RenderBox;
_doubleTapPosition = box.globalToLocal(details.globalPosition);
},
我正在尝试在我的 zoomable_images plugin but the GestureTapCallback 中实现双击缩放,但未提供点击位置信息。
理想情况下,偏移量将由回调返回。还有另一个 API 吗?
你可以提供一个GestureTapDownCallback
callback as the onTapDown
argument of the GestureDetector
constructor. The GestureTapDownCallback
takes a TapDownDetails
argument that includes the global position of the tap. You can then convert it to relative coordinates using BuildContext.findRenderObject
and RenderBox.globalToLocal
:
Offset _tapPosition;
void _handleTapDown(TapDownDetails details) {
final RenderBox referenceBox = context.findRenderObject();
setState(() {
_tapPosition = referenceBox.globalToLocal(details.globalPosition);
});
}
@override
Widget build(BuildContext context) {
return new GestureDetector(
/* ... */
onTapDown: _handleTapDown,
);
}
在您的 onDoubleTap
处理程序中,您可以参考 _tapPosition
以找出最近点击的位置。
有关此操作的示例,请参阅 InkWell
。
截至 [✓] Flutter (Channel stable, 2.5.3)
GestureDetector(
onTapDown: (details) {
var position = details.globalPosition;
// you can also check out details.localPosition;
if (position.dx < MediaQuery.of(context).size.width / 2){
// tap left side
} else {
// tap rigth size
}
},
child: SomeChildWidget(),
),
如果你想处理双击,你需要存储来自onDoubleTapDown
的点击位置,然后使用onDoubleTap
:
late Offset _doubleTapPosition;
...
onDoubleTap: () {
//do your stuff with _doubleTapPosition here
},
onDoubleTapDown: (details) {
final RenderBox box = context.findRenderObject() as RenderBox;
_doubleTapPosition = box.globalToLocal(details.globalPosition);
},