AdwAvatar.text constructor

AdwAvatar.text({
  1. Key? key,
  2. required String text,
  3. double size = defaultAvatarSize,
  4. AdwColors? backgroundColor,
  5. ImageProvider<Object>? backgroundImage,
})

Constructor that allows the pass of a String object in order to render the first letters of the first two words inside the widget.

The String object should not be empty.

Implementation

factory AdwAvatar.text({
  Key? key,
  required String text,
  double size = defaultAvatarSize,
  AdwColors? backgroundColor,
  ImageProvider? backgroundImage,
}) {
  assert(text.isNotEmpty, 'Text should not be empty');

  // 1. Trim the string from leading and trailing whitespace.
  // 2. Separate the string via whitespaces (can be multiple
  // spaces between words).
  // 3. Select a max of 2 words and store it in a list.
  final words = text.trim().split(RegExp(' +')).take(2);

  // Pick the first letters of the words stored and join them in a string.
  final letters = words.fold(
    '',
    (value, element) => '$value${element[0]}',
  );

  return AdwAvatar(
    key: key,
    size: size,
    backgroundColor: backgroundColor,
    backgroundImage: backgroundImage,
    child: Text(letters.toUpperCase()),
  );
}