0

Is there a way to get absolute coordinates of element on the screen in flutter? I'm finding element by key, but I also need it's coordinates (or at leas center). I found that flutter driver has some methods for this, like:

driver.getTopLeft(find.byValueKey('some_element_key'));
driver.getBottomRight(find.byValueKey('some_element_key'));

But it looks like that returned values are relative to parent element.

driver.getRenderObjectDiagnostics(find.byValueKey('accounts'));
driver.getWidgetDiagnostics(find.byValueKey('accounts'));

Both of these methods return the same coordinates.

A. Makarevich
  • 365
  • 1
  • 3
  • 18

1 Answers1

0

You will need to do a custom data handler function or custom command that will do the following in app:

        // grab the top most widget for your app, maybe first() will work instead of firstWhere()
        var element = collectAllElementsFrom(
                WidgetsBinding.instance.renderViewElement,
                skipOffstage: false)
            .firstWhere((element) =>
                element.widget == <some_filtering_here>);
        // get render box
        final RenderBox box = element.renderObject as RenderBox;
        // get global coordinates
        Offset centerOffset = box.localToGlobal(box.size.center(Offset.zero));

More about custom finders and custom commands here:

https://arturkorobeynyk.medium.com/using-commands-to-query-flutter-driver-for-widgets-states-and-properties-c7a5a18fea42

https://arturkorobeynyk.medium.com/using-gestures-within-data-handler-function-of-flutter-driver-extension-6b4347be4b7c

Artur Korobeynyk
  • 955
  • 1
  • 9
  • 24