showModernFormDateTimePicker function Null safety

Future<DateTime?> showModernFormDateTimePicker(
  1. {DateTime? initialDate,
  2. bool? dateAndTime = true}
)

Implementation

Future<DateTime?> showModernFormDateTimePicker({
  DateTime? initialDate,
  bool? dateAndTime = true,
}) async {
  BuildContext context = ModernFormGlobalContext.context!;

  TargetPlatform platform = Theme.of(context).platform;
  // double breakpoint = 630;
  if (dateAndTime == null) {
    dateAndTime = true;
  }
  DateTime? dateTime;

  if (platform == TargetPlatform.iOS) {
    if (dateAndTime) {
      dateTime = await DatePicker.showDateTimePicker(context,
          locale: LocaleType.pt, currentTime: initialDate ?? DateTime.now());
    } else {
      dateTime = await DatePicker.showDatePicker(context,
          locale: LocaleType.pt, currentTime: initialDate ?? DateTime.now());
    }
  } else {
    DateTime? date = await showDatePicker(
      context: context,
      firstDate: DateTime.now().subtract(Duration(days: 25000)),
      initialDate: initialDate ?? DateTime.now(),
      fieldLabelText: "Selecione a data",
      helpText: "Selecione a data",
      lastDate: DateTime.now().add(
        Duration(days: 2000),
      ),
    );
    TimeOfDay? time;
    if (dateAndTime) {
      time = await showTimePicker(
        context: context,
        initialTime: initialDate != null
            ? TimeOfDay.fromDateTime(initialDate)
            : TimeOfDay.now(),
        builder: (BuildContext context, Widget? child) {
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(
              alwaysUse24HourFormat: true,
            ),
            child: child!,
          );
        },
      );
    }

    if (date != null) {
      dateTime = joinDateWithTime(date: date, time: time);
    }
  }
  return dateTime;
}