visitAttributions method

void visitAttributions(
  1. AttributionVisitor visitor
)

Visits all attributions in this AttributedText by calling visitor whenever an attribution begins or ends.

If multiple attributions begin or end at the same index, then all of those attributions are reported together.

Only Reports Beginnings and Endings:

This visitation method does not report all applied attributions at a given index. It only reports attributions that begin or end at a specific index.

For example:

Bold: |xxxxxxxxxxxx| Italics: |------xxxxxx|

Bold is attributed throughout the range. Italics begins at index 6. When visitor is notified about italics beginning at 6, visitor is NOT notified that bold applies at that same index.

Implementation

void visitAttributions(AttributionVisitor visitor) {
  final startingAttributions = <Attribution>{};
  final endingAttributions = <Attribution>{};
  int currentIndex = -1;

  visitor.onVisitBegin();

  for (final marker in spans.markers) {
    // If the marker offset differs from the currentIndex it means
    // we already added all the attributions that start or end
    // at currentIndex.
    if (marker.offset != currentIndex) {
      if (currentIndex >= 0) {
        visitor.visitAttributions(this, currentIndex, startingAttributions, endingAttributions);
      }

      currentIndex = marker.offset;
      startingAttributions.clear();
      endingAttributions.clear();
    }

    if (marker.isStart) {
      startingAttributions.add(marker.attribution);
    } else {
      endingAttributions.add(marker.attribution);
    }
  }

  // Visit the final set of end markers.
  if (endingAttributions.isNotEmpty) {
    visitor.visitAttributions(this, currentIndex, startingAttributions, endingAttributions);
  }

  visitor.onVisitEnd();
}