LCOV - code coverage report
Current view: top level - components/avatar - foss_avatar.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 35 35
Test Date: 2026-11-29 09:15:47 Functions: - 0 0

            Line data    Source code
       1              : import 'package:flutter/widgets.dart';
       2              : import 'package:fossui/src/theme/theme.dart';
       3              : 
       4              : part 'foss_avatar_style.dart';
       5              : 
       6              : /// The size axis of a [FossAvatar]. Selects the square box edge and the
       7              : /// fallback text step; [md] (32) is the default.
       8              : enum FossAvatarSize {
       9              :   /// 24 logical pixels.
      10              :   xs._(24),
      11              : 
      12              :   /// 28 logical pixels.
      13              :   sm._(28),
      14              : 
      15              :   /// 32 logical pixels (default).
      16              :   md._(32),
      17              : 
      18              :   /// 36 logical pixels.
      19              :   lg._(36),
      20              : 
      21              :   /// 40 logical pixels.
      22              :   xl._(40),
      23              : 
      24              :   /// 48 logical pixels.
      25              :   xl2._(48),
      26              :   ;
      27              : 
      28              :   const FossAvatarSize._(this._box);
      29              : 
      30              :   final double _box;
      31              : 
      32              :   // The fallback type step climbs with the box: the three smallest share xs,
      33              :   // then sm, base, lg.
      34            1 :   TextStyle _fallbackType(FossTypography t) => switch (this) {
      35            4 :     FossAvatarSize.xs || FossAvatarSize.sm || FossAvatarSize.md => t.xs,
      36            2 :     FossAvatarSize.lg => t.sm,
      37            2 :     FossAvatarSize.xl => t.base,
      38            2 :     FossAvatarSize.xl2 => t.lg,
      39              :   };
      40              : }
      41              : 
      42              : /// {@category Layout}
      43              : /// {@template foss.avatar.preview}
      44              : /// <img src="https://fossui.org/components/avatar/overview/light.png"
      45              : ///   alt="FossAvatar, light theme" width="480"
      46              : ///   style="max-width:100%;height:auto" />
      47              : /// <img src="https://fossui.org/components/avatar/overview/dark.png"
      48              : ///   alt="FossAvatar, dark theme" width="480"
      49              : ///   style="max-width:100%;height:auto" />
      50              : ///
      51              : /// See the [avatar documentation ↗](https://fossui.org/docs/components/avatar) or
      52              : /// try it live in the
      53              : /// [playground ↗](https://play.fossui.org/components/#/?path=components/avatar/fossavatar/playground).
      54              : /// {@endtemplate}
      55              : ///
      56              : /// A user's stand-in: a fixed-size circle that shows a profile [image] and
      57              : /// falls back to a [fallback] glyph (usually initials) while the image loads,
      58              : /// when it is absent, or when it fails to load. Static and non-interactive.
      59              : ///
      60              : /// [image] is an [ImageProvider]; null renders the [fallback] alone. [fallback]
      61              : /// is any widget and shows beneath the image until the first frame arrives, so
      62              : /// a dead URL degrades to initials instead of crashing. [size] drives the box
      63              : /// and the fallback text step. Colors, type, and shape come from
      64              : /// `context.fossTheme`; pass a [FossAvatarStyle] for a one-off override.
      65              : ///
      66              : /// {@macro foss.customize}
      67              : ///
      68              : /// ```dart
      69              : /// FossAvatar(
      70              : ///   image: NetworkImage('https://example.com/v.png'),
      71              : ///   fallback: const Text('VL'),
      72              : ///   semanticLabel: 'Violet Light',
      73              : /// );
      74              : /// ```
      75              : class FossAvatar extends StatelessWidget {
      76              :   /// {@macro foss.avatar.preview}
      77              :   ///
      78              :   /// Creates an avatar. With no [image] the [fallback] fills the circle; with
      79              :   /// neither, a bare `background` circle renders.
      80            1 :   const FossAvatar({
      81              :     this.image,
      82              :     this.fallback,
      83              :     this.size = FossAvatarSize.md,
      84              :     this.semanticLabel,
      85              :     this.style,
      86              :     super.key,
      87              :   });
      88              : 
      89              :   /// The profile image. Null renders the [fallback] alone.
      90              :   final ImageProvider? image;
      91              : 
      92              :   /// Shown beneath the image until it loads, and whenever it is absent or
      93              :   /// fails. Usually [Text] initials; any widget is accepted.
      94              :   final Widget? fallback;
      95              : 
      96              :   /// Selects the box edge and the fallback text step. Defaults to
      97              :   /// [FossAvatarSize.md].
      98              :   final FossAvatarSize size;
      99              : 
     100              :   /// Accessibility name for the avatar. When null the avatar is decorative.
     101              :   final String? semanticLabel;
     102              : 
     103              :   /// Per-instance visual overrides.
     104              :   final FossAvatarStyle? style;
     105              : 
     106            1 :   @override
     107              :   Widget build(BuildContext context) {
     108            1 :     final theme = context.fossTheme;
     109              : 
     110            1 :     final avatar = SizedBox.square(
     111            2 :       dimension: size._box,
     112            1 :       child: DecoratedBox(
     113            1 :         decoration: BoxDecoration(
     114            4 :           color: style?.backgroundColor ?? theme.colors.background,
     115              :           shape: BoxShape.circle,
     116              :         ),
     117            1 :         child: Stack(
     118              :           fit: StackFit.expand,
     119            1 :           children: [
     120            2 :             if (fallback case final fb?) _fallbackLayer(theme, fb),
     121            2 :             if (image case final img?) _imageLayer(img),
     122              :           ],
     123              :         ),
     124              :       ),
     125              :     );
     126              : 
     127              :     // A labelled avatar exposes one image node; unlabelled it is decorative
     128              :     // and contributes no semantics, so the monogram never announces.
     129            1 :     final label = semanticLabel;
     130            1 :     if (label == null) return ExcludeSemantics(child: avatar);
     131            1 :     return Semantics(
     132              :       image: true,
     133              :       label: label,
     134            1 :       child: ExcludeSemantics(child: avatar),
     135              :     );
     136              :   }
     137              : 
     138              :   // The fallback fills a muted circle with the centered glyph, its type stepped
     139              :   // to the box and overridable through [style].
     140            2 :   Widget _fallbackLayer(FossThemeData theme, Widget fb) => DecoratedBox(
     141            1 :     decoration: BoxDecoration(
     142            4 :       color: style?.fallbackColor ?? theme.colors.muted,
     143              :       shape: BoxShape.circle,
     144              :     ),
     145            1 :     child: Center(
     146            1 :       child: DefaultTextStyle.merge(
     147              :         textAlign: TextAlign.center,
     148            1 :         style: size
     149            2 :             ._fallbackType(theme.typography)
     150            1 :             .medium
     151            3 :             .copyWith(color: theme.colors.mutedForeground)
     152            3 :             .merge(style?.fallbackTextStyle),
     153              :         child: fb,
     154              :       ),
     155              :     ),
     156              :   );
     157              : 
     158              :   // The image is the only square layer, so it carries the sole clip; a
     159              :   // fallback-only avatar pays for none. The fallback shows through until the
     160              :   // first frame and whenever the image is absent or fails.
     161            2 :   Widget _imageLayer(ImageProvider img) => ClipOval(
     162            1 :     child: Image(
     163              :       image: img,
     164              :       fit: BoxFit.cover,
     165            1 :       frameBuilder: (context, child, frame, _) =>
     166              :           frame == null ? const SizedBox.shrink() : child,
     167            1 :       errorBuilder: (context, _, _) => const SizedBox.shrink(),
     168              :     ),
     169              :   );
     170              : }
        

Generated by: LCOV version 2.0-1