buildDatePicker method
Implementation
@protected
Widget buildDatePicker(BuildParameters params) {
DateTime? value;
final props = params.props;
final pickDate = props["mode"] != "time";
final pickTime = props["mode"] != "date";
final formatSpec = props["format"] ??
{
"_type": pickDate && pickTime
? "KFormatterDateTime"
: pickDate
? "KFormatterDate"
: "KFormatterTime"
};
if (props["value"] == "now") {
value = DateTime.now();
} else {
value = tryParseDateTime(props["value"]);
}
final controller = props["controller"] ??
TextEditingController(
text: properties.build(Types.kFormatter.type, formatSpec,
argument: value));
props["controller"] = controller;
final alias = props["alias"] ?? params.id;
props["onSaved"] = (v) {
if (alias != null) {
params.state[alias] = tryParseDateTime(value);
}
};
DateTime firstDate;
if (props["firstDate"] == "now") {
firstDate = DateTime.now();
} else {
firstDate = DateTime(1900);
if (props["firstDate"] != null) {
firstDate = parseDateTime(props["lastDate"], defaultValue: firstDate);
}
}
DateTime lastDate;
if (props["lastDate"] == "now") {
lastDate = DateTime.now();
} else {
lastDate = DateTime(2100);
if (props["lastDate"] != null) {
lastDate = parseDateTime(props["lastDate"], defaultValue: lastDate);
}
}
if (!parseBool(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);
}