isVisibleAt method
Returns true if this finder finds at least 1 visible widget
at the given alignment.
Provide alignment to fine tune the visibility check by calling
Finder.hitTestable at this alignment of the Widget.
This might be helpful in case the tested Widget is or contains a Row
or a Column. The default Alignment.center might always be the best
choice as the following example demonstrates:
/// This [Widget] will only be found when calling
///await $(Foo).waitUntilVisible(alignment: Alignment.topCenter)
class Foo extends StatelessWidget {
Foo({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Column(
children: [
Text(
'Foo',
),
SizedBox(height: 48),
Text('Bar'),
],
)
}
}
As there is an empty SizedBox in the center of the Column,
calling await $(Foo).waitUntilVisible() will fail
as the underlying Finder.hitTestable will not find any
hit-testable widget. Changing the alignment parameter
to Alignment.topCenter and calling
await $(Foo).waitUntilVisible(alignment: Alignment.topCenter)
will make the test pass as the underlying Finder.hitTestable
will find the Text widget at the top of the Column.
Implementation
bool isVisibleAt({Alignment alignment = Alignment.center}) {
final isVisible = hitTestable(at: alignment).evaluate().isNotEmpty;
if (isVisible == true) {
assert(
exists == true,
'visible returned true, but exists returned false',
);
}
return isVisible;
}