showCupertinoDatePicker function

void showCupertinoDatePicker(
  1. BuildContext context, {
  2. Key? key,
  3. CupertinoDatePickerMode mode = CupertinoDatePickerMode.dateAndTime,
  4. required void onDateTimeChanged(
    1. DateTime value
    ),
  5. DateTime? initialDateTime,
  6. DateTime? minimumDate,
  7. DateTime? maximumDate,
  8. int minimumYear = 1,
  9. int? maximumYear,
  10. int minuteInterval = 1,
  11. bool use24hFormat = false,
  12. Color? backgroundColor,
  13. ImageFilter? filter,
  14. bool useRootNavigator = true,
  15. bool? semanticsDismissible,
  16. Widget? cancelText,
  17. Widget? doneText,
  18. bool useText = false,
  19. bool leftHanded = false,
})

Display the Cupertino Date Picker

Implementation

void showCupertinoDatePicker(
  BuildContext context, {
  Key? key,
  CupertinoDatePickerMode mode = CupertinoDatePickerMode.dateAndTime,
  required void Function(DateTime value) onDateTimeChanged,
  DateTime? initialDateTime,
  DateTime? minimumDate,
  DateTime? maximumDate,
  int minimumYear = 1,
  int? maximumYear,
  int minuteInterval = 1,
  bool use24hFormat = false,
  Color? backgroundColor,
  ImageFilter? filter,
  bool useRootNavigator = true,
  bool? semanticsDismissible,
  Widget? cancelText,
  Widget? doneText,
  bool useText = false,
  bool leftHanded = false,
}) {
  // Default to right now.
  initialDateTime ??= DateTime.now();

  // Retrieve the current 'theme'
  final theme = CupertinoTheme.of(context);

  // Assign the spinner's background colour.
  backgroundColor ??= theme.scaffoldBackgroundColor;

  if (!useText) {
    cancelText = const Icon(CupertinoIcons.clear_circled);
  } else {
    cancelText ??= Text(
      'Cancel',
      style: theme.textTheme.actionTextStyle.copyWith(
        fontWeight: FontWeight.w600,
        color: Colors.red,
      ),
    );
  }

  if (!useText) {
    doneText = const Icon(CupertinoIcons.check_mark_circled);
  } else {
    doneText ??= Text(
      'Save',
      style:
          theme.textTheme.actionTextStyle.copyWith(fontWeight: FontWeight.w600),
    );
  }

  final cancelButton = CupertinoButton(
    padding: const EdgeInsets.symmetric(horizontal: 15),
    onPressed: () {
      onDateTimeChanged(DateTime(0000));
      Navigator.of(context).pop();
    },
    child: cancelText,
  );

  final doneButton = CupertinoButton(
    padding: const EdgeInsets.symmetric(horizontal: 15),
    onPressed: () => Navigator.of(context).pop(),
    child: doneText,
  );

  //
  showCupertinoModalPopup<void>(
    context: context,
    builder: (context) => SizedBox(
      height: 240,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Container(
            alignment: Alignment.centerRight,
            decoration: const BoxDecoration(
              color: Color.fromRGBO(249, 249, 247, 1),
              border: Border(
                bottom: BorderSide(width: 0.5, color: Colors.black38),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                if (leftHanded) doneButton else cancelButton,
                if (leftHanded) cancelButton else doneButton,
              ],
            ),
          ),
          Expanded(
              child: CupertinoDatePicker(
            key: key,
            mode: mode,
            onDateTimeChanged: (DateTime value) {
              if (mode == CupertinoDatePickerMode.time) {
                onDateTimeChanged(
                    DateTime(0000, 01, 01, value.hour, value.minute));
              } else {
                onDateTimeChanged(value);
              }
            },
            initialDateTime: initialDateTime,
            minimumDate: minimumDate,
            maximumDate: maximumDate,
            minimumYear: minimumYear,
            maximumYear: maximumYear,
            minuteInterval: minuteInterval,
            use24hFormat: use24hFormat,
            backgroundColor: backgroundColor,
          )),
        ],
      ),
    ),
    filter: filter,
    useRootNavigator: useRootNavigator,
    semanticsDismissible: semanticsDismissible,
  );
}