waitForScreen method
Wait until a specific screen is visible in the widget tree. Polls every 500ms until the screen appears or times out.
Implementation
Future<bool> waitForScreen(String screenWidgetName,
{int timeoutSeconds = 30}) async {
print('\n⏳ Waiting for $screenWidgetName to appear...');
final deadline = DateTime.now().add(Duration(seconds: timeoutSeconds));
while (DateTime.now().isBefore(deadline)) {
final tree = await captureWidgetTree(silent: true);
final currentScreen = _findCurrentScreen(tree);
if (currentScreen.contains(screenWidgetName) ||
_treeContains(tree, screenWidgetName)) {
print(' ✅ $screenWidgetName detected');
return true;
}
stdout.write(' ⏳ Current screen: $currentScreen — waiting...\r');
await Future.delayed(const Duration(milliseconds: 500));
}
print('\n ⚠️ Timed out waiting for $screenWidgetName');
return false;
}