Files
SpotiFLAC-Mobile/lib/utils/widget_capture.dart
T
zarzet b10370e4fc refactor(share): share the widget capture helper
The lyrics card and the rewind slide both rendered a RepaintBoundary to a
temp PNG. captureBoundaryToPngFile does that; pixel ratio, file name, frame
waiting and share parameters stay with each caller.
2026-10-19 00:48:31 +07:00

30 lines
1.1 KiB
Dart

import 'dart:io';
import 'dart:ui' show ImageByteFormat;
import 'package:flutter/rendering.dart' show RenderRepaintBoundary;
import 'package:flutter/widgets.dart';
import 'package:path_provider/path_provider.dart';
/// Renders the [RepaintBoundary] behind [boundaryKey] to a PNG named
/// [fileName] in the temporary directory.
///
/// Returns null when the boundary is not laid out or the image cannot be
/// encoded, so each caller decides how to report that.
Future<File?> captureBoundaryToPngFile(
GlobalKey boundaryKey, {
required double pixelRatio,
required String fileName,
}) async {
final boundary =
boundaryKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
if (boundary == null) return null;
final image = await boundary.toImage(pixelRatio: pixelRatio);
final bytes = await image.toByteData(format: ImageByteFormat.png);
image.dispose();
if (bytes == null) return null;
final directory = await getTemporaryDirectory();
final file = File('${directory.path}/$fileName');
await file.writeAsBytes(bytes.buffer.asUint8List(), flush: true);
return file;
}