样式交互修改
This commit is contained in:
@@ -41,10 +41,10 @@ class C_ReservationController extends GetxController with BaseControllerMixin {
|
||||
|
||||
final DateTime _now = DateTime.now();
|
||||
|
||||
// 计算当前时间属于哪个半小时区间
|
||||
// 计算当前时间属于哪个1小时区间
|
||||
late final TimeOfDay _initialStartTime = _calculateInitialStartTime(_now);
|
||||
late final TimeOfDay _initialEndTime = TimeOfDay.fromDateTime(
|
||||
_getDateTimeFromTimeOfDay(_initialStartTime).add(const Duration(minutes: 30)),
|
||||
_getDateTimeFromTimeOfDay(_initialStartTime).add(const Duration(minutes: 60)),
|
||||
);
|
||||
late final Rx<DateTime> selectedDate = DateTime(_now.year, _now.month, _now.day).obs;
|
||||
late final Rx<TimeOfDay> startTime = _initialStartTime.obs;
|
||||
@@ -52,13 +52,7 @@ class C_ReservationController extends GetxController with BaseControllerMixin {
|
||||
|
||||
/// 静态辅助方法,用于计算初始的开始时间
|
||||
static TimeOfDay _calculateInitialStartTime(DateTime now) {
|
||||
if (now.minute < 30) {
|
||||
// 如果当前分钟小于30,则开始时间为当前小时的0分
|
||||
return TimeOfDay(hour: now.hour, minute: 0);
|
||||
} else {
|
||||
// 如果当前分钟大于等于30,则开始时间为当前小时的30分
|
||||
return TimeOfDay(hour: now.hour, minute: 30);
|
||||
}
|
||||
return TimeOfDay(hour: now.hour, minute: 0);
|
||||
}
|
||||
|
||||
/// 静态辅助方法,将TimeOfDay转换为DateTime
|
||||
@@ -186,30 +180,24 @@ class C_ReservationController extends GetxController with BaseControllerMixin {
|
||||
}
|
||||
}
|
||||
|
||||
///30 分钟为间隔 时间选择器
|
||||
///60 分钟为间隔 时间选择器
|
||||
void pickTime(BuildContext context) {
|
||||
final now = DateTime.now();
|
||||
final isToday =
|
||||
selectedDate.value.year == now.year &&
|
||||
selectedDate.value.month == now.month &&
|
||||
selectedDate.value.day == now.day;
|
||||
selectedDate.value.month == now.month &&
|
||||
selectedDate.value.day == now.day;
|
||||
|
||||
final List<TimeSlot> availableSlots = [];
|
||||
for (int i = 0; i < 48; i++) {
|
||||
final startMinutes = i * 30;
|
||||
final endMinutes = startMinutes + 30;
|
||||
for (int i = 0; i < 24; i++) {
|
||||
// 每次增加 60 分钟
|
||||
final startMinutes = i * 60;
|
||||
final endMinutes = startMinutes + 60;
|
||||
|
||||
final startTime = TimeOfDay(hour: startMinutes ~/ 60, minute: startMinutes % 60);
|
||||
// 注意:endMinutes % 60 始终为 0,因为间隔是整小时
|
||||
final endTime = TimeOfDay(hour: (endMinutes ~/ 60) % 24, minute: endMinutes % 60);
|
||||
|
||||
final slotStartDateTime = DateTime(
|
||||
selectedDate.value.year,
|
||||
selectedDate.value.month,
|
||||
selectedDate.value.day,
|
||||
startTime.hour,
|
||||
startTime.minute,
|
||||
);
|
||||
|
||||
// 如果不是今天,所有时间段都有效
|
||||
if (!isToday) {
|
||||
availableSlots.add(TimeSlot(startTime, endTime));
|
||||
@@ -224,8 +212,16 @@ class C_ReservationController extends GetxController with BaseControllerMixin {
|
||||
endTime.minute,
|
||||
);
|
||||
|
||||
// 注意:如果是跨天的 00:00 (例如 23:00 - 00:00),需要将日期加一天,否则 isAfter 判断会出错
|
||||
// 但由于我们用的是 endTime.hour % 24,当变成 0 时,日期还是 selectedDate
|
||||
// 这里做一个特殊处理:如果 endTime 是 00:00,意味着它实际上是明天的开始
|
||||
DateTime realEndDateTime = slotEndDateTime;
|
||||
if (endTime.hour == 0 && endTime.minute == 0) {
|
||||
realEndDateTime = slotEndDateTime.add(const Duration(days: 1));
|
||||
}
|
||||
|
||||
// 只要时间段的结束时间晚于当前时间,这个时间段就是可预约的
|
||||
if (slotEndDateTime.isAfter(now)) {
|
||||
if (realEndDateTime.isAfter(now)) {
|
||||
availableSlots.add(TimeSlot(startTime, endTime));
|
||||
}
|
||||
}
|
||||
@@ -236,13 +232,11 @@ class C_ReservationController extends GetxController with BaseControllerMixin {
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找当前选中的时间对应的新列表中的索引
|
||||
int initialItem = availableSlots.indexWhere(
|
||||
(slot) =>
|
||||
slot.start.hour == startTime.value.hour &&
|
||||
(startTime.value.minute < 30
|
||||
? slot.start.minute == 0
|
||||
: slot.start.minute == 30),
|
||||
(slot) => slot.start.hour == startTime.value.hour,
|
||||
);
|
||||
|
||||
if (initialItem == -1) {
|
||||
initialItem = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user