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

            Line data    Source code
       1              : import 'dart:async';
       2              : import 'dart:math' as math;
       3              : 
       4              : import 'package:flutter/semantics.dart' show SemanticsRole;
       5              : import 'package:flutter/services.dart';
       6              : import 'package:flutter/widgets.dart';
       7              : import 'package:fossui/src/components/combobox/foss_combobox.dart';
       8              : import 'package:fossui/src/icons/foss_glyph.dart';
       9              : import 'package:fossui/src/theme/theme.dart';
      10              : 
      11              : part '_foss_select_listbox.dart';
      12              : part 'foss_multi_select.dart';
      13              : part 'foss_select_item.dart';
      14              : part 'foss_select_style.dart';
      15              : 
      16              : const double _disabledOpacity = 0.64;
      17              : const double _placeholderOpacity = 0.72;
      18              : const double _darkFillOpacity = 0.32;
      19              : const double _minWidth = 144;
      20              : const double _minTapTarget = 48;
      21              : const double _indicatorColumn = 16;
      22              : const double _iconSize = 18;
      23              : 
      24              : const double _ringWidth = 3;
      25              : const double _ringOffset = 1;
      26              : 
      27              : const double _popupOffset = 4;
      28              : const double _popupMargin = 8;
      29              : const double _rowMinHeight = 32;
      30              : const double _openScale = 0.96;
      31              : 
      32              : // Error border and ring alphas: the border deepens when the trigger is focused,
      33              : // the ring lifts in dark mode.
      34              : const double _errorBorderOpacity = 0.36;
      35              : const double _errorBorderFocusedOpacity = 0.64;
      36              : const double _errorRingOpacityLight = 0.16;
      37              : const double _errorRingOpacityDark = 0.24;
      38              : const double _focusRingOpacity = 0.24;
      39              : 
      40              : // Inner top-lit rim at rest: a faint dark line in light mode, a faint white
      41              : // highlight in dark mode.
      42              : const Color _rimLight = Color(0x0A000000);
      43              : const Color _rimDark = Color(0x0FFFFFFF);
      44              : 
      45              : /// {@category Inputs}
      46              : /// {@template foss.select.preview}
      47              : /// <img src="https://fossui.org/components/select/overview/light.png"
      48              : ///   alt="FossSelect, light theme" width="480"
      49              : ///   style="max-width:100%;height:auto" />
      50              : /// <img src="https://fossui.org/components/select/overview/dark.png"
      51              : ///   alt="FossSelect, dark theme" width="480"
      52              : ///   style="max-width:100%;height:auto" />
      53              : ///
      54              : /// See the [select documentation ↗](https://fossui.org/docs/components/select) or
      55              : /// try it live in the
      56              : /// [playground ↗](https://play.fossui.org/components/#/?path=components/select/fossselect/playground).
      57              : /// {@endtemplate}
      58              : ///
      59              : /// A pick-from-list control with no typing.
      60              : ///
      61              : /// The trigger shows the current value or a [placeholder]; tapping it opens an
      62              : /// anchored popup listing the [items], and picking a row reports its value
      63              : /// through [onChanged] and closes the popup. Selection is controlled: pass
      64              : /// [value] and rebuild on [onChanged]. A null [onChanged] (or `enabled: false`)
      65              : /// disables the field.
      66              : ///
      67              : /// Colors, type, and metrics come from `context.fossTheme`; pass a
      68              : /// [FossSelectStyle] to [style] for a one-off. For multiple selection without
      69              : /// typing, use [FossMultiSelect].
      70              : ///
      71              : /// {@macro foss.customize}
      72              : ///
      73              : /// See also [FossMultiSelect] for multiple selection and [FossCombobox] to
      74              : /// filter by typing.
      75              : ///
      76              : /// ```dart
      77              : /// FossSelect<String>(
      78              : ///   value: plan,
      79              : ///   placeholder: 'Choose a plan',
      80              : ///   onChanged: (v) => setState(() => plan = v),
      81              : ///   items: const [
      82              : ///     FossSelectItem(value: 'monthly', label: 'Monthly'),
      83              : ///     FossSelectItem(value: 'yearly', label: 'Yearly'),
      84              : ///   ],
      85              : /// );
      86              : /// ```
      87              : class FossSelect<T> extends StatelessWidget {
      88              :   /// {@macro foss.select.preview}
      89              :   ///
      90              :   /// Creates a single-select control over [items].
      91            7 :   const FossSelect({
      92              :     required this.items,
      93              :     this.value,
      94              :     this.onChanged,
      95              :     this.placeholder,
      96              :     this.label,
      97              :     this.errorText,
      98              :     this.size = FossSelectSize.md,
      99              :     this.enabled = true,
     100              :     this.style,
     101              :     super.key,
     102              :   });
     103              : 
     104              :   /// The options to choose from.
     105              :   final List<FossSelectItem<T>> items;
     106              : 
     107              :   /// The picked value, or null when nothing is selected.
     108              :   final T? value;
     109              : 
     110              :   /// Called with the picked value when a row is chosen. A null callback
     111              :   /// disables the field. The field has no clear affordance, so this never
     112              :   /// fires with null.
     113              :   final ValueChanged<T>? onChanged;
     114              : 
     115              :   /// Text shown in the trigger when [value] is null.
     116              :   final String? placeholder;
     117              : 
     118              :   /// Optional label rendered above the trigger.
     119              :   final String? label;
     120              : 
     121              :   /// When non-null, marks the field invalid and recolors the trigger border.
     122              :   final String? errorText;
     123              : 
     124              :   /// The trigger height and type scale.
     125              :   final FossSelectSize size;
     126              : 
     127              :   /// Whether the field accepts input. Disabled when false or [onChanged] is
     128              :   /// null.
     129              :   final bool enabled;
     130              : 
     131              :   /// Per-instance overrides layered on the theme-resolved style.
     132              :   final FossSelectStyle? style;
     133              : 
     134            7 :   @override
     135              :   Widget build(BuildContext context) {
     136            7 :     return _FossSelectField<T>(
     137            7 :       items: items,
     138            7 :       size: size,
     139            7 :       style: style,
     140            7 :       label: label,
     141            7 :       placeholder: placeholder,
     142            7 :       errorText: errorText,
     143           14 :       enabled: enabled && onChanged != null,
     144            7 :       triggerLabel: _selectedLabel(),
     145              :       indicator: _SelectIndicator.checkmark,
     146           21 :       isSelected: (v) => v == value,
     147              :       closeOnPick: true,
     148            6 :       onPick: (v) => onChanged?.call(v),
     149              :     );
     150              :   }
     151              : 
     152            7 :   String? _selectedLabel() {
     153           14 :     for (final item in items) {
     154           23 :       if (item.value == value) return item.label;
     155              :     }
     156              :     return null;
     157              :   }
     158              : }
     159              : 
     160              : /// Builds the default trigger appearance from the theme tokens for [size].
     161            8 : _SelectVisuals _resolve(FossThemeData theme, FossSelectSize size) {
     162            8 :   final c = theme.colors;
     163              :   final (minHeight, padding, textStyle, gap) = switch (size) {
     164            8 :     FossSelectSize.sm => (
     165              :       32.0,
     166            3 :       EdgeInsets.symmetric(horizontal: theme.spacing(2.5)),
     167            2 :       theme.typography.sm,
     168            2 :       theme.spacing(1.5),
     169              :     ),
     170            8 :     FossSelectSize.md => (
     171              :       36.0,
     172           24 :       EdgeInsets.symmetric(horizontal: theme.spacing(3)),
     173           16 :       theme.typography.base,
     174           16 :       theme.spacing(2),
     175              :     ),
     176            1 :     FossSelectSize.lg => (
     177              :       40.0,
     178            3 :       EdgeInsets.symmetric(horizontal: theme.spacing(3)),
     179            2 :       theme.typography.base,
     180            2 :       theme.spacing(2),
     181              :     ),
     182              :   };
     183              : 
     184              :   // Dark lifts the resting trigger fill by the input color at 32% of its alpha,
     185              :   // composited to opaque. Light is the bare surface.
     186            8 :   final background = c.isDark
     187            1 :       ? Color.alphaBlend(
     188            5 :           c.input.withValues(alpha: c.input.a * _darkFillOpacity),
     189            1 :           c.background,
     190              :         )
     191            8 :       : c.background;
     192              : 
     193            8 :   return _SelectVisuals(
     194              :     background: background,
     195            8 :     foreground: c.foreground,
     196           16 :     placeholderColor: c.mutedForeground.withValues(
     197           24 :       alpha: c.mutedForeground.a * _placeholderOpacity,
     198              :     ),
     199            8 :     borderColor: c.input,
     200           16 :     borderRadius: theme.radii.lg,
     201           16 :     rowRadius: theme.radii.sm,
     202              :     padding: padding,
     203              :     minHeight: minHeight,
     204              :     textStyle: textStyle,
     205           16 :     shadow: theme.shadows.xs,
     206              :     iconSize: _iconSize,
     207              :     gap: gap,
     208            8 :     popupColor: c.popover,
     209            8 :     popupBorderColor: c.border,
     210           16 :     popupShadow: theme.shadows.lg,
     211            8 :     highlightColor: c.accent,
     212            8 :     highlightForeground: c.accentForeground,
     213              :   );
     214              : }
     215              : 
     216              : /// Lays the trigger-facing fields of [override] over the resolved [base]. The
     217              : /// popup and row roles stay token-driven.
     218            8 : _SelectVisuals _apply(_SelectVisuals base, FossSelectStyle? override) {
     219              :   if (override == null) return base;
     220            1 :   return base.copyWith(
     221            1 :     background: override.backgroundColor,
     222            1 :     foreground: override.foregroundColor,
     223            1 :     placeholderColor: override.placeholderColor,
     224            1 :     borderColor: override.borderColor,
     225            1 :     borderRadius: override.borderRadius,
     226            1 :     padding: override.padding,
     227            1 :     minHeight: override.minHeight,
     228            1 :     textStyle: override.textStyle,
     229            1 :     shadow: override.shadow,
     230            1 :     iconSize: override.iconSize,
     231            1 :     gap: override.gap,
     232              :   );
     233              : }
     234              : 
     235              : /// The fully resolved, non-null trigger, popup, and row appearance.
     236              : @immutable
     237              : class _SelectVisuals {
     238            8 :   const _SelectVisuals({
     239              :     required this.background,
     240              :     required this.foreground,
     241              :     required this.placeholderColor,
     242              :     required this.borderColor,
     243              :     required this.borderRadius,
     244              :     required this.rowRadius,
     245              :     required this.padding,
     246              :     required this.minHeight,
     247              :     required this.textStyle,
     248              :     required this.shadow,
     249              :     required this.iconSize,
     250              :     required this.gap,
     251              :     required this.popupColor,
     252              :     required this.popupBorderColor,
     253              :     required this.popupShadow,
     254              :     required this.highlightColor,
     255              :     required this.highlightForeground,
     256              :   });
     257              : 
     258              :   final Color background;
     259              :   final Color foreground;
     260              :   final Color placeholderColor;
     261              :   final Color borderColor;
     262              :   final double borderRadius;
     263              :   final double rowRadius;
     264              :   final EdgeInsetsGeometry padding;
     265              :   final double minHeight;
     266              :   final TextStyle textStyle;
     267              :   final List<BoxShadow> shadow;
     268              :   final double iconSize;
     269              :   final double gap;
     270              :   final Color popupColor;
     271              :   final Color popupBorderColor;
     272              :   final List<BoxShadow> popupShadow;
     273              :   final Color highlightColor;
     274              :   final Color highlightForeground;
     275              : 
     276            1 :   _SelectVisuals copyWith({
     277              :     Color? background,
     278              :     Color? foreground,
     279              :     Color? placeholderColor,
     280              :     Color? borderColor,
     281              :     double? borderRadius,
     282              :     EdgeInsetsGeometry? padding,
     283              :     double? minHeight,
     284              :     TextStyle? textStyle,
     285              :     List<BoxShadow>? shadow,
     286              :     double? iconSize,
     287              :     double? gap,
     288            1 :   }) => _SelectVisuals(
     289            1 :     background: background ?? this.background,
     290            1 :     foreground: foreground ?? this.foreground,
     291            1 :     placeholderColor: placeholderColor ?? this.placeholderColor,
     292            1 :     borderColor: borderColor ?? this.borderColor,
     293            1 :     borderRadius: borderRadius ?? this.borderRadius,
     294            1 :     rowRadius: rowRadius,
     295            1 :     padding: padding ?? this.padding,
     296            1 :     minHeight: minHeight ?? this.minHeight,
     297            1 :     textStyle: textStyle ?? this.textStyle,
     298            1 :     shadow: shadow ?? this.shadow,
     299            1 :     iconSize: iconSize ?? this.iconSize,
     300            1 :     gap: gap ?? this.gap,
     301            1 :     popupColor: popupColor,
     302            1 :     popupBorderColor: popupBorderColor,
     303            1 :     popupShadow: popupShadow,
     304            1 :     highlightColor: highlightColor,
     305            1 :     highlightForeground: highlightForeground,
     306              :   );
     307              : }
        

Generated by: LCOV version 2.0-1