getRichText static method

List<Widget> getRichText(
  1. Paragraph paragraph,
  2. List<InlineSpan> paragraphWidget,
  3. List<Styles> stylesList,
  4. Document wordDocument,
)

Function for processing rich text

Implementation

static List<Widget> getRichText(Paragraph paragraph, List<InlineSpan> paragraphWidget, List<Styles> stylesList, Document wordDocument) {
  TextStyle textStyle = const TextStyle();
  String jc = "";
  String indentText = "";
  List<Widget> pageWidgets = [];
  int spaceBefore = 0;
  int spaceAfter = 0;
  Map<String, String> paraBorder = {};
  if (paragraph.style.isNotEmpty) {
    Styles? paraStyles = stylesList.firstWhereOrNull((style) {
      return style.styleId == paragraph.style;
    });
    if (paraStyles != null) {
      if (paraStyles.fontSize != 0) {
        textStyle = textStyle.copyWith(fontSize: paraStyles.fontSize.toDouble() / 2);
      }
      if (paraStyles.formats.contains("italic")) {
        textStyle = textStyle.copyWith(fontStyle: FontStyle.italic);
      }
      if (paraStyles.formats.contains("bold")) {
        textStyle = textStyle.copyWith(fontWeight: FontWeight.bold);
      }
      if (paraStyles.formats.contains("single-underline")) {
        textStyle = textStyle.copyWith(decoration: TextDecoration.underline);
      }
      if (paraStyles.formats.contains("double-underline")) {
        textStyle = textStyle.copyWith(decoration: TextDecoration.underline, decorationStyle: TextDecorationStyle.double);
      }
      if (paraStyles.formats.contains("strike")) {
        textStyle = textStyle.copyWith(decoration: TextDecoration.lineThrough);
      }
      if (paraStyles.formats.contains("subscript")) {
        textStyle = textStyle.copyWith(fontFeatures: [const FontFeature.subscripts()]);
      }
      if (paraStyles.textColor != null && paraStyles.textColor != "auto") {
        Color selectedColor = Color(int.parse("FF${paraStyles.textColor!}", radix: 16));

        textStyle = textStyle.copyWith(color: selectedColor);
      } else {
        textStyle = textStyle.copyWith(color: Colors.black);
      }
      if (paraStyles.fonts.isNotEmpty) {
        textStyle = textStyle.copyWith(fontFamily: paraStyles.fonts["ascii"]);
      }
      if (paraStyles.jc != null && paraStyles.jc!.isNotEmpty) {
        jc = paraStyles.jc!;
      }

      if (paraStyles.firstLineInd != 0) {
        for (int i = 0; i < paraStyles.firstLineInd / 100; i++) {
          indentText += " ";
        }
      }
      if (paraStyles.leftInd != 0) {
        for (int i = 0; i < paraStyles.leftInd / 100; i++) {
          indentText += " ";
        }
      }
      if (paraStyles.spacingBefore != 0) {
        spaceBefore = paraStyles.spacingBefore;
      }
      if (paraStyles.spacingAfter != 0) {
        spaceAfter = paraStyles.spacingAfter;
      }
      if (paraStyles.styleId == "ListParagraph") {
        indentText = "\u2022 $indentText";
      }
      if (paraStyles.paraGraphBorder.isNotEmpty) {
        paraBorder = paraStyles.paraGraphBorder;
      }
      if (paraStyles.jc != null && paraStyles.jc!.isNotEmpty) {
        jc = paraStyles.jc!;
      }
    }
  } else {
    textStyle = textStyle.copyWith(color: Colors.black);
  }
  if (paragraph.style.isEmpty) {
    if (wordDocument.defaultLineSpacing != 0) {
      for (int i = 0; i < wordDocument.defaultLineSpacing / 100; i++) {
        indentText += " ";
      }
    }
    if (wordDocument.defaultFontSize != 0) {
      textStyle = textStyle.copyWith(fontSize: wordDocument.defaultFontSize / 2);
    }
  }
  if (paragraph.shadingColor != null && paragraph.shadingColor != "auto") {
    Color paraBgColor = Color(int.parse("FF${paragraph.shadingColor!}", radix: 16));
    textStyle = textStyle.copyWith(backgroundColor: paraBgColor);
  }
  if (paragraph.formats != null && paragraph.formats!.isNotEmpty) {
    if (paragraph.formats!["jc"] != null) {
      jc = paragraph.formats!["jc"]!;
    }
  }

  RichText richText = RichText(
    softWrap: true,
    text: TextSpan(text: indentText, style: textStyle, children: paragraphWidget),
    textAlign: jc == "right"
        ? TextAlign.end
        : jc == "center"
            ? TextAlign.center
            : TextAlign.start,
  );

  if (spaceBefore != 0) {
    pageWidgets.add(SizedBox(
      height: spaceBefore.toDouble() / 10,
    ));
  }
  Widget alignmentWidget = Container(
    child: richText,
  );
  if (jc == "center") {
    alignmentWidget = Row(
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [richText],
    );
  } else if (jc == "right") {
    alignmentWidget = Row(
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.end,
      children: [Expanded(child: richText)],
    );
  }
  if (paraBorder.isNotEmpty) {
    pageWidgets.add(borderContainer(paraBorder, alignmentWidget));
  } else {
    pageWidgets.add(alignmentWidget);
  }
  if (spaceAfter != 0) {
    pageWidgets.add(SizedBox(
      height: spaceAfter.toDouble() / 10,
    ));
  }
  return pageWidgets;
}