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) {
  final String textLC = caseSensitive ? text : text.toLowerCase();

  // corner case: if both term and terms array are passed then combine
  final List<String> termList = [term ?? '', ...(terms ?? [])];

  // remove empty search terms ('') because they cause infinite loops
  final List<String> termListLC = termList
      .where((s) => s.isNotEmpty)
      .map((s) => caseSensitive ? s : s.toLowerCase())
      .toList();

  List<InlineSpan> children = [];

  int start = 0;
  int idx = 0; // walks text (string that is searched)
  while (idx < textLC.length) {
    // print('=== idx=$idx');
    nonHighlightAdd(int end) => children
        .add(TextSpan(text: text.substring(start, end), style: textStyle));

    // find index of term that's closest to current idx position
    int iNearest = -1;
    int idxNearest = __int64MaxValue;
    for (int i = 0; i < termListLC.length; i++) {
      // print('*** i=$i');
      int at;
      if ((at = textLC.indexOf(termListLC[i], idx)) >= 0) //MAGIC//CORE
      {
        // print('idx=$idx i=$i at=$at => FOUND: ${termListLC[i]}');

        if (words) {
          if (at > 0 &&
              !wordDelimiters.contains(
                  textLC[at - 1])) // is preceding character a delimiter?
          {
            // print('disqualify preceding: idx=$idx i=$i');
            continue; // preceding character isn't delimiter so disqualify
          }

          int followingIdx = at + termListLC[i].length;
          if (followingIdx < textLC.length &&
              !wordDelimiters.contains(textLC[
                  followingIdx])) // is character following the search term a delimiter?
          {
            // print('disqualify following: idx=$idx i=$i');
            continue; // following character isn't delimiter so disqualify
          }
        }

        // print('term #$i found at=$at (${termListLC[i]})');
        if (at < idxNearest) {
          // print('PEG');
          iNearest = i;
          idxNearest = at;
        }
      }
    }

    if (iNearest >= 0) {
      // found one of the terms at or after idx
      // iNearest is the index of the closest term at or after idx that matches

      // print('iNearest=$iNearest @ $idxNearest');
      if (start < idxNearest) {
        // we found a match BUT FIRST output non-highlighted text that comes BEFORE this match
        nonHighlightAdd(idxNearest);
        start = idxNearest;
      }

      // output the match using desired highlighting
      int termLen = termListLC[iNearest].length;
      children.add(TextSpan(
          text: text.substring(start, idxNearest + termLen),
          style: textStyleHighlight));
      start = idx = idxNearest + termLen;
    } else {
      if (words) {
        idx++;
        nonHighlightAdd(idx);
        start = idx;
      } else {
        // if none match at all (ever!)
        // --or--
        // one or more matches but during this iteration there are NO MORE matches
        // in either case, add reminder of text as non-highlighted text
        nonHighlightAdd(textLC.length);
        break;
      }
    }
  }

  return RichText(
      maxLines: maxLines,
      overflow: overflow,
      text: TextSpan(children: children, style: textStyle),
      textAlign: textAlign,
      textScaleFactor: MediaQuery.of(context).textScaleFactor);
}