getTextSpan static method

InlineSpan getTextSpan(
  1. MsTextSpan textSpan,
  2. List<Styles> stylesList
)

Function for processing text spans

Implementation

static InlineSpan getTextSpan(MsTextSpan textSpan, List<Styles> stylesList) {
  TextStyle textStyle = const TextStyle(inherit: false);
  String tempSpanText = textSpan.text;

  if (textSpan.fontSize != 0) {
    textStyle = textStyle.copyWith(fontSize: textSpan.fontSize.toDouble() / 2);
  }
  if (textSpan.formats.contains("italic")) {
    textStyle = textStyle.copyWith(fontStyle: FontStyle.italic);
  }
  if (textSpan.formats.contains("bold")) {
    textStyle = textStyle.copyWith(fontWeight: FontWeight.bold);
  }
  if (textSpan.formats.contains("single-underline")) {
    textStyle = textStyle.copyWith(decoration: TextDecoration.underline);
  }
  if (textSpan.formats.contains("double-underline")) {
    textStyle = textStyle.copyWith(decoration: TextDecoration.underline, decorationStyle: TextDecorationStyle.double);
  }
  if (textSpan.formats.contains("strike")) {
    textStyle = textStyle.copyWith(decoration: TextDecoration.lineThrough);
  }
  if (textSpan.textColor.isNotEmpty && textSpan.textColor != "auto") {
    Color selectedColor = Color(int.parse("FF${textSpan.textColor}", radix: 16));

    textStyle = textStyle.copyWith(color: selectedColor);
  }
  if (textSpan.fonts.isNotEmpty) {
    textStyle = textStyle.copyWith(fontFamily: textSpan.fonts["ascii"]);
  }
  if (textSpan.highlightColor.isNotEmpty) {
    textStyle = textStyle.copyWith(backgroundColor: getColorFromName(textSpan.highlightColor));
  }
  if (textSpan.shadingColor.isNotEmpty) {
    Color shadingColor = Color(int.parse("FF${textSpan.shadingColor}", radix: 16));
    textStyle = textStyle.copyWith(backgroundColor: shadingColor);
  }

  if (textSpan.style.isNotEmpty) {
    Styles? textStyles = stylesList.firstWhereOrNull((style) {
      return style.styleId == textSpan.style;
    });
    if (textStyles != null) {
      if (textStyles.fontSize != 0) {
        textStyle = textStyle.copyWith(fontSize: textStyles.fontSize.toDouble() / 2);
      }
      if (textStyles.formats.contains("italic")) {
        textStyle = textStyle.copyWith(fontStyle: FontStyle.italic);
      }
      if (textStyles.formats.contains("bold")) {
        textStyle = textStyle.copyWith(fontWeight: FontWeight.bold);
      }
      if (textStyles.formats.contains("single-underline")) {
        textStyle = textStyle.copyWith(decoration: TextDecoration.underline);
      }
      if (textStyles.formats.contains("double-underline")) {
        textStyle = textStyle.copyWith(decoration: TextDecoration.underline, decorationStyle: TextDecorationStyle.double);
      }
      if (textStyles.formats.contains("strike")) {
        textStyle = textStyle.copyWith(decoration: TextDecoration.lineThrough);
      }
      if (textStyles.formats.contains("subscript")) {
        textSpan.formats.add("subscript");
      }
      if (textStyles.formats.contains("superscript")) {
        textSpan.formats.add("superscript");
      }
      if (textStyles.textColor != null && textStyles.textColor != "auto") {
        Color selectedColor = Color(int.parse("FF${textStyles.textColor!}", radix: 16));

        textStyle = textStyle.copyWith(color: selectedColor);
      } else {
        textStyle = textStyle.copyWith(color: Colors.black);
      }
      if (textStyles.fonts.isNotEmpty) {
        textStyle = textStyle.copyWith(fontFamily: textStyles.fonts["ascii"]);
      }
    }
  }
  if (textSpan.formats.contains("subscript") || textSpan.formats.contains("superscript")) {
    double fontSize = textStyle.fontSize ?? 22;
    textStyle = textStyle.copyWith(fontSize: fontSize / 3).copyWith(color: Colors.grey);
    if (textSpan.formats.contains("subscript")) {
      return WidgetSpan(
        child: Transform.translate(
          offset: const Offset(0.0, 1.0),
          child: Text(
            tempSpanText,
            style: textStyle,
          ),
        ),
      );
    } else {
      return WidgetSpan(
        child: Transform.translate(
          offset: const Offset(0.0, -3.0),
          child: Text(
            tempSpanText,
            style: textStyle,
          ),
        ),
      );
    }
  } else {
    return TextSpan(text: tempSpanText, style: textStyle);
  }
}