picker method

  1. @override
Future<DateTime> picker(
  1. BuildContext context,
  2. DateTime currentDateTime
)
override

Callback to display picker.

The currently selected DateTime is passed to currentDateTime.

ピッカーを表示するためのコールバック。

currentDateTimeに現在選択中のDateTimeが渡されます。

Implementation

@override
Future<DateTime> picker(
  BuildContext context,
  DateTime currentDateTime,
) async {
  final theme = Theme.of(context);
  final now = defaultDateTime ?? DateTime.now();
  final date = await showDatePicker(
    context: context,
    helpText: dateSelectorHelpText ?? helpText,
    cancelText: cancelText,
    confirmText: confirmText,
    locale: locale,
    initialDatePickerMode: initialDatePickerMode,
    errorFormatText: errorFormatText,
    errorInvalidText: errorInvalidText,
    fieldHintText: fieldHintText,
    fieldLabelText: fieldLabelText,
    firstDate: startDate ?? now.subtract(const Duration(days: 365)),
    initialDate: currentDateTime,
    lastDate: endDate ??
        now.add(
          const Duration(days: 365),
        ),
    builder: (context, child) {
      final colorScheme = theme.colorScheme;
      final datePickerTheme = theme.datePickerTheme;
      final textButtonTheme = theme.textButtonTheme;
      return Theme(
        data: theme.copyWith(
          colorScheme: colorScheme.copyWith(
            primary: primaryColor ?? colorScheme.primary,
            onPrimary: onPrimaryColor ?? colorScheme.onPrimary,
            surface: backgroundColor ?? colorScheme.surface,
            onSurface: foregroundColor ?? colorScheme.onSurface,
          ),
          datePickerTheme: datePickerTheme.copyWith(
            headerBackgroundColor: headerBackgroundColor ??
                datePickerTheme.headerBackgroundColor ??
                primaryColor ??
                colorScheme.primary,
            headerForegroundColor: headerForegroundColor ??
                datePickerTheme.headerForegroundColor ??
                onPrimaryColor ??
                colorScheme.onPrimary,
          ),
          textButtonTheme: TextButtonThemeData(
            style: textButtonTheme.style?.copyWith(
                  foregroundColor: textButtonTheme.style?.foregroundColor ??
                      WidgetStateProperty.all(
                        primaryColor ?? colorScheme.primary,
                      ),
                ) ??
                ButtonStyle(
                  foregroundColor: textButtonTheme.style?.foregroundColor ??
                      WidgetStateProperty.all(
                        primaryColor ?? colorScheme.primary,
                      ),
                ),
          ),
        ),
        child: child!,
      );
    },
  );
  if (date != null) {
    final time = await showTimePicker(
      context: context,
      helpText: timeSelectorHelpText ?? helpText,
      initialTime: TimeOfDay.fromDateTime(currentDateTime),
    );
    return _DateTimeTextField.combine(
      date,
      time ?? TimeOfDay.fromDateTime(now),
    );
  } else {
    return currentDateTime;
  }
}