如何通过Flutter检测用户在屏幕上的位置?

时间:2017-06-14 03:09:33

标签: dart 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!')
    );
  }
}

哪个会注册点击...但我如何找到从哪里点击?

1 个答案:

答案 0 :(得分:10)

观察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;
    print("tap down " + x.toString() + ", " + y.toString());
  }

  _onTapUp(TapUpDetails details) {
    var x = details.globalPosition.dx;
    var y = details.globalPosition.dy;
    print("tap up " + x.toString() + ", " + y.toString());
  }
}