Avatar constructor

const Avatar({
  1. Key? key,
  2. required String initials,
  3. Color? backgroundColor,
  4. double? size,
  5. double? borderRadius,
  6. AvatarWidget? badge,
  7. AlignmentGeometry? badgeAlignment,
  8. double? badgeGap,
  9. ImageProvider<Object>? provider,
})

Creates an Avatar widget with optional image provider.

The default constructor creates an avatar that can display either an image (via provider) or user initials. If no image provider is specified or image loading fails, the avatar falls back to displaying initials.

Parameters:

  • initials (String, required): Text to display when no image is provided or image loading fails. Should typically be user's initials.
  • backgroundColor (Color?, optional): Background color for the initials display. If null, uses theme's muted color.
  • size (double?, optional): Width and height of the avatar in logical pixels. If null, defaults to theme.scaling * 40.
  • borderRadius (double?, optional): Corner radius in logical pixels. If null, defaults to theme.radius * size for proportional rounding.
  • badge (AvatarWidget?, optional): Optional badge overlay for status indicators. Positioned according to badgeAlignment.
  • badgeAlignment (AlignmentGeometry?, optional): Position of the badge relative to the avatar. If null, uses calculated offset based on sizes.
  • badgeGap (double?, optional): Spacing between avatar and badge. If null, defaults to theme.scaling * 4.
  • provider (ImageProvider?, optional): Image to display. If null or loading fails, shows initials instead.

Example:

Avatar(
  initials: 'JD',
  size: 48,
  backgroundColor: Colors.blue.shade100,
  badge: AvatarBadge(color: Colors.green),
);

Implementation

const Avatar({
  super.key,
  required this.initials,
  this.backgroundColor,
  this.size,
  this.borderRadius,
  this.badge,
  this.badgeAlignment,
  this.badgeGap,
  this.provider,
});