toWidget method

  1. @override
Widget toWidget(
  1. RenderContext context
)
override

Implementation

@override
Widget toWidget(RenderContext context) {
  StyledElement? node;
  List<Widget> widgets = <Widget>[];
  final rubySize =
      context.parser.style['rt']?.fontSize?.value ?? max(9.0, context.style.fontSize!.value / 2);
  final rubyYPos = rubySize + rubySize / 2;
  List<StyledElement> children = [];
  context.tree.children.forEachIndexed((index, element) {
    if (!((element is TextContentElement) &&
        (element.text ?? "").trim().isEmpty &&
        index > 0 &&
        index + 1 < context.tree.children.length &&
        context.tree.children[index - 1] is! TextContentElement &&
        context.tree.children[index + 1] is! TextContentElement)) {
      children.add(element);
    }
  });
  for (var c in children) {
    if (c.name == "rt" && node != null) {
      final widget = Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Container(
            alignment: Alignment.bottomCenter,
            child: Center(
              child: Transform(
                transform: Matrix4.translationValues(0, -(rubyYPos), 0),
                child: CssBoxWidget(
                  style: c.style,
                  child: Text(
                    c.element!.innerHtml,
                    style: c.style.generateTextStyle().copyWith(fontSize: rubySize),
                  ),
                ),
              ),
            ),
          ),
          CssBoxWidget(
            style: context.style,
            child: node is TextContentElement
                ? Text(
                    node.text?.trim() ?? "",
                    style: context.style.generateTextStyle(),
                  )
                : RichText(text: context.parser.parseTree(context, node)),
          ),
        ],
      );
      widgets.add(widget);
    } else {
      node = c;
    }
  }
  return Padding(
    padding: EdgeInsets.only(top: rubySize),
    child: Wrap(
      key: AnchorKey.of(context.parser.key, this),
      runSpacing: rubySize,
      children: widgets
          .map((e) => Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                textBaseline: TextBaseline.alphabetic,
                mainAxisSize: MainAxisSize.min,
                children: [e],
              ))
          .toList(),
    ),
  );
}