如何在抖动/飞镖中创建时隙?
我想从给定的开始时间和结束时间开始列出30分钟的时隙。
示例: 开始时间9:00 AM 结束时间是10:00 PM 列表将是
-c
有没有图书馆可以解决这个问题?
答案 0 :(得分:3)
您可以使用生成器来创建它,尽管语法有点难看,因为Flutter添加的TimeOfDay
类不是很强大:
Iterable<TimeOfDay> getTimes(TimeOfDay startTime, TimeOfDay endTime, Duration step) sync* {
var hour = startTime.hour;
var minute = startTime.minute;
do {
yield TimeOfDay(hour: hour, minute: minute);
minute += step.inMinutes;
while (minute >= 60) {
minute -= 60;
hour++;
}
} while (hour < endTime.hour ||
(hour == endTime.hour && minute <= endTime.minute));
}
用法:
final startTime = TimeOfDay(hour: 9, minute: 0);
final endTime = TimeOfDay(hour: 22, minute: 0);
final step = Duration(minutes: 30);
final times = getTimes(startTime, endTime, step)
.map((tod) => tod.format(context))
.toList();
print(times);
// Results:
// [9:00 AM, 9:30 AM, 10:00 AM, 10:30 AM, 11:00 AM, 11:30 AM, 12:00 PM, 12:30 PM, 1:00 PM, 1:30 PM, 2:00 PM, 2:30 PM, 3:00 PM, 3:30 PM, 4:00 PM, 4:30 PM, 5:00 PM, 5:30 PM, 6:00 PM, 6:30 PM, 7:00 PM, 7:30 PM, 8:00 PM, 8:30 PM, 9:00 PM, 9:30 PM, 10:00 PM]
答案 1 :(得分:0)
创建一个长度为的列表: List.generate
List<String>.generate(3, (int index) => "time ${index + 1}");
// ["time 1", "time 2", "time 3"]