truncateLines function

String truncateLines(
  1. String text,
  2. int maxLines, {
  3. String suffix = '\n...',
})

Truncate text to a maximum number of lines.

Implementation

String truncateLines(String text, int maxLines, {String suffix = '\n...'}) {
  final lines = text.split('\n');
  if (lines.length <= maxLines) return text;
  return '${lines.take(maxLines).join('\n')}$suffix';
}