60 lines
1.6 KiB
Dart
60 lines
1.6 KiB
Dart
import 'package:ef/ef.dart';
|
|
|
|
class CalendarController extends GetControllerEx {
|
|
Rx<DateTime> displayedMonth = DateTime.now().obs;
|
|
Rx<DateTime?> selectedDate = Rx<DateTime?>(null);
|
|
|
|
List<DateTime> getDaysInMonth() {
|
|
DateTime firstDayOfMonth =
|
|
DateTime(displayedMonth.value.year, displayedMonth.value.month, 1);
|
|
DateTime lastDayOfMonth =
|
|
DateTime(displayedMonth.value.year, displayedMonth.value.month + 1, 0);
|
|
List<DateTime> days = [];
|
|
|
|
for (int i = 0; i < lastDayOfMonth.day; i++) {
|
|
days.add(firstDayOfMonth.add(Duration(days: i)));
|
|
}
|
|
return days;
|
|
}
|
|
|
|
List<List<DateTime>> getCalendarRows(List<DateTime> daysInMonth) {
|
|
List<List<DateTime>> calendarRows = [];
|
|
int firstWeekday = daysInMonth.first.weekday;
|
|
int emptyDays = (firstWeekday == 7 ? 6 : firstWeekday - 1);
|
|
|
|
List<DateTime> 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;
|
|
}
|
|
}
|