LCOV - code coverage report
Current view: top level - foundation - foss_dialog_surface.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 126 126
Test Date: 2026-11-29 09:15:47 Functions: - 0 0

            Line data    Source code
       1              : import 'dart:math' as math;
       2              : 
       3              : import 'package:flutter/widgets.dart';
       4              : import 'package:fossui/src/theme/foss_theme.dart';
       5              : import 'package:fossui/src/theme/spacing/foss_spacing.dart';
       6              : import 'package:fossui/src/theme/typography/foss_typography.dart';
       7              : 
       8              : /// Inner top-lit rim on the surface: a faint dark line in light mode, a faint
       9              : /// white highlight in dark mode, the same rim the controls carry.
      10              : const Color _rimLight = Color(0x0A000000);
      11              : const Color _rimDark = Color(0x0FFFFFFF);
      12              : 
      13              : /// Compact height of a footer action, so the bar hugs the controls rather than
      14              : /// reserving the full 48px tap target around each one.
      15              : const double _footerActionHeight = 36;
      16              : 
      17              : /// The footer treatment shared by the dialog and the alert dialog.
      18              : enum FossDialogFooterVariant {
      19              :   /// No bar: the actions sit on the plain surface.
      20              :   bare,
      21              : 
      22              :   /// A bordered bar tinted with the muted role behind the actions.
      23              :   filled,
      24              : }
      25              : 
      26              : /// How a modal presents: a centered card or a bottom-stuck sheet.
      27              : enum FossDialogPresentation {
      28              :   /// A card centered in the viewport, clamped to a max width.
      29              :   centered,
      30              : 
      31              :   /// A full-width sheet stuck to the bottom edge, slid up into view. The
      32              :   /// mobile-first default.
      33              :   bottomSheet,
      34              : }
      35              : 
      36              : /// The route label from a [Text] title, or null for any other title widget.
      37            5 : String? dialogRouteLabel(Widget? title) => switch (title) {
      38            9 :   Text(:final data?) => data,
      39              :   _ => null,
      40              : };
      41              : 
      42              : /// The header, body, and footer insets, with the seams between slots collapsed.
      43              : ///
      44              : /// The slots share a 16 gutter (`spacing(4)`). Where two meet, the facing edges
      45              : /// tighten so an all-slots surface does not read looser than a bare one: the
      46              : /// body drops its top to 4 under a header and its bottom to 4 above a bare
      47              : /// footer, and a bare footer drops its top to 12 below a panel or 16 directly
      48              : /// under a header. A [filled] footer keeps the full gutters (its bar draws the
      49              : /// separation). [safeBottom], the system inset under a bottom sheet, is
      50              : /// absorbed into the footer bottom rather than stacked on it.
      51            6 : ({EdgeInsets header, EdgeInsets body, EdgeInsets footer}) resolveDialogInsets({
      52              :   required FossSpacing spacing,
      53              :   required bool hasHeader,
      54              :   required bool hasContent,
      55              :   required bool hasActions,
      56              :   required bool filled,
      57              :   required double safeBottom,
      58              : }) {
      59            6 :   final gutter = spacing(4);
      60            6 :   final footerBase = filled ? spacing(3) : gutter;
      61              :   return (
      62            6 :     header: EdgeInsets.only(
      63              :       left: gutter,
      64              :       right: gutter,
      65              :       top: gutter,
      66            6 :       bottom: spacing(4),
      67              :     ),
      68            6 :     body: EdgeInsets.only(
      69              :       left: gutter,
      70              :       right: gutter,
      71            5 :       top: hasHeader ? spacing(1) : gutter,
      72            2 :       bottom: hasActions && !filled ? spacing(1) : gutter,
      73              :     ),
      74            6 :     footer: EdgeInsets.only(
      75              :       left: gutter,
      76              :       right: gutter,
      77            9 :       top: filled ? spacing(3) : (hasContent ? spacing(3) : spacing(4)),
      78            6 :       bottom: safeBottom > footerBase ? safeBottom : footerBase,
      79              :     ),
      80              :   );
      81              : }
      82              : 
      83              : /// Builds the shared title and description column, or null when both are
      84              : /// absent. The surface applies the padding (it collapses the seams against the
      85              : /// panel and footer), so this returns the unpadded column. The column aligns to
      86              : /// the start; overrides merge over the token defaults.
      87            5 : Widget? buildDialogHeader({
      88              :   required FossThemeData theme,
      89              :   required Widget? title,
      90              :   required Widget? description,
      91              :   required TextStyle? titleStyleOverride,
      92              :   required TextStyle? descriptionStyleOverride,
      93              : }) {
      94              :   if (title == null && description == null) return null;
      95            4 :   final colors = theme.colors;
      96           12 :   final titleStyle = theme.typography.xl.semibold
      97            8 :       .copyWith(color: colors.popoverForeground, height: 1)
      98            4 :       .merge(titleStyleOverride);
      99            8 :   final descriptionStyle = theme.typography.sm
     100            8 :       .copyWith(color: colors.mutedForeground)
     101            4 :       .merge(descriptionStyleOverride);
     102              : 
     103            4 :   return Column(
     104              :     crossAxisAlignment: CrossAxisAlignment.start,
     105              :     mainAxisSize: MainAxisSize.min,
     106            8 :     spacing: theme.spacing(2),
     107            4 :     children: [
     108              :       if (title case final title?)
     109            4 :         DefaultTextStyle.merge(style: titleStyle, child: title),
     110              :       if (description case final description?)
     111            2 :         DefaultTextStyle.merge(style: descriptionStyle, child: description),
     112              :     ],
     113              :   );
     114              : }
     115              : 
     116              : /// The resolved surface visuals: the theme tokens after any per-instance style
     117              : /// overrides. Grouped so the surface takes one value rather than five loose
     118              : /// fields.
     119              : @immutable
     120              : class FossDialogVisual {
     121              :   /// Creates a resolved visual bundle. Every field is already resolved; there
     122              :   /// is no fallback here.
     123            5 :   const FossDialogVisual({
     124              :     required this.maxWidth,
     125              :     required this.backgroundColor,
     126              :     required this.borderColor,
     127              :     required this.borderRadius,
     128              :     required this.shadows,
     129              :   });
     130              : 
     131              :   /// Maximum width of the card in logical pixels.
     132              :   final double maxWidth;
     133              : 
     134              :   /// Surface fill.
     135              :   final Color backgroundColor;
     136              : 
     137              :   /// 1px border color.
     138              :   final Color borderColor;
     139              : 
     140              :   /// Corner radius in logical pixels.
     141              :   final double borderRadius;
     142              : 
     143              :   /// Shadow layers under the surface.
     144              :   final List<BoxShadow> shadows;
     145              : }
     146              : 
     147              : /// The centered card shared by the modal overlays: surface, optional header,
     148              : /// scrollable body, optional footer, and an optional close affordance. The
     149              : /// header is built by the caller (its alignment differs between the dialog and
     150              : /// the alert dialog); this widget owns the surface and the footer.
     151              : class FossDialogSurface extends StatelessWidget {
     152              :   /// Creates the surface. [header] and [content] are already-built slots;
     153              :   /// [actions] drive the footer; [closeButton] overlays the top corner.
     154            5 :   const FossDialogSurface({
     155              :     required this.header,
     156              :     required this.content,
     157              :     required this.actions,
     158              :     required this.footerVariant,
     159              :     required this.visual,
     160              :     this.presentation = FossDialogPresentation.centered,
     161              :     this.closeButton,
     162              :     this.semanticLabel,
     163              :     super.key,
     164              :   });
     165              : 
     166              :   /// The header slot (title and description), or null.
     167              :   final Widget? header;
     168              : 
     169              :   /// The scrollable body slot, or null.
     170              :   final Widget? content;
     171              : 
     172              :   /// The footer actions; empty hides the footer.
     173              :   final List<Widget> actions;
     174              : 
     175              :   /// The footer treatment.
     176              :   final FossDialogFooterVariant footerVariant;
     177              : 
     178              :   /// The resolved surface tokens (fill, border, radius, shadow, width).
     179              :   final FossDialogVisual visual;
     180              : 
     181              :   /// Whether the surface is a centered card or a bottom-stuck sheet.
     182              :   final FossDialogPresentation presentation;
     183              : 
     184              :   /// An optional close affordance, placed in the top corner.
     185              :   final Widget? closeButton;
     186              : 
     187              :   /// Names the modal route for assistive technology, usually the title text.
     188              :   final String? semanticLabel;
     189              : 
     190            5 :   @override
     191              :   Widget build(BuildContext context) {
     192           10 :     final sheet = presentation == FossDialogPresentation.bottomSheet;
     193            9 :     final surface = sheet ? _buildSheet(context) : _buildCard(context);
     194              : 
     195            5 :     return Semantics(
     196              :       scopesRoute: true,
     197              :       namesRoute: true,
     198              :       explicitChildNodes: true,
     199            5 :       label: semanticLabel,
     200              :       child: surface,
     201              :     );
     202              :   }
     203              : 
     204              :   /// The centered card: clamped width, full superellipse border, top-lit rim.
     205            4 :   Widget _buildCard(BuildContext context) {
     206            4 :     final theme = context.fossTheme;
     207            4 :     final shape = RoundedSuperellipseBorder(
     208           12 :       side: BorderSide(color: visual.borderColor),
     209           12 :       borderRadius: BorderRadius.circular(visual.borderRadius),
     210              :     );
     211              : 
     212            4 :     final card = ConstrainedBox(
     213           12 :       constraints: BoxConstraints(maxWidth: visual.maxWidth),
     214            4 :       child: DecoratedBox(
     215            4 :         decoration: ShapeDecoration(
     216            8 :           color: visual.backgroundColor,
     217              :           shape: shape,
     218            8 :           shadows: visual.shadows,
     219              :         ),
     220            4 :         child: ClipPath(
     221            4 :           clipper: ShapeBorderClipper(shape: shape),
     222            4 :           child: CustomPaint(
     223            4 :             foregroundPainter: _RimPainter(
     224            8 :               color: theme.colors.isDark ? _rimDark : _rimLight,
     225           16 :               radius: math.max(0, visual.borderRadius - 1),
     226            8 :               topLit: theme.colors.isDark,
     227              :             ),
     228            4 :             child: _slots(context, safeBottom: 0),
     229              :           ),
     230              :         ),
     231              :       ),
     232              :     );
     233              : 
     234            4 :     return Padding(
     235           12 :       padding: EdgeInsets.all(theme.spacing(4)),
     236            4 :       child: Center(child: card),
     237              :     );
     238              :   }
     239              : 
     240              :   /// The bottom sheet: full width, squared, top border only, no rim, stuck to
     241              :   /// the bottom edge with the actions stacked full width.
     242            5 :   Widget _buildSheet(BuildContext context) {
     243            5 :     final theme = context.fossTheme;
     244           10 :     final safeBottom = MediaQuery.paddingOf(context).bottom;
     245              : 
     246            5 :     final sheet = DecoratedBox(
     247            5 :       decoration: BoxDecoration(
     248           10 :         color: visual.backgroundColor,
     249           20 :         border: Border(top: BorderSide(color: visual.borderColor)),
     250           10 :         boxShadow: visual.shadows,
     251              :       ),
     252            5 :       child: _slots(context, safeBottom: safeBottom),
     253              :     );
     254              : 
     255              :     // Keep a gap above a full-height sheet so it never covers the whole screen.
     256            5 :     return Padding(
     257           15 :       padding: EdgeInsets.only(top: theme.spacing(12)),
     258            5 :       child: Align(alignment: Alignment.bottomCenter, child: sheet),
     259              :     );
     260              :   }
     261              : 
     262              :   /// The header / body / footer column with the close affordance overlaid. The
     263              :   /// seam collapse between the slots is resolved by [resolveDialogInsets].
     264            5 :   Widget _slots(BuildContext context, {required double safeBottom}) {
     265            5 :     final theme = context.fossTheme;
     266            5 :     final insets = resolveDialogInsets(
     267            5 :       spacing: theme.spacing,
     268            5 :       hasHeader: header != null,
     269            5 :       hasContent: content != null,
     270           10 :       hasActions: actions.isNotEmpty,
     271           10 :       filled: footerVariant == FossDialogFooterVariant.filled,
     272              :       safeBottom: safeBottom,
     273              :     );
     274              : 
     275            5 :     return Stack(
     276            5 :       children: [
     277            5 :         Column(
     278              :           mainAxisSize: MainAxisSize.min,
     279              :           crossAxisAlignment: CrossAxisAlignment.stretch,
     280            5 :           children: [
     281            5 :             if (header case final header?)
     282            4 :               Padding(padding: insets.header, child: header),
     283            5 :             if (content case final content?)
     284            4 :               Flexible(
     285            4 :                 child: SingleChildScrollView(
     286            4 :                   child: Padding(padding: insets.body, child: content),
     287              :                 ),
     288              :               ),
     289           10 :             if (actions.isNotEmpty)
     290            3 :               _Footer(
     291            3 :                 variant: footerVariant,
     292              :                 padding: insets.footer,
     293            3 :                 actions: actions,
     294              :               ),
     295              :           ],
     296              :         ),
     297            5 :         if (closeButton case final button?)
     298            2 :           PositionedDirectional(
     299            4 :             top: theme.spacing(2),
     300            4 :             end: theme.spacing(2),
     301              :             child: button,
     302              :           ),
     303              :       ],
     304              :     );
     305              :   }
     306              : }
     307              : 
     308              : /// Paints a 1px rim inside the surface: brightest along one edge, fading to
     309              : /// nothing by the middle. [topLit] lights the top edge; otherwise the bottom.
     310              : class _RimPainter extends CustomPainter {
     311            4 :   const _RimPainter({
     312              :     required this.color,
     313              :     required this.radius,
     314              :     required this.topLit,
     315              :   });
     316              : 
     317              :   final Color color;
     318              :   final double radius;
     319              :   final bool topLit;
     320              : 
     321            4 :   @override
     322              :   void paint(Canvas canvas, Size size) {
     323            8 :     final rect = (Offset.zero & size).deflate(0.5);
     324            4 :     final shape = RSuperellipse.fromRectAndRadius(
     325              :       rect,
     326            8 :       Radius.circular(radius),
     327              :     );
     328            4 :     final shader = LinearGradient(
     329            4 :       begin: topLit ? Alignment.topCenter : Alignment.bottomCenter,
     330              :       end: Alignment.center,
     331           16 :       colors: [color, color.withValues(alpha: 0)],
     332            4 :     ).createShader(rect);
     333            4 :     final paint = Paint()
     334            4 :       ..shader = shader
     335            4 :       ..style = PaintingStyle.stroke
     336            4 :       ..strokeWidth = 1;
     337            4 :     canvas.drawRSuperellipse(shape, paint);
     338              :   }
     339              : 
     340            1 :   @override
     341              :   bool shouldRepaint(_RimPainter oldDelegate) =>
     342            3 :       oldDelegate.color != color ||
     343            3 :       oldDelegate.radius != radius ||
     344            3 :       oldDelegate.topLit != topLit;
     345              : }
     346              : 
     347              : class _Footer extends StatelessWidget {
     348            3 :   const _Footer({
     349              :     required this.variant,
     350              :     required this.padding,
     351              :     required this.actions,
     352              :   });
     353              : 
     354              :   final FossDialogFooterVariant variant;
     355              : 
     356              :   /// The footer inset, resolved with the slot seams by [resolveDialogInsets].
     357              :   final EdgeInsets padding;
     358              : 
     359              :   final List<Widget> actions;
     360              : 
     361            3 :   @override
     362              :   Widget build(BuildContext context) {
     363            3 :     final theme = context.fossTheme;
     364            3 :     final colors = theme.colors;
     365            6 :     final filled = variant == FossDialogFooterVariant.filled;
     366              : 
     367              :     // Actions always sit in a trailing-aligned row, never stacked. Each is
     368              :     // capped to the compact action height so the footer hugs the controls
     369              :     // instead of reserving each control's full tap-target box.
     370            3 :     final content = Row(
     371              :       mainAxisAlignment: MainAxisAlignment.end,
     372            6 :       spacing: theme.spacing(2),
     373            3 :       children: [
     374            3 :         for (final action in actions)
     375            3 :           ConstrainedBox(
     376              :             constraints: const BoxConstraints(maxHeight: _footerActionHeight),
     377              :             child: action,
     378              :           ),
     379              :       ],
     380              :     );
     381              : 
     382            3 :     return DecoratedBox(
     383            3 :       decoration: BoxDecoration(
     384              :         // The bar is the muted role at 72% of its own opacity; muted is already
     385              :         // translucent, so multiply rather than replace its alpha.
     386              :         color: filled
     387           15 :             ? colors.muted.withValues(alpha: colors.muted.a * 0.72)
     388              :             : null,
     389            9 :         border: filled ? Border(top: BorderSide(color: colors.border)) : null,
     390              :       ),
     391            6 :       child: Padding(padding: padding, child: content),
     392              :     );
     393              :   }
     394              : }
        

Generated by: LCOV version 2.0-1