toJpeg static method

Future<Uint8List?> toJpeg(
  1. GlobalKey<State<StatefulWidget>> key, {
  2. double pixelRatio = 2.0,
  3. int quality = 90,
  4. Color backgroundColor = Colors.white,
})

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;
  }
}