shortText static method

String shortText(
  1. String? text,
  2. int length
)

Implementation

static String shortText(String? text, int length) {
  if (text == null || text.isEmpty) return '';

  if (text.length > length) {
    // trim text from position at 0 to length
    text = text.substring(0, length);
    // trim text from position at 0 to position last space, if there is one;
    // falls back to the hard cut when there's no space in it (e.g. one
    // long unbroken word/URL) instead of crashing on substring(0, -1)
    var lastSpace = text.lastIndexOf(' ');
    text = '${lastSpace >= 0 ? text.substring(0, lastSpace) : text}...';
  }

  return text;
}