getDaysInWeek()列出了从星期日到星期六的日子。
getSevenInWeek()列出了2019年的第一周。1月1日应该在星期二而不是星期日。
如何使用DateTime类或Date_Utils包下的weekday属性将日期与星期几相匹配?
$project
答案 0 :(得分:1)
代替在getSevenInWeek
函数中设置日期。像这样在State类中进行操作,然后将其传递给getSevenInWeek
和getDaysInWeek
:
// no need for a library.
String getDay(int dayNumber) {
List<String> weekdays = const [
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
];
return weekdays[dayNumber];
}
class ThirdRouteState extends State<ThirdRoute> {
List<DateTime> seven = <DateTime>[];
@override
void initState() {
seven = <DateTime>[];
DateTime start = DateTime(2019, 01, 01);
DateTime end = DateTime(2019, 01, 08);
while (start.isBefore(end)) {
seven.add(start);
start = start.add(const Duration(days: 1));
}
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Calendar Page"),
),
body: Column(children: <Widget>[
Table(
children: <TableRow>[
TableRow(
children: getDaysInWeek(seven),
),
TableRow(
children: getSevenInWeek(seven),
),
],
),
]),
);
}
}
List<Widget> getSevenInWeek(List<DateTime> days) {
return days.map((DateTime time) {
return Text(
new DateFormat("dd").format(time),
style: TextStyle(backgroundColor: Colors.red),
textAlign: TextAlign.left,
);
}).toList();
}
List<Widget> getDaysInWeek(List<DateTime> days) {
List<Widget> dayWidgets = [];
days.forEach(
(DateTime day) {
dayWidgets.add(
new CalendarTile(
isDayOfWeek: true,
dayOfWeek: getDay(day.weekday),
),
);
},
);
return dayWidgets;
}