showModernFormTimeRangePicker function Null safety

Future<TimeRange?> showModernFormTimeRangePicker(
  1. {TimeRange? initialValue}
)

Implementation

Future<TimeRange?> showModernFormTimeRangePicker(
    {TimeRange? initialValue}) async {
  BuildContext context = ModernFormGlobalContext.context!;

  TimeOfDay? start = await showTimePicker(
    context: context,
    initialTime:
        initialValue?.start != null ? initialValue!.start! : TimeOfDay.now(),
    builder: (BuildContext context, Widget? child) {
      return MediaQuery(
        data: MediaQuery.of(context).copyWith(
          alwaysUse24HourFormat: true,
        ),
        child: child!,
      );
    },
  );

  if (start != null) {
    TimeOfDay? end = await showTimePicker(
      context: context,
      initialTime:
          initialValue?.end != null ? initialValue!.end! : TimeOfDay.now(),
      builder: (BuildContext context, Widget? child) {
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(
            alwaysUse24HourFormat: true,
          ),
          child: child!,
        );
      },
    );

    if (end != null) {
      return TimeRange(start: start, end: end);
    }
  }

  return null;
}