baselineWidgetSpan function

WidgetSpan baselineWidgetSpan(
  1. Widget child
)

Wraps child in a WidgetSpan that sits on the surrounding text baseline.

A widget in a paragraph defaults to being centred on the line box, which leaves it visibly off the baseline of the words around it. Use this from an InlineCodeBuilder when the design genuinely needs a widget:

inlineCodeBuilder: (context, code, style, codeStyle) =>
    baselineWidgetSpan(MyCodeChip(code: code, style: style)),

A WidgetSpan still cannot wrap across lines and is skipped by text selection — prefer returning a CodeTextSpan where the design allows.

Implementation

WidgetSpan baselineWidgetSpan(Widget child) {
  return WidgetSpan(
    alignment: PlaceholderAlignment.baseline,
    baseline: TextBaseline.alphabetic,
    // A paragraph scales an inline widget's box for you; a child that also
    // scales its own text is counted twice. See `GptMarkdownConfig.getRich`.
    child: MediaQuery.withNoTextScaling(child: child),
  );
}