dateSelect static method

Future<DateTime?> dateSelect(
  1. BuildContext context, {
  2. DateTime? selectedDate,
  3. DateTime? firstDate,
  4. DateTime? lastDate,
  5. DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
  6. DatePickerMode initialDatePickerMode = DatePickerMode.day,
  7. DateFiltering? dateFiltering,
  8. String helpText = "Select date",
  9. String cancelText = "Cancel",
  10. String confirmText = "Confirm",
  11. String invalidText = "Enter a valid date",
  12. String fieldLabelText = "Date",
  13. String fieldHintText = "yyyy/mm/dd",
  14. DateSelectListener? selectListener,
})

Implementation

static Future<DateTime?> dateSelect(
    BuildContext context, {
      DateTime? selectedDate,
      DateTime? firstDate,
      DateTime? lastDate,
      DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
      DatePickerMode initialDatePickerMode = DatePickerMode.day,
      DateFiltering? dateFiltering,
      String helpText = "Select date",
      String cancelText = "Cancel",
      String confirmText = "Confirm",
      String invalidText = "Enter a valid date",
      String fieldLabelText = "Date",
      String fieldHintText = "yyyy/mm/dd",
      DateSelectListener? selectListener}) async{


  log("NUIDTUtil -> Showing date picker");
  late Future<DateTime?> dateTime;

  DateTime initialDate;
  final mySelectedDate = selectedDate ?? DateTime.now();
  if(lastDate != null && mySelectedDate.isAfter(lastDate)){
    initialDate = lastDate;
  }
  else if(firstDate != null && firstDate.isAfter(mySelectedDate)){
    initialDate = firstDate;
  }
  else{
    initialDate = mySelectedDate;
  }

  if(Platform.isIOS){
    final datePicker = CupertinoDatePicker(
        onDateTimeChanged: (date) async{
          if(selectListener != null){
            selectListener(date);
          }
          dateTime = NUIAsync.delayForResult(Duration(milliseconds: 100), () async{
            return date;
          });
        },
        initialDateTime: mySelectedDate,
        minimumDate: firstDate ?? DateTime(DateTime.now().year - 100),
        maximumDate: lastDate ?? DateTime(DateTime.now().year + 100),
        mode: CupertinoDatePickerMode.date,
    );
    showNUIBottomSheet(context: context, content: (context){
      return Container(
        height: screenHeight(context)/3,
        color: NUIColors.NUIWhite,
        child: datePicker,
      );
    });
  }
  else {
    dateTime = showDatePicker(
        context: context,
        initialDate: initialDate,
        firstDate: firstDate ?? DateTime(DateTime.now().year - 100),
        lastDate: lastDate ?? DateTime(DateTime.now().year + 100),
        initialEntryMode: initialEntryMode,
        initialDatePickerMode: initialDatePickerMode,
        selectableDayPredicate: dateFiltering,
        helpText: helpText,
        confirmText: confirmText,
        cancelText: cancelText,
        errorFormatText: "Enter a valid date",
        errorInvalidText: invalidText,
        fieldLabelText: fieldLabelText,
        fieldHintText: fieldHintText,
    );
  }

  final dateResult = await dateTime;
  if(selectListener != null){
    selectListener(dateResult);
  }
  return dateResult;
}