span static method

TextSpan span(
  1. BuildContext context,
  2. UEpubBlock block,
  3. UEpubTypography typography,
  4. UDocColorMode mode, {
  5. int start = 0,
  6. int end = -1,
  7. void onLink(
    1. String href
    )?,
})

Styled text of a block.

Implementation

static TextSpan span(
  BuildContext context,
  UEpubBlock block,
  UEpubTypography typography,
  UDocColorMode mode, {
  int start = 0,
  int end = -1,
  void Function(String href)? onLink,
}) {
  final double base = typography.baseFontSize * (block.kind == UEpubBlockKind.heading ? headingScale(block.level) : 1);
  final Color text = foreground(context, mode);
  final Color link = Theme.of(context).colorScheme.primary;
  final String? family = typography.fontFamily;
  final List<UEpubSpan> spans = end < 0 && start == 0 ? block.spans : block.sliceSpans(start, end < 0 ? block.text.length : end);
  final List<InlineSpan> children = <InlineSpan>[];
  for (final UEpubSpan span in spans) {
    final String? href = span.href;
    final TextStyle style = TextStyle(
      fontSize: base * span.sizeFactor * (span.superscript || span.subscript ? 0.72 : 1),
      fontWeight: span.bold || block.kind == UEpubBlockKind.heading ? FontWeight.w700 : FontWeight.w400,
      fontStyle: span.italic ? FontStyle.italic : FontStyle.normal,
      decoration: href != null || span.underline ? TextDecoration.underline : (span.strike ? TextDecoration.lineThrough : TextDecoration.none),
      decorationColor: href != null ? link.withValues(alpha: 0.5) : null,
      color: href != null ? link : (mode == UDocColorMode.normal ? (span.color ?? text) : text),
      backgroundColor: mode == UDocColorMode.normal ? span.background : null,
      fontFamily: span.monospace ? "monospace" : null,
      fontFeatures: span.superscript ? const <FontFeature>[FontFeature.superscripts()] : (span.subscript ? const <FontFeature>[FontFeature.subscripts()] : null),
    );
    children.add(
      TextSpan(
        text: span.text,
        style: style,
        recognizer: href == null || onLink == null ? null : (TapGestureRecognizer()..onTap = () => onLink(href)),
      ),
    );
  }
  return TextSpan(
    style: DefaultTextStyle.of(context).style.merge(
      TextStyle(
        fontSize: base,
        color: text,
        height: typography.lineHeight,
        letterSpacing: typography.letterSpacing,
        wordSpacing: typography.wordSpacing,
        fontFamily: family,
        package: family == "Vazir" ? "u" : null,
        fontWeight: block.kind == UEpubBlockKind.heading ? FontWeight.w700 : FontWeight.w400,
      ),
    ),
    children: children,
  );
}