rich_i18n library

A library for parsing rich text with XML tags into structured items.

This library provides a simple way to parse strings containing XML-like tags (similar to HTML) and convert them into a list of RichTextItem objects with styling properties.

Supported XML tags (HTML-like):

  • <b> or <bold> or <strong>: Bold text
  • <u> or <underline>: Underlined text
  • <s> or <strike> or <strikethrough> or <del>: Strikethrough text
  • <i> or <italic> or <em>: Italic text
  • <a href="url">: Hyperlink
  • <span> or <font> with attributes:
    • color: Text color (e.g., "#FF0000" or "red")
    • href: Link URL
    • background-color|backgroundColor: Background color
    • font-weight|fontWeight: Font weight (e.g., "700")
    • font-size|fontSize: Font size (e.g., "14")
    • font-family|fontFamily: Font family name
    • font-style|fontStyle: Font style (kItalicFontStyle)
    • text-decoration|textDecoration: Text decoration (kUnderlineTextDecoration, kLineThroughTextDecoration) Examples:
// Simple bold text
tryGetRichTextSync('hello <b>dart</b>');
// Returns: [RichTextItem(text: 'hello '), RichTextItem(text: 'dart', bold: true)]

// Nested tags
tryGetRichTextSync('hello <b>dart and <u>flutter</u></b>');
// Returns: [
//   RichTextItem(text: 'hello '),
//   RichTextItem(text: 'dart and ', bold: true),
//   RichTextItem(text: 'flutter', bold: true, textDecoration: 'underline')
// ]

Classes

RichTextItem
Represents a segment of rich text with styling properties.
RichTextItemDescriptor
Describes issues found while parsing a rich text item.
VerboseRichTextItem
A RichTextItem with additional descriptor information.

Constants

kBoldFontWeight → const int
The font weight value that represents bold text.
kItalicFontStyle → const String
The font style value that represents italic text.
kLineThroughTextDecoration → const String
The text decoration value that represents line through text.
kUnderlineTextDecoration → const String
The text decoration value that represents underline text.

Functions

tryGetRichTextSync(String text) List<RichTextItem>?
Parses a rich text string with XML tags and returns a list of RichTextItem.
verboseGetRichText(String text) Future<List<VerboseRichTextItem>>
Parses a rich text string with XML tags and returns a list of VerboseRichTextItem containing both the parsed items and descriptors.

Exceptions / Errors

RichTextException
Exception thrown when parsing rich text fails.