buildSelect function

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

Implementation

Widget buildSelect(BuildContext context, OpenUiNode node) {
  final theme = node.theme;
  final label = node.props.string('label');
  final f = _field(node, 'value', label);
  final choices = _choices(node.props.list('options'));
  final value = _plain(f.value);
  final selected = choices.indexWhere((o) => o.value == value);
  final style = _text(theme, size: theme.fontSizeOf('Select', 16), height: 1.4);
  return _labeled(
    theme,
    label,
    Container(
      constraints: const BoxConstraints(minHeight: 42),
      alignment: AlignmentDirectional.centerStart,
      padding: const EdgeInsetsDirectional.only(start: 12, end: 12),
      decoration: BoxDecoration(
        color: theme.canvas,
        borderRadius: BorderRadius.circular(theme.radii.md),
        border: Border.all(color: theme.borderStrong),
      ),
      child: Material(
        type: MaterialType.transparency,
        child: DropdownButtonHideUnderline(
          child: DropdownButton<int>(
            isExpanded: true,
            isDense: true,
            value: selected < 0 ? null : selected,
            hint: Text(
              node.props.string('placeholder') ??
                  OpenUiLocalizations.of(context).choose,
              style: style.copyWith(color: theme.faint),
            ),
            style: style,
            dropdownColor: theme.canvas,
            borderRadius: BorderRadius.circular(theme.radii.md),
            icon: Glyph(
              'chevron-down',
              size: 16,
              strokeWidth: 2.4,
              color: theme.chevron,
            ),
            items: [
              for (final (i, o) in choices.indexed)
                DropdownMenuItem(value: i, child: Text(o.label)),
            ],
            onChanged: (i) => f.set(choices[i!].value),
          ),
        ),
      ),
    ),
  );
}