build method

Future<Duration?> build(
  1. BuildContext context,
  2. Duration? currentDuration
)

Build the picker.

context is passed BuildContext. currentDuration is passed the currently selected DateTime.

ピッカーのビルドを行ないます。

contextBuildContextが渡されます。currentDurationに現在選択されているDateTimeが渡されます。

Implementation

Future<Duration?> build(
  BuildContext context,
  Duration? currentDuration,
) async {
  assert(
    begin == null ||
        end == null ||
        (begin != null &&
            end != null &&
            begin!.inMicroseconds < end!.inMicroseconds),
    "[begin] must be before [end].",
  );
  Duration? res;
  final theme = Theme.of(context);
  final enableDays = (begin?.inDays ?? 0) > 0 || (end?.inDays ?? 0) > 0;
  final selectedDay = (currentDuration?.inDays ??
      defaultDuration?.inDays ??
      begin?.inDays ??
      end?.inDays ??
      0);
  final enableHours = (begin?.inHours ?? 0) > 0 || (end?.inHours ?? 0) > 0;
  final selectedHours = (currentDuration?.inHours.remainder(24) ??
      defaultDuration?.inHours.remainder(24) ??
      begin?.inHours.remainder(24) ??
      end?.inHours.remainder(24) ??
      0);
  final enableMinutes =
      (begin?.inMinutes ?? 0) > 0 || (end?.inMinutes ?? 0) > 0;
  final selectedMinutes = (currentDuration?.inMinutes.remainder(60) ??
      defaultDuration?.inMinutes.remainder(60) ??
      begin?.inMinutes.remainder(60) ??
      end?.inMinutes.remainder(60) ??
      0);
  final selectedSeconds = (currentDuration?.inSeconds.remainder(60) ??
      defaultDuration?.inSeconds.remainder(60) ??
      begin?.inSeconds.remainder(60) ??
      end?.inSeconds.remainder(60) ??
      0);

  List<_PickerItem<int>> pickerItems = <_PickerItem<int>>[];
  for (var s = begin?.inSeconds.remainder(60) ?? 0;
      s <= (end?.inSeconds.remainder(60) ?? 0);
      s++) {
    pickerItems.add(
      _PickerItem(
        text: Text(
          "$s$secondSuffix",
          style: TextStyle(
            color: color ?? theme.colorScheme.onSurface,
          ),
        ),
        value: s,
      ),
    );
  }
  if (enableMinutes) {
    final secondsPickerItems = List<_PickerItem<int>>.from(pickerItems);
    pickerItems = <_PickerItem<int>>[];
    for (var m = begin?.inMinutes.remainder(60) ?? 0;
        m <= (end?.inMinutes.remainder(60) ?? 0);
        m++) {
      pickerItems.add(
        _PickerItem(
          text: Text(
            "$m$minuteSuffix",
            style: TextStyle(
              color: color ?? theme.colorScheme.onSurface,
            ),
          ),
          value: m,
          children: secondsPickerItems,
        ),
      );
    }
    if (enableHours) {
      final minutesPickerItems = List<_PickerItem<int>>.from(pickerItems);
      pickerItems = <_PickerItem<int>>[];
      for (var h = begin?.inHours.remainder(24) ?? 0;
          h <= (end?.inHours.remainder(24) ?? 0);
          h++) {
        pickerItems.add(
          _PickerItem(
            text: Text(
              "$h$hourSuffix",
              style: TextStyle(
                color: color ?? theme.colorScheme.onSurface,
              ),
            ),
            value: h,
            children: minutesPickerItems,
          ),
        );
      }
      if (enableDays) {
        final hoursPickerItems = List<_PickerItem<int>>.from(pickerItems);
        pickerItems = <_PickerItem<int>>[];
        for (var d = begin?.inDays ?? 0; d <= (end?.inDays ?? 0); d++) {
          pickerItems.add(
            _PickerItem(
              text: Text(
                "$d$daySuffix",
                style: TextStyle(
                  color: color ?? theme.colorScheme.onSurface,
                ),
              ),
              value: d,
              children: hoursPickerItems,
            ),
          );
        }
      }
    }
  }
  await _Picker(
    height: 240,
    backgroundColor: backgroundColor ?? theme.colorScheme.surface,
    containerColor: backgroundColor ?? theme.colorScheme.surface,
    headerColor: backgroundColor ?? theme.colorScheme.surface,
    textStyle: TextStyle(color: color ?? theme.colorScheme.onSurface),
    confirmText: confirmText,
    cancelText: cancelText,
    selecteds: [
      if (enableDays) selectedDay,
      if (enableHours) selectedHours,
      if (enableMinutes) selectedMinutes,
      selectedSeconds,
    ],
    adapter: _PickerDataAdapter<int>(
      data: [
        ...pickerItems,
      ],
    ),
    changeToFirst: true,
    hideHeader: false,
    onConfirm: (_Picker picker, List<int> value) {
      if (value.length >= 4) {
        res = Duration(
          days: value[0],
          hours: value[1],
          minutes: value[2],
          seconds: value[3],
        );
      } else if (value.length == 3) {
        res = Duration(
          hours: value[0],
          minutes: value[1],
          seconds: value[2],
        );
      } else if (value.length == 2) {
        res = Duration(
          minutes: value[0],
          seconds: value[1],
        );
      } else if (value.length == 1) {
        res = Duration(
          seconds: value[0],
        );
      }
    },
  ).showModal(context);
  return res;
}