truncateWords method

String truncateWords(
  1. int maxLength, {
  2. String ellipsis = '...',
})

Truncates at a word boundary instead of mid-word.

Implementation

String truncateWords(int maxLength, {String ellipsis = '...'}) {
  if (length <= maxLength) return this;
  final cut = substring(0, maxLength - ellipsis.length);
  final lastSpace = cut.lastIndexOf(' ');
  return (lastSpace > 0 ? cut.substring(0, lastSpace) : cut) + ellipsis;
}