Line data Source code
1 : part of 'foss_radio.dart';
2 :
3 : /// The visual treatment of a [FossRadioGroup].
4 : enum FossRadioGroupVariant {
5 : /// Bare circle 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 [FossRadio] options as a single-choice group.
14 : ///
15 : /// Holds the selected [groupValue] and the [onChanged] callback and shares them
16 : /// with its [children] through an inherited scope, so each [FossRadio] reads
17 : /// its own checked state and reports taps back to the group. Renders an
18 : /// optional [label] above the options and an optional [errorText] below.
19 : ///
20 : /// A non-null [errorText] marks every option invalid; `enabled: false` or a
21 : /// null [onChanged] disables the whole group. Colors, type, and spacing come
22 : /// from `context.fossTheme`.
23 : ///
24 : /// ```dart
25 : /// FossRadioGroup<String>(
26 : /// label: 'Billing plan',
27 : /// groupValue: plan,
28 : /// onChanged: (value) => setState(() => plan = value),
29 : /// children: const [
30 : /// FossRadio(value: 'monthly', label: 'Monthly'),
31 : /// FossRadio(value: 'yearly', label: 'Yearly', description: 'Save 16%'),
32 : /// ],
33 : /// );
34 : /// ```
35 : class FossRadioGroup<T> extends StatelessWidget {
36 : /// Creates a radio group. [children] are the [FossRadio] options, each of the
37 : /// same value type [T].
38 1 : const FossRadioGroup({
39 : required this.children,
40 : this.groupValue,
41 : this.onChanged,
42 : this.label,
43 : this.errorText,
44 : this.variant = FossRadioGroupVariant.plain,
45 : this.enabled = true,
46 : super.key,
47 : });
48 :
49 : /// The options, each a [FossRadio] of value type [T].
50 : final List<Widget> children;
51 :
52 : /// The selected value, or null when none is selected.
53 : final T? groupValue;
54 :
55 : /// Called with the tapped option's value. Null disables the group.
56 : final ValueChanged<T>? onChanged;
57 :
58 : /// Optional label rendered above the options.
59 : final String? label;
60 :
61 : /// Error caption below the options. A non-null value marks the group invalid.
62 : final String? errorText;
63 :
64 : /// The visual treatment. Defaults to [FossRadioGroupVariant.plain].
65 : final FossRadioGroupVariant variant;
66 :
67 : /// Whether the group accepts input. When false it dims and stops responding.
68 : final bool enabled;
69 :
70 1 : @override
71 : Widget build(BuildContext context) {
72 1 : final theme = context.fossTheme;
73 1 : final colors = theme.colors;
74 2 : final active = enabled && onChanged != null;
75 1 : final hasError = errorText != null;
76 :
77 : // RadioGroup owns the selection, the roving arrow-key focus, and the
78 : // radiogroup semantics role; the scope carries the enabled, error, and
79 : // variant state each option reads.
80 1 : return RadioGroup<T>(
81 1 : groupValue: groupValue,
82 : // RadioGroup requires a callback; the group disables per option through
83 : // the scope's [enabled], so no option is interactive when inactive.
84 1 : onChanged: (value) {
85 2 : if (value != null) onChanged?.call(value);
86 : },
87 1 : child: _FossRadioGroupScope<T>(
88 : enabled: active,
89 : hasError: hasError,
90 1 : variant: variant,
91 1 : child: Column(
92 : crossAxisAlignment: CrossAxisAlignment.start,
93 2 : spacing: theme.spacing(2),
94 1 : children: [
95 1 : if (label case final text?)
96 1 : Opacity(
97 : opacity: active ? 1 : _disabledOpacity,
98 1 : child: Text(
99 : text,
100 4 : style: theme.typography.base.medium.copyWith(
101 1 : color: colors.foreground,
102 : ),
103 : ),
104 : ),
105 1 : Column(
106 : crossAxisAlignment: CrossAxisAlignment.start,
107 2 : spacing: theme.spacing(3),
108 1 : children: children,
109 : ),
110 1 : if (errorText case final text?)
111 1 : Semantics(
112 : liveRegion: true,
113 1 : child: Text(
114 : text,
115 3 : style: theme.typography.xs.copyWith(
116 1 : color: colors.destructiveForeground,
117 : ),
118 : ),
119 : ),
120 : ],
121 : ),
122 : ),
123 : );
124 : }
125 : }
126 :
127 : /// Shares a [FossRadioGroup]'s enabled, error, and variant state with its
128 : /// [FossRadio] descendants. Selection and focus flow through the enclosing
129 : /// [RadioGroup]; read this scope with [_FossRadioGroupScope.of].
130 : class _FossRadioGroupScope<T> extends InheritedWidget {
131 : /// Creates the scope. Provided by [FossRadioGroup]; not constructed directly.
132 1 : const _FossRadioGroupScope({
133 : required this.enabled,
134 : required this.hasError,
135 : required this.variant,
136 : required super.child,
137 : super.key,
138 : });
139 :
140 : /// Whether the group is interactive.
141 : final bool enabled;
142 :
143 : /// Whether the group is in its invalid state.
144 : final bool hasError;
145 :
146 : /// The group's visual treatment.
147 : final FossRadioGroupVariant variant;
148 :
149 : /// The nearest group scope of value type [T] above [context], or null when a
150 : /// [FossRadio] is used outside a [FossRadioGroup].
151 1 : static _FossRadioGroupScope<T>? of<T>(BuildContext context) =>
152 1 : context.dependOnInheritedWidgetOfExactType<_FossRadioGroupScope<T>>();
153 :
154 1 : @override
155 : bool updateShouldNotify(_FossRadioGroupScope<T> oldWidget) =>
156 3 : enabled != oldWidget.enabled ||
157 3 : hasError != oldWidget.hasError ||
158 3 : variant != oldWidget.variant;
159 : }
|