toRichText static method

RichText toRichText(
  1. BuildContext context,
  2. String htmlContent, {
  3. dynamic linksCallback(
    1. dynamic
    )?,
  4. Map<String, TextStyle>? overrideStyle,
  5. TextStyle? defaultTextStyle,
})

Returns a RichText widget you can directly add to your widget tree. In addition to the build context and your HTML content, you can pass a function to this method. You can use the function to handle click events on anchor tags. The function will receive the actual link as its argument.

To apply a default text style which affects all of the content (unless they are overridden) pass in defaultTextStyle

HTML.toRichText(
  context,
  htmlContent,
  defaultTextStyle: TextStyle(color: Colors.grey[700]),
);

To overrides and/or apply custom styles to html tags pass in overrideStyle which is a Map of String,TextStyle where String is the html tag, and TextStyle is the style applied

HTML.toRichText(
  context,
  htmlContent,
  overrideStyle: {
    'p': TextStyle(color: Colors.red),
    'a': TextStyle(decoration: TextDecoration.underline),
    //...
    //...
  },
);

Implementation

static RichText toRichText(
  BuildContext context,
  String htmlContent, {
  Function(dynamic)? linksCallback,
  Map<String, TextStyle>? overrideStyle,
  TextStyle? defaultTextStyle,
}) {
  return RichText(
    text: toTextSpan(
      context,
      htmlContent,
      linksCallback: linksCallback,
      overrideStyle: overrideStyle,
      defaultTextStyle: defaultTextStyle,
    ),
  );
}