import 'package:ef/ef.dart'; class CalendarController extends GetControllerEx { CalendarController() { attr = GetModel({}).obs; // 传入一个空Map作为模型 } Rx displayedMonth = DateTime.now().obs; Rx selectedDate = Rx(null); List getDaysInMonth() { DateTime firstDayOfMonth = DateTime(displayedMonth.value.year, displayedMonth.value.month, 1); DateTime lastDayOfMonth = DateTime(displayedMonth.value.year, displayedMonth.value.month + 1, 0); List days = []; for (int i = 0; i < lastDayOfMonth.day; i++) { days.add(firstDayOfMonth.add(Duration(days: i))); } return days; } List> getCalendarRows(List daysInMonth) { List> calendarRows = []; int firstWeekday = daysInMonth.first.weekday; int emptyDays = (firstWeekday == 7 ? 6 : firstWeekday - 1); List row = []; for (int i = 0; i < emptyDays; i++) { row.add(DateTime(0)); } for (var day in daysInMonth) { row.add(day); if (row.length == 7) { calendarRows.add(List.from(row)); row.clear(); } } if (row.isNotEmpty) { while (row.length < 7) { row.add(DateTime(0)); } calendarRows.add(List.from(row)); } return calendarRows; } void previousMonth() { displayedMonth.value = DateTime(displayedMonth.value.year, displayedMonth.value.month - 1, 1); } void nextMonth() { displayedMonth.value = DateTime(displayedMonth.value.year, displayedMonth.value.month + 1, 1); } void selectDate(DateTime date) { selectedDate.value = date; } }