buildSegmentedControl function

Widget buildSegmentedControl(
  1. BuildContext context,
  2. OpenUiNode node
)

Implementation

Widget buildSegmentedControl(BuildContext context, OpenUiNode node) {
  final theme = node.theme;
  final f = _field(node, 'value', null);
  final value = _plain(f.value);
  final block = node.props.boolean('block') == true;
  final dark = theme.brightness == Brightness.dark;
  final compact = MediaQuery.sizeOf(context).width <= 640;
  Widget segment(_Choice o) {
    final selected = o.value == value;
    return Semantics(
      container: true,
      inMutuallyExclusiveGroup: true,
      checked: selected,
      child: Press(
        onTap: (_) => f.set(o.value),
        builder: (hovered, pressed) => AnimatedContainer(
          duration: _segmentFade,
          curve: Curves.ease,
          padding: EdgeInsets.symmetric(
            vertical: 6,
            horizontal: compact ? 11 : 14,
          ),
          decoration: BoxDecoration(
            color: selected
                ? (dark ? theme.surfaceStrong : theme.canvas)
                : Colors.transparent,
            borderRadius: BorderRadius.circular(999),
            border: selected ? Border.all(color: theme.border) : null,
            boxShadow: selected ? theme.shadowSmall : null,
          ),
          child: TweenAnimationBuilder<Color?>(
            tween: ColorTween(
              end: selected || hovered ? theme.ink : theme.muted,
            ),
            duration: _segmentFade,
            curve: Curves.ease,
            builder: (context, color, _) => Text(
              o.label,
              softWrap: false,
              style: _text(
                theme,
                size: theme.fontSizeOf('SegmentedControl', 13),
                height: 1.4,
                color: color,
                weight: selected ? theme.typography.medium : null,
              ),
            ),
          ),
        ),
      ),
    );
  }

  final choices = _choices(node.props.list('options'));
  final bar = Container(
    padding: const EdgeInsets.all(3),
    decoration: BoxDecoration(
      color: theme.surface,
      borderRadius: BorderRadius.circular(999),
    ),
    child: block
        ? FlexRow(
            gap: 2,
            children: [
              // A Stack measures its child at natural width, then centres it
              // in the share the row gives it.
              for (final o in choices)
                FlexRowItem(
                  grow: 1,
                  shrink: false,
                  child: Stack(
                    alignment: Alignment.center,
                    children: [segment(o)],
                  ),
                ),
            ],
          )
        : SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              spacing: 2,
              children: [for (final o in choices) segment(o)],
            ),
          ),
  );
  final group = Semantics(
    container: true,
    role: SemanticsRole.radioGroup,
    child: bar,
  );
  return block
      ? group
      : Align(
          alignment: AlignmentDirectional.centerStart,
          widthFactor: 1,
          heightFactor: 1,
          child: group,
        );
}