gpt4 book ai didi

flutter - 无法应用正确的逻辑来获取约会日期

转载 作者:行者123 更新时间:2023-12-04 14:07:50 25 4
gpt4 key购买 nike

我一直在开发一个约会应用程序,约会时段从当前时间到接下来的 2 天的早上 7 点到晚上 8 点开始。除非有人预订,否则每半小时可预约一次。因此,如果用户想预约今天的约会,它将禁用已经过去的时间并在当前时间 1 小时后显示可用时段。现在,我陷入困境的地方是我创建了一个函数,该函数列出了当前时间之后的所有可用约会,但不知道如何获取值来 flutter 小部件。我每次都为空

 class GetAppointment with ChangeNotifier {
final String appId, allotedCrew, allotedSlot, usedSlot, totalTimeSlots;
final DateTime totalDurationinHr;
dynamic availableTimeSlotsFm, currentDate;
bool slotAvailable, dayAvailable;

GetAppointment({
this.appId,
this.allotedCrew,
this.allotedSlot,
this.usedSlot,
this.totalTimeSlots,
this.totalDurationinHr,
this.availableTimeSlotsFm,
this.currentDate,
this.slotAvailable: true,
this.dayAvailable: true});
}

extension on DateTime {
DateTime roundDown({Duration delta = const Duration(minutes: 30)}) {
return DateTime.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch -
this.millisecondsSinceEpoch % delta.inMilliseconds);
}
}

class Appointments with ChangeNotifier {
Map<String, GetAppointment> _appointmentList = {};

Map<String, GetAppointment> get items {
return {..._appointmentList};
}

generateBookingSlots() {
final endTime = DateTime(now.year, now.month, now.day, 23, 60, now.second);
final startTime = DateTime(now.year, now.month, now.day, 6, 60, now.second);
var currentTime = DateTime.now();
var availableTimeSlots = DateTime.now();
var currentDate, availableTimeSlotsFm;
final diff_dy = endTime.difference(startTime).inDays;
final diff_hr = endTime.difference(startTime).inHours;

//List available day from current day
for (int i = 0; i <= 3; i++) {
int a = 0;
if ((diff_hr * 2) <= 0) {
a = 1;
}
currentDate = DateTime.now().add(Duration(days: 1) * (i + a));
}

// List numbers of hours in single day
final listAllTime = List.generate(diff_hr * 2, (index) {
final totalDuration = startTime.add(Duration(minutes: 30));
final totalTimeSlots =
DateFormat.jm().format(startTime.add(Duration(minutes: 30) * index));

if (currentTime.difference(startTime).inHours > 0 &&
currentTime.isBefore(endTime) &&
availableTimeSlots.isBefore(endTime)) {
availableTimeSlots = currentTime.add(Duration(minutes: 30) * index);
availableTimeSlots.roundDown(delta: Duration(minutes: 30));
availableTimeSlotsFm = DateFormat.jm()
.format(availableTimeSlots.roundDown(delta: Duration(minutes: 30)));
// availableTimeSlots.roundDown();
}
_appointmentList.putIfAbsent(
index.toString(),
() => GetAppointment(
appId: index.toString(),
allotedCrew: "2",
allotedSlot: "2",
usedSlot: "2",
totalTimeSlots: totalTimeSlots,
totalDurationinHr: totalDuration,
availableTimeSlotsFm: availableTimeSlotsFm,
currentDate: currentDate,
));
return {
'totalDurationinHr': totalDuration,
'totalTimeSlotsFm': totalTimeSlots,
'availableTimeSlotsFm': availableTimeSlotsFm,
'currentDate': currentDate
};
});
// notifyListeners();
}

enter image description here

它应该是这样的,我可以处理 UI 部分,只是不知道如何以简单的列表格式获取这些时间

最佳答案

而不是尝试这样做

List<Map<String, dynamic>> get generateBookingSlots {
final endTime = DateTime(now.year, now.month, now.day, 23, 60, now.second);
final startTime = DateTime(now.year, now.month, now.day, 6, 60, now.second);
var currentTime = DateTime.now();
var availableTimeSlots = DateTime.now();
var currentDate, availableTimeSlotsFm;
final diff_dy = endTime.difference(startTime).inDays;
final diff_hr = endTime.difference(startTime).inHours;

//List available day from current day
for (int i = 0; i <= 3; i++) {
int a = 0;
if ((diff_hr * 2) <= 0) {
a = 1;
}
currentDate = DateTime.now().add(Duration(days: 1) * (i + a));
}

// List numbers of hours in single day
final listAllTime = List.generate(diff_hr * 2, (index) {
final totalDuration = startTime.add(Duration(minutes: 30));
final totalTimeSlots =
DateFormat.jm().format(startTime.add(Duration(minutes: 30) * index));

if (currentTime.difference(startTime).inHours > 0 &&
currentTime.isBefore(endTime) &&
availableTimeSlots.isBefore(endTime)) {
availableTimeSlots = currentTime.add(Duration(minutes: 30) * index);
availableTimeSlots.roundDown(delta: Duration(minutes: 30));
availableTimeSlotsFm = DateFormat.jm()
.format(availableTimeSlots.roundDown(delta: Duration(minutes: 30)));
// availableTimeSlots.roundDown();
}

return {
'totalDurationinHr': totalDuration,
'totalTimeSlotsFm': totalTimeSlots,
'availableTimeSlotsFm': availableTimeSlotsFm,
'currentDate': currentDate
}
});
return listAlltime.toList();
}

现在您可以通过调用 generateBookingSlots 轻松获取您的数据如果您需要获取个人数据,请执行此操作

List<Map<String, dynamic>> get totalDurationinHr{
final totalDurationinHr = List.generate(generateBookingSlots.length, (index) {
return {'totalDurationinHr' :generateBookingSlots[index]['totalDurationinHr'] }
}

}

关于flutter - 无法应用正确的逻辑来获取约会日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66975082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com