splitAtCharacterIndex method

List<List<InlineSpan>> splitAtCharacterIndex(
  1. SplitAtIndex index, {
  2. bool ignoreFloatedWidgetSpans = false,
})

Splits this list of spans at the given character index and returns one or two lists. If index is zero, or if index is greater than the number of characters in these spans, a list containing just this list is returned. If this list was split, an array of two lists is returned, containing the two new lists.

Implementation

List<List<InlineSpan>> splitAtCharacterIndex(
  SplitAtIndex index, {
  bool ignoreFloatedWidgetSpans = false,
}) {
  if (index.value == 0) return [this];

  var i = 0;
  for (final span in this) {
    final result = span is SplittableMixin<InlineSpan>
        ? (span as SplittableMixin<InlineSpan>).splitAtIndex(index,
            ignoreFloatedWidgetSpans: ignoreFloatedWidgetSpans)
        : span.defaultSplitSpanAtIndex(index,
            ignoreFloatedWidgetSpans: ignoreFloatedWidgetSpans,
            copyWithTextSpan: (span, text, children) => span.copyWith(
                text: text,
                children: children,
                noText: text == null,
                noChildren: children == null));

    if (index.value == 0) {
      if (result.length == 2) {
        return [
          [...take(i), result.first],
          [result.last, ...skip(i + 1)],
        ];
      } else if (result.length == 1) {
        return [
          [...take(i), result.first],
          if (i + 1 < length) [...skip(i + 1)],
        ];
      } else {
        assert(false);
        break;
      }
    }

    i++;
  }

  return [this];
}