buildDatePicker function

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

Implementation

Widget buildDatePicker(BuildContext context, OpenUiNode node) {
  final theme = node.theme;
  final label = node.props.string('label');
  final f = _field(node, 'value', label);
  final current = f.value is String ? _parseDate(f.value as String) : null;
  final min = _parseDate(node.props.string('min'));
  final max = _parseDate(node.props.string('max'));
  final style = _text(
    theme,
    size: theme.fontSizeOf('DatePicker', 16),
    height: 1.4,
    tabular: true,
  );
  final localizations = MaterialLocalizations.of(context);
  final shown = current == null
      ? localizations.dateHelpText
      : localizations.formatCompactDate(current);
  return _labeled(
    theme,
    label,
    Semantics(
      button: true,
      label: _nonEmpty(label) == null
          ? OpenUiLocalizations.of(context).date
          : null,
      excludeSemantics: _nonEmpty(label) == null,
      value: current == null ? null : _formatDate(current),
      child: Press(
        radius: theme.radii.md,
        onTap: (ctx) async {
          final first = min ?? DateTime(1900);
          final last = max != null && !max.isBefore(first)
              ? max
              : (first.isAfter(DateTime(2100)) ? first : DateTime(2100));
          final today = node.localNow;
          var initial = current ?? DateTime(today.year, today.month, today.day);
          if (initial.isBefore(first)) initial = first;
          if (initial.isAfter(last)) initial = last;
          if (Theme.of(ctx).platform == TargetPlatform.iOS) {
            await showCupertinoModalPopup<void>(
              context: ctx,
              builder: (popup) => _cupertinoThemed(
                theme,
                Container(
                  height: 260,
                  color: theme.canvas,
                  child: SafeArea(
                    top: false,
                    child: Column(
                      children: [
                        Align(
                          alignment: AlignmentDirectional.centerEnd,
                          child: CupertinoButton(
                            onPressed: () => Navigator.pop(popup),
                            child: Text(OpenUiLocalizations.of(ctx).done),
                          ),
                        ),
                        Expanded(
                          child: CupertinoDatePicker(
                            mode: CupertinoDatePickerMode.date,
                            initialDateTime: initial,
                            minimumDate: first,
                            maximumDate: last,
                            onDateTimeChanged: (d) => f.set(_formatDate(d)),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            );
            return;
          }
          final picked = await showDatePicker(
            context: ctx,
            initialDate: initial,
            firstDate: first,
            lastDate: last,
            builder: (_, child) =>
                Theme(data: _materialTheme(theme), child: child!),
          );
          if (picked != null) f.set(_formatDate(picked));
        },
        builder: (hovered, pressed) => Container(
          constraints: const BoxConstraints(minHeight: 42),
          padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 12),
          decoration: BoxDecoration(
            color: theme.canvas,
            borderRadius: BorderRadius.circular(theme.radii.md),
            border: Border.all(color: theme.borderStrong),
          ),
          child: Row(
            children: [
              Expanded(
                child: Text(
                  shown,
                  style: current == null
                      ? style.copyWith(color: theme.faint)
                      : style,
                ),
              ),
              Opacity(
                opacity: 0.55,
                child: Glyph('calendar', size: 16, color: theme.ink),
              ),
            ],
          ),
        ),
      ),
    ),
  );
}