feat:增加日程模块

This commit is contained in:
2026-04-26 14:23:32 +08:00
parent 57c6e552d4
commit 8f6e4cc23e
13 changed files with 905 additions and 518 deletions

View File

@@ -1,6 +1,8 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/material.dart' show ButtonSegment, SegmentedButton;
import 'package:syncfusion_flutter_calendar/calendar.dart' as calendar;
import 'package:task_hub/models/Schedule.dart';
import 'package:task_hub/widgets/schedule_dialog.dart';
class AppointmentDataSource extends calendar.CalendarDataSource {
AppointmentDataSource(List<calendar.Appointment> source) {
@@ -16,14 +18,106 @@ class SchedulePage extends StatefulWidget {
}
class _SchedulePageState extends State<SchedulePage> {
final calendar.CalendarController _calendarController = calendar.CalendarController();
final calendar.CalendarController _calendarController =
calendar.CalendarController();
late List<calendar.Appointment> _appointments = [];
String _viewType = 'week';
void _addSchedule() {
showDialog(
context: context,
builder:
(context) => ScheduleDialog(
onSave: (schedule) {
setState(() {
final appointment = calendar.Appointment(
id: schedule.id,
subject: schedule.title,
startTime: schedule.startTime,
endTime: schedule.endTime,
);
_appointments.add(appointment);
});
},
),
);
}
void _editSchedule(calendar.Appointment appointment) {
final schedule = Schedule(
id: appointment.id as int,
title: appointment.subject,
startTime: appointment.startTime,
endTime: appointment.endTime,
);
showDialog(
context: context,
builder:
(context) => ScheduleDialog(
schedule: schedule,
onSave: (editedSchedule) {
setState(() {
final index = _appointments.indexWhere(
(t) => t.id == editedSchedule.id,
);
if (index != -1) {
final appointment = calendar.Appointment(
id: editedSchedule.id,
subject: editedSchedule.title,
startTime: editedSchedule.startTime,
endTime: editedSchedule.endTime,
);
_appointments[index] = appointment;
}
});
},
),
);
}
void _deleteSchedule(calendar.Appointment appointment) async {
await showDialog<String>(
context: context,
builder:
(context) => ContentDialog(
title: const Text('确认删除该日程?'),
actions: [
Button(
child: const Text('取消'),
onPressed: () => Navigator.pop(context),
),
FilledButton(
child: const Text('确认'),
onPressed: () {
setState(() {
_appointments.removeWhere((schedule) => schedule.id == appointment.id);
});
Navigator.pop(context);
// Delete file here
},
)
],
),
);
}
@override
Widget build(BuildContext context) {
return ScaffoldPage.withPadding(
header: const PageHeader(title: Text('日程安排')),
header: PageHeader(
title: const Text('日程安排'),
commandBar: CommandBar(
mainAxisAlignment: MainAxisAlignment.end,
primaryItems: [
CommandBarButton(
icon: const Icon(FluentIcons.add),
label: const Text('添加日程'),
onPressed: _addSchedule,
),
],
),
),
content: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
@@ -34,7 +128,7 @@ class _SchedulePageState extends State<SchedulePage> {
onSelectionChanged: (Set<String> value) {
setState(() {
_viewType = value.first;
switch(_viewType) {
switch (_viewType) {
case 'week':
_calendarController.view = calendar.CalendarView.week;
break;
@@ -50,7 +144,10 @@ class _SchedulePageState extends State<SchedulePage> {
segments: const [
ButtonSegment(
value: 'week',
label: Text('周视图', style: TextStyle(fontFamily: 'CustomFont')),
label: Text(
'周视图',
style: TextStyle(fontFamily: 'CustomFont'),
),
),
ButtonSegment(
value: 'month',
@@ -69,49 +166,68 @@ class _SchedulePageState extends State<SchedulePage> {
],
),
const SizedBox(height: 20),
Expanded(
child: calendar.SfCalendar(
view: calendar.CalendarView.week,
controller: _calendarController,
backgroundColor: Colors.grey[20],
headerStyle: calendar.CalendarHeaderStyle(
backgroundColor: Colors.grey[20],
),
dataSource: AppointmentDataSource(_appointments),
firstDayOfWeek: 1,
showDatePickerButton: true,
showNavigationArrow: true,
showTodayButton: true,
allowViewNavigation: true,
// 月份视图设置
monthViewSettings: calendar.MonthViewSettings(
appointmentDisplayMode:
calendar.MonthAppointmentDisplayMode.appointment,
showAgenda: true,
),
scheduleViewSettings: calendar.ScheduleViewSettings(
monthHeaderSettings: calendar.MonthHeaderSettings(
backgroundColor: Colors.grey[20],
height: 85,
),
),
// 时间区域设置
timeSlotViewSettings: calendar.TimeSlotViewSettings(
startHour: 7,
endHour: 23,
timeFormat: 'HH:mm',
timeInterval: Duration(minutes: 30),
timeRulerSize: 60,
),
// 选择日期回调
onTap: (calendar.CalendarTapDetails details) {},
// 选择日程回调
onLongPress: (calendar.CalendarLongPressDetails details) async {},
),
),
Expanded(child: _buildSfCalendar()),
],
),
),
);
}
Widget _buildSfCalendar() {
return calendar.SfCalendar(
view: calendar.CalendarView.week,
controller: _calendarController,
backgroundColor: Colors.grey[20],
headerStyle: calendar.CalendarHeaderStyle(
backgroundColor: Colors.grey[20],
),
dataSource: AppointmentDataSource(_appointments),
firstDayOfWeek: 1,
showDatePickerButton: true,
showNavigationArrow: true,
showTodayButton: true,
allowViewNavigation: true,
// 月份视图设置
monthViewSettings: calendar.MonthViewSettings(
appointmentDisplayMode:
calendar.MonthAppointmentDisplayMode.appointment,
showAgenda: true,
),
scheduleViewSettings: calendar.ScheduleViewSettings(
monthHeaderSettings: calendar.MonthHeaderSettings(
backgroundColor: Colors.grey[20],
height: 85,
),
),
// 时间区域设置
timeSlotViewSettings: calendar.TimeSlotViewSettings(
startHour: 7,
endHour: 23,
timeFormat: 'HH:mm',
timeInterval: Duration(minutes: 30),
timeRulerSize: 60,
),
// 选择日期回调
onTap: (calendar.CalendarTapDetails details) {
if (details.targetElement == calendar.CalendarElement.calendarCell) {
if (!details.date!.isBefore(DateTime.now())) {
_addSchedule();
}
} else if (details.targetElement ==
calendar.CalendarElement.appointment) {
if (details.appointments?.length == 1) {
_editSchedule(details.appointments!.first);
}
}
},
// 选择日程回调
onLongPress: (calendar.CalendarLongPressDetails details) async {
if (details.targetElement == calendar.CalendarElement.appointment) {
if (details.appointments?.length == 1) {
_deleteSchedule(details.appointments!.first);
}
}
},
);
}
}

View File

@@ -3,8 +3,8 @@ import 'dart:ui';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/material.dart' show SegmentedButton, ButtonSegment;
import 'package:task_hub/models/task.dart';
import 'package:task_hub/widges/task_dialog.dart';
import 'package:task_hub/widges/task_item.dart';
import 'package:task_hub/widgets/task_dialog.dart';
import 'package:task_hub/widgets/task_item.dart';
// 任务页面
class TaskPage extends StatefulWidget {
@@ -16,8 +16,8 @@ class TaskPage extends StatefulWidget {
class _TaskPageState extends State<TaskPage> {
final List<Task> _tasks = [];
String _filter = 'all'; // all, active, completed
String _sortBy = 'created'; // created, due, priority
String _filter = 'all';
String _sortBy = 'created';
List<Task> get _filteredTasks {
List<Task> tasks =
@@ -38,7 +38,7 @@ class _TaskPageState extends State<TaskPage> {
return b.priority.index.compareTo(a.priority.index);
case 'created':
default:
return b.createdAt.compareTo(a.createdAt);
return b.createTime.compareTo(a.createTime);
}
});
@@ -77,7 +77,7 @@ class _TaskPageState extends State<TaskPage> {
);
}
void _deleteTask(String taskId) {
void _deleteTask(int taskId) {
setState(() {
_tasks.removeWhere((task) => task.id == taskId);
});
@@ -90,8 +90,8 @@ class _TaskPageState extends State<TaskPage> {
_tasks[index] = Task(
id: task.id,
title: task.title,
description: task.description,
createdAt: task.createdAt,
desc: task.desc,
createTime: task.createTime,
dueDate: task.dueDate,
isCompleted: !task.isCompleted,
priority: task.priority,
@@ -101,24 +101,6 @@ class _TaskPageState extends State<TaskPage> {
});
}
void _updateTaskPriority(String taskId, Priority priority) {
setState(() {
final index = _tasks.indexWhere((task) => task.id == taskId);
if (index != -1) {
_tasks[index] = Task(
id: _tasks[index].id,
title: _tasks[index].title,
description: _tasks[index].description,
createdAt: _tasks[index].createdAt,
dueDate: _tasks[index].dueDate,
isCompleted: _tasks[index].isCompleted,
priority: priority,
category: _tasks[index].category,
);
}
});
}
@override
Widget build(BuildContext context) {
return ScaffoldPage.withPadding(
@@ -274,9 +256,7 @@ class _TaskPageState extends State<TaskPage> {
task: task,
onToggleComplete: () => _toggleTaskComplete(task),
onEdit: () => _editTask(task),
onDelete: () => _deleteTask(task.id),
onPriorityChanged:
(priority) => _updateTaskPriority(task.id, priority),
onDelete: () => _deleteTask(task.id)
),
);
},