LCOV - code coverage report
Current view: top level - foundation - foss_field_box.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 63 63
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              : 
       5              : const double _ringWidth = 3;
       6              : const double _minTapTarget = 48;
       7              : const double _disabledOpacity = 0.64;
       8              : 
       9              : // Focus ring alpha, and the error border and ring alphas: the border deepens
      10              : // when the field is focused, the ring stays faint and lifts in dark mode.
      11              : const double _focusRingOpacity = 0.24;
      12              : const double _errorBorderOpacity = 0.36;
      13              : const double _errorBorderFocusedOpacity = 0.64;
      14              : const double _errorRingOpacityLight = 0.16;
      15              : const double _errorRingOpacityDark = 0.24;
      16              : 
      17              : // Inner top-lit rim at rest: a faint dark line in light mode, a faint white
      18              : // highlight in dark mode.
      19              : const Color _rimLight = Color(0x0A000000);
      20              : const Color _rimDark = Color(0x0FFFFFFF);
      21              : 
      22              : /// The shared field surface: resting fill, border, focus ring, resting rim and
      23              : /// shadow, a minimum height, and a touch target. Every field-shaped control
      24              : /// wraps its content in one of these so their chrome stays identical.
      25              : ///
      26              : /// The caller owns the content and its padding, and adds any tap or selection
      27              : /// gesture around this box. The border, ring, rim, and shadow are derived from
      28              : /// [enabled], [hasError], and [focused]; the box is dimmed when disabled and is
      29              : /// centered in a region at least 48 logical pixels tall for the touch target.
      30              : class FossFieldBox extends StatelessWidget {
      31              :   /// Creates a field surface around [child].
      32            8 :   const FossFieldBox({
      33              :     required this.enabled,
      34              :     required this.hasError,
      35              :     required this.focused,
      36              :     required this.background,
      37              :     required this.borderColor,
      38              :     required this.ringColor,
      39              :     required this.destructiveColor,
      40              :     required this.borderRadius,
      41              :     required this.minHeight,
      42              :     required this.shadow,
      43              :     required this.isDark,
      44              :     required this.child,
      45              :     super.key,
      46              :   });
      47              : 
      48              :   /// Whether the field accepts input. When false the box dims.
      49              :   final bool enabled;
      50              : 
      51              :   /// Whether the field is invalid, which recolors the border and ring.
      52              :   final bool hasError;
      53              : 
      54              :   /// Whether the field holds focus, which shows the border and focus ring.
      55              :   final bool focused;
      56              : 
      57              :   /// The resting fill behind the content.
      58              :   final Color background;
      59              : 
      60              :   /// The border color while resting and valid.
      61              :   final Color borderColor;
      62              : 
      63              :   /// The focus accent, used for the border and ring while focused and valid.
      64              :   final Color ringColor;
      65              : 
      66              :   /// The invalid accent, used for the border and ring while [hasError].
      67              :   final Color destructiveColor;
      68              : 
      69              :   /// The corner radius of the surface and its ring.
      70              :   final double borderRadius;
      71              : 
      72              :   /// The minimum content height before the touch-target inflation.
      73              :   final double minHeight;
      74              : 
      75              :   /// The resting drop shadow, dropped whenever focused, invalid, or disabled.
      76              :   final List<BoxShadow> shadow;
      77              : 
      78              :   /// Whether the surrounding theme is dark, which tunes the rim and error ring.
      79              :   final bool isDark;
      80              : 
      81              :   /// The field content. The caller pads it; this box only frames it.
      82              :   final Widget child;
      83              : 
      84            8 :   @override
      85              :   Widget build(BuildContext context) {
      86              :     final Color border;
      87              :     final Color? ring;
      88            8 :     if (hasError) {
      89           10 :       border = destructiveColor.withValues(
      90            5 :         alpha: focused ? _errorBorderFocusedOpacity : _errorBorderOpacity,
      91              :       );
      92            5 :       ring = focused
      93            4 :           ? destructiveColor.withValues(
      94            2 :               alpha: isDark ? _errorRingOpacityDark : _errorRingOpacityLight,
      95              :             )
      96              :           : null;
      97            8 :     } else if (focused) {
      98            7 :       border = ringColor;
      99           14 :       ring = ringColor.withValues(alpha: _focusRingOpacity);
     100              :     } else {
     101            8 :       border = borderColor;
     102              :       ring = null;
     103              :     }
     104              : 
     105           24 :     final showShadow = enabled && !focused && !hasError;
     106              : 
     107            8 :     Widget content = DecoratedBox(
     108            8 :       decoration: ShapeDecoration(
     109            8 :         color: background,
     110            8 :         shape: RoundedSuperellipseBorder(
     111            8 :           side: BorderSide(color: border),
     112           16 :           borderRadius: BorderRadius.circular(borderRadius),
     113              :         ),
     114            8 :         shadows: showShadow ? shadow : const [],
     115              :       ),
     116            8 :       child: ConstrainedBox(
     117           16 :         constraints: BoxConstraints(minHeight: minHeight),
     118            8 :         child: child,
     119              :       ),
     120              :     );
     121              : 
     122              :     if (ring != null) {
     123            7 :       content = CustomPaint(
     124           14 :         foregroundPainter: _RingPainter(color: ring, radius: borderRadius),
     125              :         child: content,
     126              :       );
     127              :     }
     128              : 
     129              :     // At rest, a 1px inner rim; dropped under the same states as the shadow.
     130              :     // Dark lights the top edge with a white highlight, light darkens the bottom
     131              :     // edge. Rim radius is the inner edge (radius - 1).
     132              :     if (showShadow) {
     133            8 :       content = CustomPaint(
     134            8 :         foregroundPainter: _RimPainter(
     135            8 :           color: isDark ? _rimDark : _rimLight,
     136           24 :           radius: math.max(0, borderRadius - 1),
     137            8 :           topLit: isDark,
     138              :         ),
     139              :         child: content,
     140              :       );
     141              :     }
     142              : 
     143           15 :     if (!enabled) content = Opacity(opacity: _disabledOpacity, child: content);
     144              : 
     145              :     // Center the box in a region at least [_minTapTarget] tall so the field
     146              :     // meets the minimum touch-target guideline without inflating its height.
     147            8 :     return ConstrainedBox(
     148              :       constraints: const BoxConstraints(minHeight: _minTapTarget),
     149            8 :       child: Center(heightFactor: 1, child: content),
     150              :     );
     151              :   }
     152              : }
     153              : 
     154              : /// Paints a 1px rim inside the field: brightest along one edge, fading to
     155              : /// nothing by the middle. [topLit] lights the top edge; otherwise the bottom.
     156              : class _RimPainter extends CustomPainter {
     157            8 :   const _RimPainter({
     158              :     required this.color,
     159              :     required this.radius,
     160              :     required this.topLit,
     161              :   });
     162              : 
     163              :   final Color color;
     164              :   final double radius;
     165              :   final bool topLit;
     166              : 
     167            8 :   @override
     168              :   void paint(Canvas canvas, Size size) {
     169           16 :     final rect = (Offset.zero & size).deflate(0.5);
     170            8 :     final shape = RSuperellipse.fromRectAndRadius(
     171              :       rect,
     172           16 :       Radius.circular(radius),
     173              :     );
     174            8 :     final shader = LinearGradient(
     175            8 :       begin: topLit ? Alignment.topCenter : Alignment.bottomCenter,
     176              :       end: Alignment.center,
     177           32 :       colors: [color, color.withValues(alpha: 0)],
     178            8 :     ).createShader(rect);
     179            8 :     final paint = Paint()
     180            8 :       ..shader = shader
     181            8 :       ..style = PaintingStyle.stroke
     182            8 :       ..strokeWidth = 1;
     183            8 :     canvas.drawRSuperellipse(shape, paint);
     184              :   }
     185              : 
     186            6 :   @override
     187              :   bool shouldRepaint(_RimPainter oldDelegate) =>
     188           18 :       oldDelegate.color != color ||
     189           15 :       oldDelegate.radius != radius ||
     190           15 :       oldDelegate.topLit != topLit;
     191              : }
     192              : 
     193              : /// Paints the focus ring: a superellipse outset just past the field edge,
     194              : /// matching its corner shape so it reads smooth, not circular.
     195              : class _RingPainter extends CustomPainter {
     196            7 :   const _RingPainter({required this.color, required this.radius});
     197              : 
     198              :   final Color color;
     199              :   final double radius;
     200              : 
     201            7 :   @override
     202              :   void paint(Canvas canvas, Size size) {
     203           14 :     final rect = (Offset.zero & size).inflate(_ringWidth);
     204            7 :     final shape = RSuperellipse.fromRectAndRadius(
     205              :       rect,
     206           21 :       Radius.circular(radius + _ringWidth),
     207              :     );
     208            7 :     final paint = Paint()
     209           14 :       ..color = color
     210            7 :       ..style = PaintingStyle.stroke
     211            7 :       ..strokeWidth = _ringWidth;
     212            7 :     canvas.drawRSuperellipse(shape, paint);
     213              :   }
     214              : 
     215            7 :   @override
     216              :   bool shouldRepaint(_RingPainter oldDelegate) =>
     217           42 :       oldDelegate.color != color || oldDelegate.radius != radius;
     218              : }
        

Generated by: LCOV version 2.0-1