viewToModelValue method

  1. @override
DateTime? viewToModelValue(
  1. DigitDOBAge? viewValue
)

Returns the value that must be supplied to the control.

Converts value from UI data type to control data type.

Implementation

@override
DateTime? viewToModelValue(DigitDOBAge? viewValue) {
  if (viewValue == null || (viewValue.years == 0 && viewValue.months == 0)) {
    return null;
  } else {
    final months = viewValue.months;
    final days = DigitDateUtils.yearsMonthsDaysToDays(
        viewValue.years, viewValue.months, viewValue.days);

    final calculatedDate = DateTime.now().subtract(Duration(days: days));

    return (viewValue.years == 0 && months == 0) || months > 11
        ? null
        : DateTime(
            calculatedDate.year,
            calculatedDate.month,
            1,
          );
  }
}