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

368

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)));
}

Share:
368
RobDil
Author by

RobDil

Updated on January 01, 2023

Comments

  • RobDil
    RobDil over 1 year

    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?