feat: enhance date range picker logic to restrict date selection and improve validation
This commit is contained in:
parent
49326e9929
commit
d05e26bc87
@ -31,16 +31,23 @@ class _DateRangePickerWidgetState extends State<DateRangePickerWidget>
|
|||||||
Future<void> _selectDate(BuildContext context, bool isStartDate) async {
|
Future<void> _selectDate(BuildContext context, bool isStartDate) async {
|
||||||
final current = isStartDate
|
final current = isStartDate
|
||||||
? widget.startDate.value ?? DateTime.now()
|
? 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 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(
|
final DateTime? picked = await showDatePicker(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: current,
|
initialDate: current.isBefore(firstDate)
|
||||||
firstDate: first,
|
? firstDate
|
||||||
|
: (current.isAfter(last) ? last : current),
|
||||||
|
firstDate: firstDate,
|
||||||
lastDate: last,
|
lastDate: last,
|
||||||
builder: (context, child) => Theme(
|
builder: (context, child) => Theme(
|
||||||
data: Theme.of(context).copyWith(
|
data: Theme.of(context).copyWith(
|
||||||
@ -64,11 +71,6 @@ class _DateRangePickerWidgetState extends State<DateRangePickerWidget>
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
widget.endDate.value = picked;
|
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
|
widget.onDateRangeSelected
|
||||||
|
|||||||
@ -215,9 +215,8 @@ class _AddServiceProjectJobBottomSheetState
|
|||||||
startLabel: "Start Date",
|
startLabel: "Start Date",
|
||||||
endLabel: "Due Date",
|
endLabel: "Due Date",
|
||||||
onDateRangeSelected: (start, end) {
|
onDateRangeSelected: (start, end) {
|
||||||
controller.startDate.value = start ?? DateTime.now();
|
controller.startDate.value;
|
||||||
controller.dueDate.value =
|
controller.dueDate.value;
|
||||||
end ?? DateTime.now().add(const Duration(days: 1));
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
MySpacing.height(16),
|
MySpacing.height(16),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user