iconWidgetToBytes static method

Future<Uint8List?> iconWidgetToBytes(
  1. Widget widget, {
  2. double size = 24.0,
})

Renders a widget representing an icon and converts it into PNG bytes. Useful when visual styling or layout beyond a simple glyph is needed.

Implementation

static Future<Uint8List?> iconWidgetToBytes(
  Widget widget, {
  double size = 24.0,
}) async {
  try {
    if (size <= 0) {
      debugPrint('Invalid size: $size');
      return null;
    }

    final wrappedWidget = MediaQuery(
      data: const MediaQueryData(),
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: SizedBox(
          width: size,
          height: size,
          child: Center(child: widget),
        ),
      ),
    );

    return await _renderWidgetToBytes(wrappedWidget, Size(size, size));
  } catch (e) {
    debugPrint('Error converting icon widget to bytes: $e');
    return null;
  }
}