import 'package:flisp_app/models/calendar.dart'; import 'package:flisp_app/provider/calendar_provider.dart'; import 'package:flisp_app/service/calendar_service.dart'; import 'package:flisp_app/utils/notify_utils.dart'; import 'package:flisp_app/widgets/awesome_dialog.dart'; import 'package:flisp_app/widgets/calendar_form.dart'; import 'package:flisp_app/widgets/calendar_widget.dart'; import 'package:flutter/material.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; import 'package:provider/provider.dart'; import 'package:syncfusion_flutter_calendar/calendar.dart'; class CalendarPage extends StatefulWidget { const CalendarPage({super.key}); @override CalendarPageState createState() => CalendarPageState(); } class CalendarPageState extends State { // 日历控制器 final CalendarController _calendarController = CalendarController(); final CalendarService calendarService = CalendarService(); final NotifyService notifyService = NotifyService(); late List _appointments; void showAddDialog() { _showDialog(false, null); } @override void initState() { super.initState(); refreshAppointments(); } void refreshAppointments() { final calendars = calendarService.getAllCalendars(); _appointments = calendars .map( (calendar) => Appointment( id: calendar.id, startTime: calendar.startTime!, endTime: calendar.endTime!, subject: calendar.title, color: Colors.blue, ), ) .toList(); } void _showDialog(bool isEditing, Calendar? calendar) { final provider = Provider.of(context, listen: false); if (isEditing) { provider.initForm(calendar!); } else { provider.resetForm(); } final formKey = GlobalKey(); showAwesomeDialog( context: context, body: CalendarForm( formKey: formKey, isEditing: isEditing, initialCalendar: calendar, ), onOk: () { if (formKey.currentState!.saveAndValidate()) { Navigator.of(context).pop(); _saveCalendar(isEditing, provider.formItem); } }, onCancel: () { provider.resetForm(); }, ); } void _saveCalendar(bool isEditing, Calendar calendar) async { late bool isSuccess; if (isEditing) { // 先删除 await notifyService.cancelNotification(calendar.id); await calendarService.deleteCalendar(calendar); // 在重新新建 calendar.id = DateTime.now().millisecondsSinceEpoch ~/ 1000; } isSuccess = await calendarService.addCalendar(calendar); if (calendar.scheduledTime != null) { await notifyService.scheduleNotification( id: calendar.id, title: '日程提醒', body: calendar.title, scheduledTime: calendar.scheduledTime!, ); } setState(() { refreshAppointments(); }); if (isSuccess) { showSuccessDialog(context, isEditing ? '更新成功' : '添加成功'); } else { showErrorDialog(context, isEditing ? '更新失败' : '添加失败'); } } void _deleteCalendar(Calendar calendar) async { await notifyService.cancelNotification(calendar.id); bool isSuccess = await calendarService.deleteCalendar(calendar); setState(() { refreshAppointments(); }); if (isSuccess) { showSuccessDialog(context, '删除成功'); } else { showErrorDialog(context, '删除失败'); } } @override Widget build(BuildContext context) { final provider = Provider.of(context, listen: false); return Scaffold( appBar: AppBar( title: Text('日程安排'), centerTitle: true, actions: [ buildCalendarMenu( onSelected: (CalendarView value) { setState(() { _calendarController.view = value; }); }, ), ], ), body: Padding( padding: EdgeInsets.all(10), child: buildCalendar( context: context, controller: _calendarController, dataSource: AppointmentDataSource(_appointments), onAdd: () { if (_calendarController.view == CalendarView.week) { _showDialog(true, provider.formItem); } }, onEdit: (appointment) { final calendar = calendarService.getCalendarById( appointment.id as int, ); _showDialog(true, calendar); }, onDelete: (appointment) { final calendar = calendarService.getCalendarById( appointment.id as int, ); _deleteCalendar(calendar); }, ), ), ); } }