buildText function

Widget buildText(
  1. BuildContext context,
  2. Element text, {
  3. void onLinkTap(
    1. String url,
    2. String title
    )?,
  4. void onHashtagTap(
    1. String tag
    )?,
  5. Widget? imageBuilder(
    1. String src,
    2. String alt
    )?,
})

Implementation

Widget buildText(
  BuildContext context,
  md.Element text, {
void Function(String url, String title)? onLinkTap,
  void Function(String tag)? onHashtagTap,
  Widget? Function(String src, String alt)? imageBuilder,
}) {
  final theme = LdTheme.of(context, listen: true);
  final (paddingTop, paddingBottom) = switch (text.tag) {
    'h1' => (LdSize.l, LdSize.s),
    'h2' => (LdSize.l, LdSize.s),
    'h3' => (LdSize.m, LdSize.s),
    'h4' => (LdSize.s, LdSize.xs),
    'h5' => (LdSize.s, LdSize.xs),
    'h6' => (LdSize.xs, LdSize.s),
    _ => (null, null),
  };
  final padding = EdgeInsets.only(
    top: paddingTop != null ? theme.pad(size: paddingTop).top : 0,
    bottom: paddingBottom != null ? theme.pad(size: paddingBottom).bottom : 0,
  );
  return Padding(
    padding: padding,
    child: Text.rich(
      TextSpan(
        children: _inlineNodesToSpans(
          context,
          text.children ?? [],
          onLinkTap: onLinkTap,
          onHashtagTap: onHashtagTap,
          imageBuilder: imageBuilder,
        ),
        style: switch (text.tag) {
          'p' => ldBuildTextStyle(theme, LdTextType.paragraph, LdSize.m),
          'h1' => ldBuildTextStyle(theme, LdTextType.headline, LdSize.l),
          'h2' => ldBuildTextStyle(theme, LdTextType.headline, LdSize.m),
          'h3' => ldBuildTextStyle(theme, LdTextType.headline, LdSize.s),
          'h4' => ldBuildTextStyle(theme, LdTextType.headline, LdSize.xs),
          'h5' => ldBuildTextStyle(theme, LdTextType.headline, LdSize.xs),
          'h6' => ldBuildTextStyle(theme, LdTextType.headline, LdSize.xs),
          'em' || 'i' => const TextStyle(fontStyle: FontStyle.italic),
          _ => throw Exception('Invalid text type: ${text.tag}'),
        },
      ),
    ),
  );
}