captureScreenshot function

Future<ScreenshotResult> captureScreenshot(
  1. ScreenshotInput input
)

Captures a screenshot of the running Flutter app.

Never throws (except AppDiedException which the dispatcher handles). All other error conditions are represented as ScreenshotFailed.

Implementation

Future<ScreenshotResult> captureScreenshot(ScreenshotInput input) async {
  final warnings = <String>[];
  final output = input.output;

  // Read session state.
  final platformInfo = readPlatformInfo();
  final deviceId = readDevice();

  // Dispatch to the correct capture backend.
  final String? captureError;
  if (platformInfo != null) {
    captureError = await _dispatchScreenshot(
      platform: platformInfo.platform,
      emulator: platformInfo.emulator,
      deviceId: deviceId,
      output: output,
      warnings: warnings,
    );
  } else {
    captureError = await _legacyCapture(output, warnings);
  }

  if (captureError != null) {
    return ScreenshotFailed(message: captureError, warnings: warnings);
  }

  final file = File(output);
  if (!file.existsSync()) {
    return ScreenshotFailed(
      message: 'Screenshot file not created',
      warnings: warnings,
    );
  }

  if (!input.fullResolution) {
    final resizeError = await _resizeToMaxDimension(output);
    if (resizeError != null) {
      return ScreenshotFailed(message: resizeError, warnings: warnings);
    }
  }

  final sizeBytes = file.lengthSync();
  return ScreenshotSaved(
    path: output,
    sizeBytes: sizeBytes,
    warnings: warnings,
  );
}