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

            Line data    Source code
       1              : part of 'foss_checkbox.dart';
       2              : 
       3              : /// The visual treatment of a [FossCheckboxGroup].
       4              : enum FossCheckboxGroupVariant {
       5              :   /// Bare box with a label, options stacked in a column.
       6              :   plain,
       7              : 
       8              :   /// Each option wrapped in a full-width, selectable bordered card.
       9              :   card,
      10              : }
      11              : 
      12              : /// {@category Inputs}
      13              : /// Lays out a set of [FossCheckboxItem] options as a multi-select group.
      14              : ///
      15              : /// Holds the checked [values] and the [onChanged] callback and shares them with
      16              : /// its [children] through an inherited scope, so each [FossCheckboxItem] reads
      17              : /// its own checked state and reports taps back to the group. A tap adds or
      18              : /// removes the item's value and reports a new set, never mutating [values].
      19              : /// Renders an optional [label] above the options and an optional [errorText]
      20              : /// below.
      21              : ///
      22              : /// A non-null [errorText] marks every option invalid; `enabled: false` or a
      23              : /// null [onChanged] disables the whole group. Colors, type, and spacing come
      24              : /// from `context.fossTheme`.
      25              : ///
      26              : /// ```dart
      27              : /// FossCheckboxGroup<String>(
      28              : ///   label: 'Frameworks',
      29              : ///   values: selected,
      30              : ///   onChanged: (next) => setState(() => selected = next),
      31              : ///   children: const [
      32              : ///     FossCheckboxItem(value: 'next', label: 'Next.js'),
      33              : ///     FossCheckboxItem(value: 'vite', label: 'Vite'),
      34              : ///   ],
      35              : /// );
      36              : /// ```
      37              : class FossCheckboxGroup<T> extends StatelessWidget {
      38              :   /// Creates a checkbox group. [children] are the [FossCheckboxItem] options,
      39              :   /// each of the same value type [T].
      40            1 :   const FossCheckboxGroup({
      41              :     required this.children,
      42              :     this.values = const {},
      43              :     this.onChanged,
      44              :     this.label,
      45              :     this.errorText,
      46              :     this.variant = FossCheckboxGroupVariant.plain,
      47              :     this.enabled = true,
      48              :     super.key,
      49              :   });
      50              : 
      51              :   /// The options, each a [FossCheckboxItem] of value type [T].
      52              :   final List<Widget> children;
      53              : 
      54              :   /// The set of checked values.
      55              :   final Set<T> values;
      56              : 
      57              :   /// Called with the new set when an option is toggled. Null disables the
      58              :   /// group.
      59              :   final ValueChanged<Set<T>>? onChanged;
      60              : 
      61              :   /// Optional label rendered above the options.
      62              :   final String? label;
      63              : 
      64              :   /// Error caption below the options. A non-null value marks the group invalid.
      65              :   final String? errorText;
      66              : 
      67              :   /// The visual treatment. Defaults to [FossCheckboxGroupVariant.plain].
      68              :   final FossCheckboxGroupVariant variant;
      69              : 
      70              :   /// Whether the group accepts input. When false it dims and stops responding.
      71              :   final bool enabled;
      72              : 
      73            1 :   @override
      74              :   Widget build(BuildContext context) {
      75            1 :     final theme = context.fossTheme;
      76            1 :     final colors = theme.colors;
      77            2 :     final active = enabled && onChanged != null;
      78            1 :     final hasError = errorText != null;
      79              : 
      80            1 :     return _FossCheckboxGroupScope<T>(
      81            1 :       values: values,
      82            1 :       onChanged: onChanged,
      83              :       enabled: active,
      84              :       hasError: hasError,
      85            1 :       variant: variant,
      86            1 :       child: Column(
      87              :         crossAxisAlignment: CrossAxisAlignment.start,
      88            2 :         spacing: theme.spacing(2),
      89            1 :         children: [
      90            1 :           if (label case final text?)
      91            1 :             Opacity(
      92              :               opacity: active ? 1 : _disabledOpacity,
      93            1 :               child: Text(
      94              :                 text,
      95            4 :                 style: theme.typography.base.medium.copyWith(
      96            1 :                   color: colors.foreground,
      97              :                 ),
      98              :               ),
      99              :             ),
     100            1 :           Column(
     101              :             crossAxisAlignment: CrossAxisAlignment.start,
     102            2 :             spacing: theme.spacing(3),
     103            1 :             children: children,
     104              :           ),
     105            1 :           if (errorText case final text?)
     106            1 :             Semantics(
     107              :               liveRegion: true,
     108            1 :               child: Text(
     109              :                 text,
     110            3 :                 style: theme.typography.xs.copyWith(
     111            1 :                   color: colors.destructiveForeground,
     112              :                 ),
     113              :               ),
     114              :             ),
     115              :         ],
     116              :       ),
     117              :     );
     118              :   }
     119              : }
     120              : 
     121              : /// A single option within a [FossCheckboxGroup].
     122              : ///
     123              : /// Renders a square box with an optional [label] and [description]. Reads its
     124              : /// checked, enabled, and invalid state from the enclosing [FossCheckboxGroup]
     125              : /// and reports a tap by toggling [value] in the group's set. A bare box (no
     126              : /// [label]) is valid. Setting `enabled: false` disables this option; disabling
     127              : /// the group disables every option.
     128              : ///
     129              : /// Must be placed under a [FossCheckboxGroup] of the same value type [T]. For a
     130              : /// standalone toggle, use [FossCheckbox] instead.
     131              : ///
     132              : /// ```dart
     133              : /// FossCheckboxItem<String>(
     134              : ///   value: 'next',
     135              : ///   label: 'Next.js',
     136              : ///   description: 'React framework',
     137              : /// );
     138              : /// ```
     139              : class FossCheckboxItem<T> extends StatelessWidget {
     140              :   /// Creates a checkbox option. [value] identifies it within the group.
     141            1 :   const FossCheckboxItem({
     142              :     required this.value,
     143              :     this.label,
     144              :     this.description,
     145              :     this.enabled = true,
     146              :     this.style,
     147              :     super.key,
     148              :   });
     149              : 
     150              :   /// The value this option contributes to the group, compared by `==`.
     151              :   final T value;
     152              : 
     153              :   /// Optional title beside the box.
     154              :   final String? label;
     155              : 
     156              :   /// Optional secondary line below the [label].
     157              :   final String? description;
     158              : 
     159              :   /// Whether this option accepts input. Disabled when false or when the group
     160              :   /// is disabled.
     161              :   final bool enabled;
     162              : 
     163              :   /// Per-instance overrides layered on the theme-resolved style.
     164              :   final FossCheckboxStyle? style;
     165              : 
     166            1 :   @override
     167              :   Widget build(BuildContext context) {
     168            1 :     final group = _FossCheckboxGroupScope.of<T>(context);
     169              :     if (group == null) {
     170            1 :       throw FlutterError(
     171            1 :         'FossCheckboxItem<$T> must be placed inside a FossCheckboxGroup<$T>.',
     172              :       );
     173              :     }
     174              : 
     175            3 :     final checked = group.values.contains(value);
     176            2 :     final enabled = this.enabled && group.enabled;
     177              : 
     178            1 :     void toggle() {
     179            2 :       final next = Set<T>.of(group.values);
     180              :       if (checked) {
     181            2 :         next.remove(value);
     182              :       } else {
     183            2 :         next.add(value);
     184              :       }
     185            2 :       group.onChanged?.call(next);
     186              :     }
     187              : 
     188            1 :     return _FossCheckboxControl(
     189              :       checked: checked,
     190              :       indeterminate: false,
     191              :       enabled: enabled,
     192            1 :       hasError: group.hasError,
     193            2 :       card: group.variant == FossCheckboxGroupVariant.card,
     194              :       standalone: false,
     195            1 :       label: label,
     196            1 :       description: description,
     197            1 :       style: style,
     198              :       onToggle: enabled ? toggle : null,
     199              :     );
     200              :   }
     201              : }
     202              : 
     203              : /// Shares a [FossCheckboxGroup]'s selection state with its [FossCheckboxItem]
     204              : /// descendants. Read it with [_FossCheckboxGroupScope.of].
     205              : class _FossCheckboxGroupScope<T> extends InheritedWidget {
     206              :   /// Creates the scope. Provided by [FossCheckboxGroup]; not constructed
     207              :   /// directly.
     208            1 :   const _FossCheckboxGroupScope({
     209              :     required this.values,
     210              :     required this.onChanged,
     211              :     required this.enabled,
     212              :     required this.hasError,
     213              :     required this.variant,
     214              :     required super.child,
     215              :     super.key,
     216              :   });
     217              : 
     218              :   /// The group's checked values.
     219              :   final Set<T> values;
     220              : 
     221              :   /// The group's change callback, invoked with the new set on a toggle.
     222              :   final ValueChanged<Set<T>>? onChanged;
     223              : 
     224              :   /// Whether the group is interactive.
     225              :   final bool enabled;
     226              : 
     227              :   /// Whether the group is in its invalid state.
     228              :   final bool hasError;
     229              : 
     230              :   /// The group's visual treatment.
     231              :   final FossCheckboxGroupVariant variant;
     232              : 
     233              :   /// The nearest group scope of value type [T] above [context], or null when a
     234              :   /// [FossCheckboxItem] is used outside a [FossCheckboxGroup].
     235            1 :   static _FossCheckboxGroupScope<T>? of<T>(BuildContext context) =>
     236            1 :       context.dependOnInheritedWidgetOfExactType<_FossCheckboxGroupScope<T>>();
     237              : 
     238            1 :   @override
     239              :   bool updateShouldNotify(_FossCheckboxGroupScope<T> oldWidget) =>
     240            3 :       !setEquals(values, oldWidget.values) ||
     241            3 :       enabled != oldWidget.enabled ||
     242            3 :       hasError != oldWidget.hasError ||
     243            3 :       variant != oldWidget.variant ||
     244            3 :       onChanged != oldWidget.onChanged;
     245              : }
        

Generated by: LCOV version 2.0-1