120 lines
3.7 KiB
Dart
120 lines
3.7 KiB
Dart
import 'package:flisp_app/models/calendar.dart';
|
|
import 'package:flisp_app/provider/calendar_provider.dart';
|
|
import 'package:flisp_app/widgets/common.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_common/widget/dialog_widget.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:syncfusion_flutter_calendar/calendar.dart';
|
|
|
|
final viewItems = [
|
|
{'value': CalendarView.week, 'label': '周视图'},
|
|
{'value': CalendarView.month, 'label': '月视图'},
|
|
{'value': CalendarView.schedule, 'label': '日程视图'},
|
|
];
|
|
|
|
class AppointmentDataSource extends CalendarDataSource {
|
|
AppointmentDataSource(List<Appointment> source) {
|
|
appointments = source;
|
|
}
|
|
}
|
|
|
|
Widget buildTabs({
|
|
required BuildContext context,
|
|
required CalendarTab currentTab,
|
|
required ValueChanged<CalendarTab> onTabChanged,
|
|
}) {
|
|
return Container(
|
|
padding: EdgeInsets.all(8),
|
|
child: buildToggleSwitch(
|
|
context: context,
|
|
currentTab: currentTab,
|
|
tabValues: CalendarTab.values,
|
|
labels: CalendarTab.values.map((e) => e.label).toList(),
|
|
onTabChanged: onTabChanged,
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget buildCalendarMenu({required void Function(CalendarView) onSelected}) {
|
|
return PopupMenuButton<CalendarView>(
|
|
onSelected: onSelected,
|
|
itemBuilder: (BuildContext context) {
|
|
return viewItems.map((item) {
|
|
return PopupMenuItem<CalendarView>(
|
|
value: item['value'] as CalendarView,
|
|
child: Text(item['label'].toString()),
|
|
);
|
|
}).toList();
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget buildCalendar({
|
|
required BuildContext context,
|
|
required CalendarController controller,
|
|
required CalendarDataSource dataSource,
|
|
required VoidCallback onAdd,
|
|
required ValueChanged<Appointment> onEdit,
|
|
required ValueChanged<Appointment> onDelete,
|
|
}) {
|
|
final provider = Provider.of<CalendarProvider>(context);
|
|
|
|
return SfCalendar(
|
|
view: CalendarView.week,
|
|
controller: controller,
|
|
backgroundColor: Theme.of(context).colorScheme.surfaceContainer,
|
|
headerStyle: CalendarHeaderStyle(
|
|
backgroundColor: Theme.of(context).colorScheme.surfaceContainer,
|
|
),
|
|
dataSource: dataSource,
|
|
firstDayOfWeek: 1,
|
|
showDatePickerButton: true,
|
|
showNavigationArrow: true,
|
|
showTodayButton: true,
|
|
allowViewNavigation: true,
|
|
// 月份视图设置
|
|
monthViewSettings: MonthViewSettings(
|
|
appointmentDisplayMode: MonthAppointmentDisplayMode.appointment,
|
|
showAgenda: true,
|
|
),
|
|
scheduleViewSettings: ScheduleViewSettings(
|
|
monthHeaderSettings: MonthHeaderSettings(
|
|
backgroundColor: Theme.of(context).colorScheme.primary,
|
|
height: 85,
|
|
),
|
|
),
|
|
// 时间区域设置
|
|
timeSlotViewSettings: TimeSlotViewSettings(
|
|
startHour: 7,
|
|
endHour: 23,
|
|
timeFormat: 'HH:mm',
|
|
timeInterval: Duration(minutes: 30),
|
|
timeRulerSize: 60,
|
|
),
|
|
// 选择日期回调
|
|
onTap: (CalendarTapDetails details) {
|
|
if (details.targetElement == CalendarElement.calendarCell) {
|
|
if (!details.date!.isBefore(DateTime.now())) {
|
|
provider.initFormByDate(details.date!);
|
|
onAdd();
|
|
}
|
|
} else if (details.targetElement == CalendarElement.appointment) {
|
|
if (details.appointments?.length == 1) {
|
|
onEdit(details.appointments!.first);
|
|
}
|
|
}
|
|
},
|
|
// 选择日程回调
|
|
onLongPress: (CalendarLongPressDetails details) async {
|
|
if (details.targetElement == CalendarElement.appointment) {
|
|
if (details.appointments?.length == 1) {
|
|
final bool? result = await showConfirmDialog(context, '确定要删除这个项目吗?');
|
|
if (result == true) {
|
|
onDelete(details.appointments!.first);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
);
|
|
}
|