buildDatePicker method

  1. @protected
Widget buildDatePicker(
  1. BuildParameters params
)

Implementation

@protected
Widget buildDatePicker(BuildParameters params) {
  DateTime? value;
  final pickDate = params.props["mode"] != "time";
  final pickTime = params.props["mode"] != "date";
  final formatSpec = params.props["format"] ??
      {
        "_type": pickDate && pickTime
            ? "KFormatterDateTime"
            : pickDate
                ? "KFormatterDate"
                : "KFormatterTime"
      };

  if (params.props["value"] == "now") {
    value = DateTime.now();
  } else {
    value = tryParseDateTime(params.props["value"]);
  }

  final controller = TextEditingController(
      text: properties.build(Types.kFormatter.type, formatSpec,
          argument: value));
  params.props["controller"] = controller;
  final alias = params.props["alias"] ?? params.id;
  params.props["onSaved"] = (v) {
    if (alias != null) {
      params.state[alias] = tryParseDateTime(value);
    }
  };

  DateTime firstDate;
  if (params.props["firstDate"] == "now") {
    firstDate = DateTime.now();
  } else {
    firstDate = DateTime(1900);
    if (params.props["firstDate"] != null) {
      firstDate =
          parseDateTime(params.props["lastDate"], defaultValue: firstDate);
    }
  }
  DateTime lastDate;
  if (params.props["lastDate"] == "now") {
    lastDate = DateTime.now();
  } else {
    lastDate = DateTime(2100);
    if (params.props["lastDate"] != null) {
      lastDate =
          parseDateTime(params.props["lastDate"], defaultValue: lastDate);
    }
  }

  if (!parseBool(params.props["readOnly"])) {
    params.actions["onTap"] = () async {
      final buildContext = params.context;
      final initialValue = value ?? DateTime.now();
      var newValue = value;
      if (pickDate) {
        final date = await showDatePicker(
            context: buildContext,
            initialDate: initialValue,
            firstDate: firstDate,
            lastDate: lastDate);
        if (date == null) {
          return;
        }
        newValue = date;
      }
      if (pickTime && buildContext.mounted) {
        final time = await showTimePicker(
            context: buildContext,
            initialTime: TimeOfDay.fromDateTime(initialValue));
        if (time == null) {
          return;
        }

        final dateValue = newValue ?? initialValue;
        newValue = DateTime(dateValue.year, dateValue.month, dateValue.day,
            time.hour, time.minute);
      }
      if (newValue != value) {
        value = newValue;
        controller.text = Types.kFormatter.build(formatSpec, argument: value);
      }
    };
  }

  return buildTextFormField(params);
}