latexWidget function
Widget
latexWidget(
- BuildContext context,
- GptMarkdownConfig config, {
- required String tex,
- required bool inline,
Rendered maths, honouring GptMarkdownConfig.blockLatexBuilder, the deprecated GptMarkdownConfig.latexBuilder, GptMarkdownConfig.latexWorkaround and LatexStyle.
inline picks between an inline formula and a display block; only the
block form takes LatexStyle's padding, background and horizontal scroll.
Inline maths inside a paragraph goes through _latexSpan, which also
honours GptMarkdownConfig.inlineLatexBuilder.
Implementation
Widget latexWidget(
BuildContext context,
GptMarkdownConfig config, {
required String tex,
required bool inline,
}) {
final latexStyle =
(resolvedStyleSheet(context, config).latex ?? const LatexStyle()).resolve(
Theme.of(context).colorScheme,
);
final rendered = (config.latexWorkaround ?? (String tex) => tex)(tex);
final style = _latexTextStyle(config, latexStyle);
final blockBuilder = inline ? null : config.blockLatexBuilder;
// ignore: deprecated_member_use_from_same_package
final legacyBuilder = config.latexBuilder;
// Build below the boundary so custom builders and the math engine read the
// effective scaler, including when this formula is nested inside a block.
Widget maths = MarkdownTextScaling.wrap(
Builder(
builder: (mathContext) {
Widget buildDefault() => _defaultLatex(
mathContext,
config,
tex: rendered,
source: tex,
style: style,
inline: inline,
);
if (blockBuilder != null) {
return blockBuilder(
BlockLatexBuildDetails(
context: mathContext,
config: config,
style: style,
tex: rendered,
source: tex,
onTap: _latexTap(config, rendered, tex, inline),
buildDefault: buildDefault,
),
);
}
if (legacyBuilder != null) {
return legacyBuilder(mathContext, rendered, style, inline);
}
return buildDefault();
},
),
enabled: !inline && config.blocksRenderDirectly,
);
if (inline) {
return maths;
}
if (latexStyle.scrollBlockHorizontally ?? false) {
// Rendered maths cannot wrap, so a wide formula overflows a phone.
maths = SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: maths,
);
}
final background = latexStyle.backgroundColor;
if (background != null) {
final radius = latexStyle.borderRadius;
maths = DecoratedBox(
decoration: BoxDecoration(
color: background,
borderRadius: radius == null ? null : BorderRadius.all(radius),
),
child: maths,
);
}
final padding = latexStyle.padding;
if (padding != null) {
maths = Padding(padding: padding, child: maths);
}
return maths;
}