LCOV - code coverage report
Current view: top level - components/alert - foss_alert.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 92 92
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/components/dialog/foss_dialog.dart';
       3              : import 'package:fossui/src/icons/foss_glyph.dart';
       4              : import 'package:fossui/src/theme/colors/foss_colors.dart';
       5              : import 'package:fossui/src/theme/foss_theme.dart';
       6              : import 'package:fossui/src/theme/typography/foss_typography.dart';
       7              : 
       8              : part 'foss_alert_style.dart';
       9              : 
      10              : /// Status border alpha (the border tint of a status variant).
      11              : const double _borderAlpha = 0.32;
      12              : 
      13              : /// Status fill alpha (the surface tint of a status variant).
      14              : const double _fillAlpha = 0.04;
      15              : 
      16              : /// Neutral dark fill: the input color at 32% of its alpha, lifted on dark.
      17              : const double _neutralDarkFillAlpha = 0.32;
      18              : 
      19              : /// The leading glyph extent. Centered in a box as tall as the title line so it
      20              : /// lands on the first line rather than the top of the row.
      21              : const double _iconSize = 16;
      22              : 
      23              : /// The status of a [FossAlert], driving its border, fill, and leading glyph.
      24              : enum FossAlertVariant {
      25              :   /// No status: a plain bordered callout.
      26              :   neutral,
      27              : 
      28              :   /// Informational.
      29              :   info,
      30              : 
      31              :   /// A successful outcome.
      32              :   success,
      33              : 
      34              :   /// A caution.
      35              :   warning,
      36              : 
      37              :   /// An error or failure.
      38              :   error,
      39              : }
      40              : 
      41              : /// {@category Feedback}
      42              : /// {@template foss.alert.preview}
      43              : /// <img src="https://fossui.org/components/alert/overview/light.png"
      44              : ///   alt="FossAlert, light theme" width="480"
      45              : ///   style="max-width:100%;height:auto" />
      46              : /// <img src="https://fossui.org/components/alert/overview/dark.png"
      47              : ///   alt="FossAlert, dark theme" width="480"
      48              : ///   style="max-width:100%;height:auto" />
      49              : ///
      50              : /// See the [alert documentation ↗](https://fossui.org/docs/components/alert) or
      51              : /// try it live in the
      52              : /// [playground ↗](https://play.fossui.org/components/#/?path=components/alert/fossalert/playground).
      53              : /// {@endtemplate}
      54              : ///
      55              : /// A static inline callout: a leading status glyph, a title, an optional
      56              : /// description, and optional actions, on a bordered surface tinted by the
      57              : /// [variant].
      58              : ///
      59              : /// The whole surface is an `alert` live region; the status glyph is semantic.
      60              : /// Colors, type, radius, and spacing come from `context.fossTheme`. Actions are
      61              : /// any widgets, typically `FossButton`s.
      62              : ///
      63              : /// {@macro foss.customize}
      64              : ///
      65              : /// See also [FossAlertDialog] for a modal that demands a decision.
      66              : ///
      67              : /// ```dart
      68              : /// FossAlert(
      69              : ///   variant: FossAlertVariant.warning,
      70              : ///   title: const Text('Storage almost full'),
      71              : ///   description: const Text('Free up space to keep syncing.'),
      72              : /// );
      73              : /// ```
      74              : class FossAlert extends StatelessWidget {
      75              :   /// {@macro foss.alert.preview}
      76              :   ///
      77              :   /// Creates an alert. [title] is effectively required; the rest are optional.
      78            2 :   const FossAlert({
      79              :     this.title,
      80              :     this.description,
      81              :     this.icon,
      82              :     this.actions = const <Widget>[],
      83              :     this.variant = FossAlertVariant.neutral,
      84              :     this.style,
      85              :     super.key,
      86              :   });
      87              : 
      88              :   /// The title line.
      89              :   final Widget? title;
      90              : 
      91              :   /// The description below the title.
      92              :   final Widget? description;
      93              : 
      94              :   /// Overrides the default leading glyph. A status variant paints its status
      95              :   /// glyph unless overridden here; [FossAlertVariant.neutral] has no default
      96              :   /// glyph, so its leading slot stays empty until an [icon] is given. A custom
      97              :   /// icon inherits the variant accent color and the glyph size.
      98              :   final Widget? icon;
      99              : 
     100              :   /// Action widgets, rendered below the text. Empty hides them.
     101              :   final List<Widget> actions;
     102              : 
     103              :   /// The status. Defaults to [FossAlertVariant.neutral].
     104              :   final FossAlertVariant variant;
     105              : 
     106              :   /// Per-instance visual overrides.
     107              :   final FossAlertStyle? style;
     108              : 
     109            2 :   @override
     110              :   Widget build(BuildContext context) {
     111            2 :     final theme = context.fossTheme;
     112            2 :     final colors = theme.colors;
     113            2 :     final sp = theme.spacing;
     114            4 :     final v = _resolve(colors, variant);
     115            2 :     final s = style;
     116              : 
     117            6 :     final titleStyle = theme.typography.sm.medium
     118            4 :         .copyWith(color: colors.foreground, decoration: TextDecoration.none)
     119            3 :         .merge(s?.titleStyle);
     120            4 :     final descriptionStyle = theme.typography.sm
     121            2 :         .copyWith(
     122            2 :           color: colors.mutedForeground,
     123              :           decoration: TextDecoration.none,
     124              :         )
     125            3 :         .merge(s?.descriptionStyle);
     126              : 
     127            3 :     final iconColor = s?.iconColor ?? v.accent;
     128            6 :     final leading = icon ?? v.glyph?.call(iconColor);
     129              :     // The box matches the title's line height so the glyph centers on the first
     130              :     // line instead of top-aligning to the row.
     131            6 :     final iconBox = (titleStyle.fontSize ?? 14) * (titleStyle.height ?? 1);
     132              : 
     133            2 :     return Semantics(
     134              :       container: true,
     135              :       liveRegion: true,
     136            2 :       child: DecoratedBox(
     137            2 :         decoration: ShapeDecoration(
     138            3 :           color: s?.backgroundColor ?? v.fill,
     139            2 :           shape: RoundedSuperellipseBorder(
     140            5 :             side: BorderSide(color: s?.borderColor ?? v.border),
     141            2 :             borderRadius: BorderRadius.circular(
     142            5 :               s?.borderRadius ?? theme.radii.xl,
     143              :             ),
     144              :           ),
     145              :         ),
     146            2 :         child: Padding(
     147            2 :           padding: EdgeInsets.symmetric(
     148            2 :             horizontal: sp(3.5),
     149            2 :             vertical: sp(3),
     150              :           ),
     151            2 :           child: Row(
     152              :             crossAxisAlignment: CrossAxisAlignment.start,
     153            2 :             spacing: sp(2),
     154            2 :             children: [
     155              :               if (leading != null)
     156            2 :                 SizedBox(
     157              :                   width: _iconSize,
     158              :                   height: iconBox,
     159            2 :                   child: Center(
     160            2 :                     child: IconTheme.merge(
     161            2 :                       data: IconThemeData(color: iconColor, size: _iconSize),
     162              :                       child: leading,
     163              :                     ),
     164              :                   ),
     165              :                 ),
     166            2 :               Expanded(
     167            2 :                 child: Column(
     168              :                   crossAxisAlignment: CrossAxisAlignment.start,
     169              :                   mainAxisSize: MainAxisSize.min,
     170            2 :                   spacing: sp(0.5),
     171            2 :                   children: [
     172            2 :                     if (title case final title?)
     173            2 :                       DefaultTextStyle.merge(style: titleStyle, child: title),
     174            2 :                     if (description case final description?)
     175            1 :                       DefaultTextStyle.merge(
     176              :                         style: descriptionStyle,
     177              :                         child: description,
     178              :                       ),
     179            4 :                     if (actions.isNotEmpty)
     180            1 :                       Padding(
     181            2 :                         padding: EdgeInsets.only(top: sp(2)),
     182            1 :                         child: Align(
     183              :                           alignment: AlignmentDirectional.centerEnd,
     184            1 :                           child: Wrap(
     185              :                             alignment: WrapAlignment.end,
     186            1 :                             spacing: sp(1),
     187            1 :                             runSpacing: sp(1),
     188            1 :                             children: actions,
     189              :                           ),
     190              :                         ),
     191              :                       ),
     192              :                   ],
     193              :                 ),
     194              :               ),
     195              :             ],
     196              :           ),
     197              :         ),
     198              :       ),
     199              :     );
     200              :   }
     201              : }
     202              : 
     203              : /// The resolved per-variant appearance.
     204              : @immutable
     205              : class _AlertVisuals {
     206            2 :   const _AlertVisuals({
     207              :     required this.border,
     208              :     required this.fill,
     209              :     required this.accent,
     210              :     required this.glyph,
     211              :   });
     212              : 
     213              :   final Color border;
     214              :   final Color fill;
     215              :   final Color accent;
     216              : 
     217              :   /// Builds the leading status glyph in the given color. Null for the neutral
     218              :   /// variant, which has no status mark.
     219              :   final Widget Function(Color color)? glyph;
     220              : }
     221              : 
     222            2 : _AlertVisuals _resolve(FossColors c, FossAlertVariant variant) {
     223            8 :   Color tintBorder(Color role) => role.withValues(alpha: role.a * _borderAlpha);
     224            8 :   Color tintFill(Color role) => role.withValues(alpha: role.a * _fillAlpha);
     225              : 
     226              :   switch (variant) {
     227            2 :     case FossAlertVariant.neutral:
     228            2 :       final fill = c.isDark
     229            1 :           ? Color.alphaBlend(
     230            5 :               c.input.withValues(alpha: c.input.a * _neutralDarkFillAlpha),
     231            1 :               c.background,
     232              :             )
     233              :           : const Color(0x00000000);
     234            2 :       return _AlertVisuals(
     235            2 :         border: c.border,
     236              :         fill: fill,
     237            2 :         accent: c.mutedForeground,
     238              :         glyph: null,
     239              :       );
     240            2 :     case FossAlertVariant.info:
     241            2 :       return _AlertVisuals(
     242            4 :         border: tintBorder(c.info),
     243            4 :         fill: tintFill(c.info),
     244            2 :         accent: c.info,
     245            4 :         glyph: (color) => FossGlyphIcon(
     246            2 :           InfoGlyph(color),
     247              :           size: _iconSize,
     248              :           semanticLabel: 'info',
     249              :         ),
     250              :       );
     251            2 :     case FossAlertVariant.success:
     252            2 :       return _AlertVisuals(
     253            4 :         border: tintBorder(c.success),
     254            4 :         fill: tintFill(c.success),
     255            2 :         accent: c.success,
     256            4 :         glyph: (color) => FossGlyphIcon(
     257            2 :           SuccessGlyph(color),
     258              :           size: _iconSize,
     259              :           semanticLabel: 'success',
     260              :         ),
     261              :       );
     262            2 :     case FossAlertVariant.warning:
     263            2 :       return _AlertVisuals(
     264            4 :         border: tintBorder(c.warning),
     265            4 :         fill: tintFill(c.warning),
     266            2 :         accent: c.warning,
     267            4 :         glyph: (color) => FossGlyphIcon(
     268            2 :           WarningGlyph(color),
     269              :           size: _iconSize,
     270              :           semanticLabel: 'warning',
     271              :         ),
     272              :       );
     273            2 :     case FossAlertVariant.error:
     274            2 :       return _AlertVisuals(
     275            4 :         border: tintBorder(c.destructive),
     276            4 :         fill: tintFill(c.destructive),
     277            2 :         accent: c.destructive,
     278            4 :         glyph: (color) => FossGlyphIcon(
     279            2 :           ErrorGlyph(color),
     280              :           size: _iconSize,
     281              :           semanticLabel: 'error',
     282              :         ),
     283              :       );
     284              :   }
     285              : }
        

Generated by: LCOV version 2.0-1