Is there a way to simulate a touch on a specific position of a widget or screen?

201

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),));
Share:
201
Neo00
Author by

Neo00

Updated on January 03, 2023

Comments

  • Neo00
    Neo00 10 months

    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.

    • a_local_nobody
      a_local_nobody almost 2 years
      Secondly I want to ask, is there any "app development software" asking more than one question and asking for recommendations to software in one post is a great way to get your question closed :) stick to one question at a time, don't ask for recommendations to software
    • pskink
      pskink almost 2 years
      you mean a workaround like this: Timer(Duration(seconds: 3), () { WidgetsBinding.instance!.handlePointerEvent(PointerDownEvent‌​(pointer: 0, position: Offset(100, 100),)); WidgetsBinding.instance!.handlePointerEvent(PointerUpEvent(p‌​ointer: 0,position: Offset(100, 100),)); }); - of course both constructors should add more properties for it to work in most cases
    • Neo00
      Neo00 almost 2 years
      @a_local_nobody Right ^^ removed it!
    • Neo00
      Neo00 almost 2 years
      @pskink Thank you for the comment, I'm certainly looking more infos about what you said. Is it possible for you to provide a piece code that would do something as simple as, "maybe having two ink buttons, and if the first button is pressed, simulate a tap on a position on the second button, assuming it would show ink-animation(?) too?" It would certainly open me up more but nevertheless I'm happy with your comment as I'm looking more about it :) thank you. Also, what do you mean by both constructors should add more properties for it to work in most cases?
    • pskink
      pskink almost 2 years
      i gave you a code how to simulate a simple GestureDetector.onTap event - it is done by handling pointer down event followed by pointer up event, just add some GestureDetector and check it, "Also, what do you mean by both constructors should add more properties for it to work in most cases?" - i dont know maybe timeStamp is required? now it is set to this value
    • Neo00
      Neo00 almost 2 years
      @pskink Thank you very much. I actually didn't expect WidgetsBinding was an instance of.. all the widgets on screen probably, dont know the better wording. Expected it to be a child under some widget and thought of some possible complications, but it has worked great as expected. I've also written this as an answer to my question. Thank you very much.
    • pskink
      pskink almost 2 years
      sure, your welcome
    • Neo00
      Neo00 almost 2 years
      @pskink Im sorry if its not right to ping you, but I had a question related to the usage of WidgetsBinding that I've formed into a separate question at stackoverflow.com/questions/70902255/… . Thank you for reading ^^
  • Mohammad
    Mohammad almost 2 years
    If this workaround solved your problem, then mark it as accepted to help others know it
  • Neo00
    Neo00 almost 2 years
    @Mohammad SO says I can accept my own answer in "2" days, hence cant rn. I tried before too.