build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  List<TextSpan> textSpans = []; // Holds styled text spans for RichText.
  int currentStartIndex = 0; // Tracks current index in content string.

  // Determine default color based on theme if no color is specified.
  TextStyle effectiveDefaultStyle =
      (defaultTextStyle ?? const TextStyle(fontSize: 14.0)).copyWith(
    color: defaultTextStyle?.color ??
        (Theme.of(context).brightness == Brightness.light
            ? Colors.black
            : Colors.white),
  );

  // Loop through each highlight style in the list to apply to content text.
  for (final highlightStyle in highlightedTextStyles) {
    // Escape special characters in highlight text for accurate matching.
    String escapedText = RegExp.escape(highlightStyle.highlightedText);

    // Create a regex based on `allowsPartialMatch` to control match type.
    RegExp regex = highlightStyle.allowsPartialMatch
        ? RegExp(escapedText, caseSensitive: false)
        : RegExp(r'\b' + escapedText + r'\b', caseSensitive: false);

    // Find all matches in content text, add surrounding and highlighted text.
    while (true) {
      final match = regex.firstMatch(content.substring(currentStartIndex));
      if (match == null) break; // Exit if no match found.

      // Add unhighlighted text span before the match.
      if (match.start > 0) {
        textSpans.add(TextSpan(
          text: content.substring(
              currentStartIndex, currentStartIndex + match.start),
          style: effectiveDefaultStyle,
        ));
      }

      // Add highlighted text span with specified style.
      textSpans.add(
        TextSpan(
          text: match.group(0),
          style: highlightStyle.customStyle,
        ),
      );

      currentStartIndex += match.end; // Move start to next unmatched section.
    }
  }

  // Add any remaining unhighlighted text after the last match.
  if (currentStartIndex < content.length) {
    textSpans.add(TextSpan(
      text: content.substring(currentStartIndex),
      style: effectiveDefaultStyle,
    ));
  }

  // Use RichText to display the content with all specified highlights.
  return RichText(
    text: TextSpan(
      children: textSpans,
      style: effectiveDefaultStyle,
    ),
  );
}