Implementation
Widget buildRadioGroup(BuildContext context, OpenUiNode node) {
final theme = node.theme;
final label = node.props.string('label');
final f = _field(node, 'value', label);
final value = _plain(f.value);
final variant = switch (node.props.string('variant')) {
'pills' => 'pills',
'cards' => 'cards',
_ => 'list',
};
final choices = _choices(node.props.list('options'));
Widget option(_Choice o, FocusNode focus) {
final selected = o.value == value;
final text = Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
o.label,
style: _text(
theme,
size: variant == 'list' ? null : theme.fontSizeOf('RadioGroup', 15),
height: variant == 'list' ? null : 22 / 15,
weight: variant == 'cards' ? theme.typography.medium : null,
color: variant == 'pills' && selected ? theme.onInk : theme.ink,
),
),
if (o.description != null) ...[
const SizedBox(height: 2),
Text(
o.description!,
style: _text(
theme,
size: theme.fontSizeOf('RadioGroup.description', 14),
height: 21 / 14,
color: theme.muted,
),
),
],
],
);
return Semantics(
container: true,
inMutuallyExclusiveGroup: true,
checked: selected,
child: Press(
focusNode: focus,
radius: variant == 'cards'
? theme.radii.xl
: (variant == 'pills' ? 999 : 4),
onTap: (_) => f.set(o.value),
builder: (hovered, pressed) {
if (variant == 'list') {
return ConstrainedBox(
constraints: const BoxConstraints(minHeight: 28),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 17,
height: 17,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: selected ? theme.ink : Colors.transparent,
border: selected
? null
: Border.all(color: theme.muted, width: 1.5),
),
child: selected
? DecoratedBox(
decoration: BoxDecoration(
color: theme.onInk,
shape: BoxShape.circle,
),
child: const SizedBox.square(dimension: 6),
)
: null,
),
const SizedBox(width: 9),
Flexible(child: text),
],
),
);
}
final cards = variant == 'cards';
final ringWidth = cards && selected ? 2.0 : 1.0;
return AnimatedContainer(
duration: const Duration(milliseconds: 150),
curve: Curves.ease,
constraints: const BoxConstraints(minHeight: 42),
padding: cards
? EdgeInsets.symmetric(
vertical: 15 - ringWidth,
horizontal: 17 - ringWidth,
)
: const EdgeInsets.symmetric(vertical: 9, horizontal: 16),
decoration: BoxDecoration(
color: selected
? (cards ? theme.pressed : theme.ink)
: (hovered ? theme.hover : Colors.transparent),
borderRadius: BorderRadius.circular(cards ? theme.radii.xl : 999),
border: Border.all(
color: selected ? theme.ink : theme.border,
width: ringWidth,
),
),
child: text,
);
},
),
);
}
Widget options(List<FocusNode> nodes) => switch (variant) {
'pills' => Wrap(
spacing: theme.spacing.controlGap,
runSpacing: theme.spacing.controlGap,
children: [for (final (i, o) in choices.indexed) option(o, nodes[i])],
),
'cards' => LayoutBuilder(
builder: (context, box) {
final gap = theme.spacing.controlGap;
final columns = math.max(
1,
((box.maxWidth + gap) / (200 + gap)).floor(),
);
final width = (box.maxWidth - gap * (columns - 1)) / columns;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
for (var start = 0; start < choices.length; start += columns) ...[
if (start > 0) SizedBox(height: gap),
FlexRow(
gap: gap,
align: FlexAlign.stretch,
children: [
for (
var i = start;
i < math.min(start + columns, choices.length);
i++
)
FlexRowItem(
fixed: (_) => width,
child: option(choices[i], nodes[i]),
),
],
),
],
],
);
},
),
_ => Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
for (final (i, o) in choices.indexed) ...[
if (i > 0) const SizedBox(height: 2),
option(o, nodes[i]),
],
],
),
};
return Semantics(
container: true,
role: SemanticsRole.radioGroup,
label: label,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
if (_nonEmpty(label) != null) ...[
Text(label!, style: _labelStyle(theme)),
const SizedBox(height: 10),
],
_RadioKeys(
count: choices.length,
selected: choices.indexWhere((o) => o.value == value),
onSelect: (i) => f.set(choices[i].value),
builder: options,
),
],
),
);
}