Avatar.network constructor

Avatar.network({
  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. int? cacheWidth,
  10. int? cacheHeight,
  11. required String photoUrl,
})

Creates an Avatar with a network image.

This named constructor automatically configures a NetworkImage provider with optional image resizing for memory optimization. Falls back to displaying initials if the network image fails to load.

Parameters:

  • initials (String, required): Fallback text when image loading fails.
  • photoUrl (String, required): URL of the network image to display.
  • cacheWidth (int?, optional): Resize width for memory optimization. If specified, image will be decoded at this width.
  • cacheHeight (int?, optional): Resize height for memory optimization. If specified, image will be decoded at this height.
  • backgroundColor (Color?, optional): Background color for initials fallback.
  • size (double?, optional): Avatar dimensions in logical pixels.
  • borderRadius (double?, optional): Corner radius in logical pixels.
  • badge (AvatarWidget?, optional): Optional badge overlay.
  • badgeAlignment (AlignmentGeometry?, optional): Badge position.
  • badgeGap (double?, optional): Spacing between avatar and badge.

Example:

Avatar.network(
  initials: 'JD',
  photoUrl: 'https://example.com/photo.jpg',
  cacheWidth: 100,
  cacheHeight: 100,
);

Implementation

Avatar.network({
  super.key,
  required this.initials,
  this.backgroundColor,
  this.size,
  this.borderRadius,
  this.badge,
  this.badgeAlignment,
  this.badgeGap,
  int? cacheWidth,
  int? cacheHeight,
  required String photoUrl,
}) : provider = ResizeImage.resizeIfNeeded(
        cacheWidth,
        cacheHeight,
        NetworkImage(photoUrl),
      );