flutter: is it possible to click at a certain x,y coordinate on the screen?

Viewed 737

I'm trying to find a way where I can automatically click on any (x, y) coordinate of the screen without user involvement. basically I need to develop this functionality for android in flutter. we may take inspiration from ADB, like this command allow us to tap on x,y coordinates.

adb shell input tap x y
2 Answers

After looking at flutter_test's source code, Here is what I came up with:

void tap(Offset pos){
    final result = HitTestResult();
    WidgetsBinding.instance.hitTest(result, pos);
    result.path.forEach((element) {
      element.target.handleEvent(
        PointerDownEvent(
            localPosition: pos,
            kind: PointerDeviceKind.touch),
        element,
      );
      element.target.handleEvent(
        PointerUpEvent(
            localPosition: pos,
            kind: PointerDeviceKind.touch),
        element,
      );
    });
}

Not sure if this is what you are looking for, but it is possible to use the adb functionality like you mentioned in Flutter. You just need to make sure the adb path is correct. For example:

String adbPath() {
  return join(envVars['ANDROID_SDK_ROOT'] ?? envVars['ANDROID_HOME'], 'platform-tools', Platform.isWindows ? 'adb.exe' : 'adb');
}


void tap(int x, int y) {
  Process.run(_adbPath(), ['shell', 'input', 'tap', x.toString(), y.toString()]);
}
Related