buildSvg function

Widget buildSvg(
  1. BuildContext context,
  2. OpenUiNode node
)

Svg(children, viewBox, width?, height?).

Implementation

Widget buildSvg(BuildContext context, OpenUiNode node) {
  final theme = node.theme;
  final ink = DefaultTextStyle.of(context).style.color ?? theme.ink;
  final fontSize =
      DefaultTextStyle.of(context).style.fontSize ??
      theme.fontSizeOf('Svg', 14);
  final viewport = MediaQuery.sizeOf(context);
  final box = (_ok(node.props['viewBox'], _viewBox) ?? '0 0 100 100')
      .trim()
      .split(RegExp(r'[\s,]+'))
      .map(double.parse)
      .toList();
  final viewBox = Rect.fromLTWH(box[0], box[1], box[2], box[3]);
  final scene = [
    for (final e in node.props.elements('children')) ?_shape(e, theme, ink),
  ];
  final width = OpenUiTheme.length(
    node.props['width'],
    fontSize: fontSize,
    viewport: viewport,
  );
  final height = OpenUiTheme.length(
    node.props['height'],
    fontSize: fontSize,
    viewport: viewport,
  ).px;
  final maxWidth = node.props['width'] == null && viewBox.width > 0
      ? math.max(280.0, viewBox.width * 1.5)
      : double.infinity;
  return Directionality(
    textDirection: TextDirection.ltr,
    child: LayoutBuilder(
      builder: (context, constraints) {
        final available = constraints.maxWidth.isFinite
            ? constraints.maxWidth
            : 320.0;
        final w = math.min(
          math.min(width.px ?? available * (width.fraction ?? 1), maxWidth),
          available,
        );
        final h =
            height ??
            (viewBox.width > 0 && viewBox.height > 0
                ? w * viewBox.height / viewBox.width
                : w);
        return Align(
          alignment: Alignment.topCenter,
          heightFactor: 1,
          child: Semantics(
            image: true,
            child: SizedBox(
              width: w,
              height: h,
              child: CustomPaint(painter: SvgScenePainter(viewBox, scene)),
            ),
          ),
        );
      },
    ),
  );
}