Is there a way to simulate a touch on a specific position of a widget or screen?
I want to make a touch or tap somewhere on a widget without making the user explicitly touch the screen at that point. Is there any way to do so?
I've checked SO answers, and some recommend using "integration tests" but which is not available to do on devices which aren't physically or in some way connected to the laptop (couldn't find a better wording).
I also attempted to do a hitTest wondering if it actually touches or taps the screen at that point, while interacting with the UI, but seems like it doesn't.
onPressed: () {
RenderObject? rb = context.findRenderObject();
if (rb is RenderBox){
final hit = BoxHitTestResult();
if (rb.hitTest(hit, position: Offset(300,500))){
print(hit.path);
}
}
},
Also, please understand the question right, as in many of the answers I've been reading regarding my query has been answered with "you don't need it, just call the function/method". I want to "simulate" a tap on a certain specified position of the screen, maybe according the X,Y axis as I attempted on the code.
An honest thank you for any directions.
If anything is lacking in my question, please let me know.
Edit: (The following is an update to what I'm finding)
I went diving into the Flutter codes. I found a few files:
Under flutter/src/gestures/hit_test.dart
I found void handleEvent(PointerEvent event, HitTestEntry entry);
. I assume it consumes a PointerEvent
. Checking further,
Under flutter/src/gestures/events.dart
I find class PointerDownEvent extends PointerEvent
and under flutter_test/src/test_pointer.dart
I find class TestPointer
Would any of these files give any light to what I want to do? I'm checking through.
Solution 1:
Thanks to @pskink , the following code works the best for me/ the situation. I've been able to make the taps as wanted. Thank you very much.
WidgetsBinding.instance!.handlePointerEvent(PointerDownEvent(pointer: 0, position: Offset(100, 100),)); WidgetsBinding.instance!.handlePointerEvent(PointerUpEvent(pointer: 0,position: Offset(100, 100),));