feat: enhance date range picker logic to restrict date selection and improve validation

This commit is contained in:
Vaibhav Surve 2025-11-19 12:19:47 +05:30
parent 49326e9929
commit d05e26bc87
2 changed files with 14 additions and 13 deletions

View File

@ -31,16 +31,23 @@ class _DateRangePickerWidgetState extends State<DateRangePickerWidget>
Future<void> _selectDate(BuildContext context, bool isStartDate) async {
final current = isStartDate
? widget.startDate.value ?? DateTime.now()
: widget.endDate.value ?? DateTime.now();
: widget.endDate.value ?? DateTime.now().add(const Duration(days: 1));
// Ensure initialDate is within firstDate..lastDate
final first = DateTime(2000);
final last = current.isAfter(DateTime.now()) ? current : DateTime.now();
// Restrict Start Date: allow today or earlier
// Restrict End Date: cannot be before startDate
final last = isStartDate ? DateTime.now() : DateTime(2100);
final firstDate = isStartDate
? first
: (widget.startDate.value != null ? widget.startDate.value! : first);
final DateTime? picked = await showDatePicker(
context: context,
initialDate: current,
firstDate: first,
initialDate: current.isBefore(firstDate)
? firstDate
: (current.isAfter(last) ? last : current),
firstDate: firstDate,
lastDate: last,
builder: (context, child) => Theme(
data: Theme.of(context).copyWith(
@ -64,11 +71,6 @@ class _DateRangePickerWidgetState extends State<DateRangePickerWidget>
}
} else {
widget.endDate.value = picked;
// Auto-adjust startDate if needed
if (widget.startDate.value != null &&
widget.startDate.value!.isAfter(picked)) {
widget.startDate.value = picked;
}
}
widget.onDateRangeSelected

View File

@ -215,9 +215,8 @@ class _AddServiceProjectJobBottomSheetState
startLabel: "Start Date",
endLabel: "Due Date",
onDateRangeSelected: (start, end) {
controller.startDate.value = start ?? DateTime.now();
controller.dueDate.value =
end ?? DateTime.now().add(const Duration(days: 1));
controller.startDate.value;
controller.dueDate.value;
},
),
MySpacing.height(16),