toPngFile static method

Future<String> toPngFile({
  1. required Widget widget,
  2. required Size size,
  3. double pixelRatio = 2.0,
  4. Duration waitBeforeCapture = Duration.zero,
  5. ThemeData? theme,
  6. String? filePath,
})

filePath is where to write the PNG. If null, writes under the OS temp root; stale files accumulate until cleaned up by the caller.

Implementation

static Future<String> toPngFile({
  required Widget widget,
  required Size size,
  double pixelRatio = 2.0,
  Duration waitBeforeCapture = Duration.zero,
  ThemeData? theme,
  String? filePath,
}) async {
  final bytes = await toPng(
    widget: widget,
    size: size,
    pixelRatio: pixelRatio,
    waitBeforeCapture: waitBeforeCapture,
    theme: theme,
  );
  final path = filePath ?? _defaultPngPath();
  final file = File(path);
  await file.parent.create(recursive: true);
  await file.writeAsBytes(bytes, flush: true);
  return file.path;
}