toJpeg static method
Capture the widget attached to key as JPEG bytes.
Flutter can encode PNG natively, but JPEG requires raw pixels plus a
Dart encoder. Transparent pixels are flattened against backgroundColor.
Implementation
static Future<Uint8List?> toJpeg(
GlobalKey key, {
double pixelRatio = 2.0,
int quality = 90,
Color backgroundColor = Colors.white,
}) async {
if (!_isValidPixelRatio(pixelRatio)) return null;
try {
final image = await toImage(key, pixelRatio: pixelRatio);
if (image == null) return null;
return imageToJpeg(
image,
quality: quality,
backgroundColor: backgroundColor,
);
} catch (e) {
debugPrint('ChartExporter.toJpeg error: $e');
return null;
}
}