How to tap all items of a list or a grid in a Flutter widget test?

Viewed 878

In my Flutter widget tests I want to tap all (or first n) items in a list or grid view.

I already came up with a solution and although it seems to work, it looks overly complicated to find the tap target again by key:

for (final element in find.byType(ListTile).evaluate()) {
  await tester.tap(find.byKey(element.widget.key!));
}

Is there a more elegant way to do it?

1 Answers

You can tap at a certain location on the widget: e.g. tapping at center of the widget

for (final element in find.byType(ListTile).evaluate()) {
  await tester.tapAt(tester.getCenter(find.byWidget(element.widget)));
}

Related