How to Calculate the Coordinates of touch down and up in Flutter? code example

Example: get coordinates of touch in flutter

//Use GestureDetector
 void onTapDown(BuildContext context, TapDownDetails details) {
    print('${details.globalPosition}');
    final RenderBox box = context.findRenderObject();
    final Offset localOffset = box.globalToLocal(details.globalPosition);
    setState(() {
      posx = localOffset.dx;
      posy = localOffset.dy;
    });
    _showPopupMenu();
  }

  _showPopupMenu() {
    showMenu<String>(
      context: context,
      position: RelativeRect.fromLTRB(0.0, posy, 0.0,
          0.0), //position where you want to show the menu on screen
      items: [
        PopupMenuItem<String>(
            child: Text(
              'Edit Details',
              style: Theme.of(context).textTheme.caption,
            ),
            value: '1'),
        PopupMenuItem<String>(
            child: Text(
              'Add/Edit Photo',
              style: Theme.of(context).textTheme.caption,
            ),
            value: '2'),
        PopupMenuItem<String>(
            child: Text(
              'Add/Edit QR code',
              style: Theme.of(context).textTheme.caption,
            ),
            value: '3'),
      ],
      elevation: 8.0,
    ).then<void>((String itemSelected) {
      if (itemSelected == null) return;

      if (itemSelected == "1") {
        //code here
      } else if (itemSelected == "2") {
        //code here
      } else {
        //scan and update QR Barcode code here
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (_) => UpdateMachineCode(),
          ),
        );
      }
    });
  }

Tags:

Dart Example