parseHtml static method

RichText parseHtml(
  1. String htmlText,
  2. double? fontSize
)

Implementation

static RichText parseHtml(String htmlText, double? fontSize) {
  final List<TextSpan> spans = [];

  final regex = RegExp(r'(<b>(.*?)<\/b>)|([^<]+)', caseSensitive: false);

  final matches = regex.allMatches(htmlText);

  for (final match in matches) {
    if (match.group(1) != null) {
      // testo in <b>
      spans.add(
        TextSpan(
          text: match.group(2),
          style: const TextStyle(fontWeight: FontWeight.bold),
        ),
      );
    } else if (match.group(3) != null) {
      // testo normale
      spans.add(
        TextSpan(
          text: match.group(3),
          style: const TextStyle(fontWeight: FontWeight.normal),
        ),
      );
    }
  }

  return RichText(
    textAlign: TextAlign.center,
    text: TextSpan(
      children: spans,
      style: TextStyle(
        color: CuReDesign.textColor,
        fontSize: fontSize ?? 16,
        height: 1.5,
      ),
    ),
  );
}