mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-10-21 15:02:21 +02:00
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.
30 lines
1.1 KiB
Dart
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;
|
|
}
|