wordWrap function
Wrap text to a maximum line width.
Implementation
String wordWrap(String text, {int maxWidth = 80}) {
if (text.length <= maxWidth) return text;
final buffer = StringBuffer();
final words = text.split(RegExp(r'\s+'));
var lineLength = 0;
for (var i = 0; i < words.length; i++) {
final word = words[i];
if (lineLength + word.length + (lineLength > 0 ? 1 : 0) > maxWidth) {
buffer.writeln();
lineLength = 0;
}
if (lineLength > 0) {
buffer.write(' ');
lineLength++;
}
buffer.write(word);
lineLength += word.length;
}
return buffer.toString();
}