initials property

String get initials

Gets the initials for the comment author.

For names with multiple words, takes the first letter of the first two words. For single words, takes the first 1-2 characters. Returns '??' if the username is empty.

Examples:

  • "John Doe" -> "JD"
  • "Alice" -> "AL"
  • "A" -> "A"
  • "" -> "??"

Implementation

String get initials {
  if (username.isEmpty) return '??';
  final words = username.trim().split(RegExp(r'\s+'));
  if (words.length >= 2) {
    return (words[0][0] + words[1][0]).toUpperCase();
  }
  return username.length >= 2
      ? username.substring(0, 2).toUpperCase()
      : username.substring(0, 1).toUpperCase();
}