【发布时间】:2022-07-06 13:19:59
【问题描述】:
我需要创建 2 个按钮,每周和每月。当我单击这些时,我需要从当天获取周/月。如果当前日期是 1 月 5 日,当我单击每周按钮时,我应该只得到从 1 月 1 日到 5 日的日期。不能超过到 12 月。 (如果我的英语有任何错误,请原谅)。有人可以告诉我如何在颤振中实现这一点吗?
【问题讨论】:
标签: android flutter flutter-test
我需要创建 2 个按钮,每周和每月。当我单击这些时,我需要从当天获取周/月。如果当前日期是 1 月 5 日,当我单击每周按钮时,我应该只得到从 1 月 1 日到 5 日的日期。不能超过到 12 月。 (如果我的英语有任何错误,请原谅)。有人可以告诉我如何在颤振中实现这一点吗?
【问题讨论】:
标签: android flutter flutter-test
请检查每周日期选择器https://pub.dev/packages/weekly_date_picker。这将与您的要求有些相似。
对于每月日期选择器,有很多选项,例如 https://pub.dev/packages/syncfusion_flutter_datepicker & list - https://pub.dev/packages?q=date+picker
如果您想要不同的东西,请分享您想要实现的 UI。所以我可以分享类似的解决方案。
【讨论】:
static DateTime? getFromDate(int calenderType) {
// calenderType 2 = week
// calenderType 3 = month
// calenderType 4 = year
DateTime lastDayOfMonth = new DateTime(DateTime.now().year, DateTime.now().month, 0);
DateTime lastYear = new DateTime(DateTime.now().year,1,0);
if(calenderType==2){
DateTime weekDate = DateTime.now().subtract(Duration(days: 7));
if(lastDayOfMonth.isBefore(weekDate)){
print('before'+weekDate.toString());
return weekDate;
}
else{
print('after'+weekDate.toString());
return lastDayOfMonth.add(Duration(days: 1));
}
}else if(calenderType == 3){
int totalDays = DateTimeRange(
start: DateTime(DateTime.now().year,DateTime.now().month,1),
end: DateTime(DateTime.now().year,DateTime.now().month + 1))
.duration
.inDays;
DateTime monthDate = DateTime.now().subtract(Duration(days: totalDays));
if(lastDayOfMonth.isBefore(monthDate)){
print('before'+monthDate.toString());
return monthDate;
}
else{
print('after'+monthDate.toString());
return lastDayOfMonth.add(Duration(days: 1));
}
}
else if(calenderType ==4) {
int totalDays = DateTimeRange(start:DateTime(DateTime.now().year,1,1),
end:DateTime(DateTime.now().year,DateTime.now().month,DateTime.now().day)).duration.inDays;
DateTime yearDate = DateTime.now().subtract(Duration(days: totalDays));
if(lastYear.isBefore(yearDate)){
print('before'+yearDate.toString());
return yearDate;
}
else{
print('after'+yearDate.toString());
return lastYear.add(Duration(days: 1));
}
}
这样就解决了。
【讨论】: