initials property

String get initials

Gets the user's initials for display in avatars.

For names with multiple words, takes the first letter of the first two words. For single words, takes the first 1-2 characters. If name is blank, derives up to two letters from uuid (alphanumeric only).

Examples:

  • "John Doe" -> "JD"
  • "Alice" -> "AL"
  • "A" -> "A"
  • "" with uuid "ab12-cd" -> "AB"

Implementation

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