truncate method

  1. @visibleForTesting
TextEditingValue truncate (
  1. TextEditingValue value,
  2. int maxLength
)
@visibleForTesting

Truncate the given TextEditingValue to maxLength runes.

Implementation

@visibleForTesting
static TextEditingValue truncate(TextEditingValue value, int maxLength) {
  final TextSelection newSelection = value.selection.copyWith(
      baseOffset: math.min(value.selection.start, maxLength),
      extentOffset: math.min(value.selection.end, maxLength),
  );
  final RuneIterator iterator = RuneIterator(value.text);
  if (iterator.moveNext())
    for (int count = 0; count < maxLength; ++count)
      if (!iterator.moveNext())
        break;
  final String truncated = value.text.substring(0, iterator.rawIndex);
  return TextEditingValue(
    text: truncated,
    selection: newSelection,
    composing: TextRange.empty,
  );
}