feat:增加日程模块
This commit is contained in:
106
lib/widgets/common.dart
Normal file
106
lib/widgets/common.dart
Normal file
@@ -0,0 +1,106 @@
|
||||
import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:omni_datetime_picker/omni_datetime_picker.dart';
|
||||
|
||||
Widget buildFormItem({
|
||||
required String label,
|
||||
required Widget child,
|
||||
bool required = false,
|
||||
}) {
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 80,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (required)
|
||||
Padding(
|
||||
padding: EdgeInsets.only(right: 4),
|
||||
child: Text(
|
||||
'*',
|
||||
style: TextStyle(color: Colors.red, fontSize: 16),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
label,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
const Text(':', style: TextStyle(fontSize: 14)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
// 右侧表单控件
|
||||
Expanded(child: child),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// 显示日期/日期时间的按钮组件
|
||||
Widget buildOmniDateButton({
|
||||
required BuildContext context,
|
||||
required DateTime? selectedDate,
|
||||
required String placeholder,
|
||||
required OmniDateTimePickerType type,
|
||||
required ValueChanged<DateTime?> onChanged,
|
||||
}) {
|
||||
String displayText = '$placeholder';
|
||||
|
||||
if (selectedDate != null) {
|
||||
switch (type) {
|
||||
case OmniDateTimePickerType.date:
|
||||
displayText = DateFormat('yyyy-MM-dd').format(selectedDate);
|
||||
break;
|
||||
case OmniDateTimePickerType.time:
|
||||
displayText = DateFormat('HH:mm:ss').format(selectedDate);
|
||||
break;
|
||||
case OmniDateTimePickerType.dateAndTime:
|
||||
displayText = DateFormat('yyyy-MM-dd HH:mm:ss').format(selectedDate);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return Button(
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(FluentIcons.calendar, size: 16),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
displayText,
|
||||
style: TextStyle(
|
||||
color: selectedDate == null ? Colors.grey[130] : null,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
onPressed: () async {
|
||||
final DateTime? picked = await showOmniDateTimePicker(
|
||||
context: context,
|
||||
initialDate: selectedDate ?? DateTime.now(),
|
||||
firstDate: DateTime(
|
||||
DateTime.now().year,
|
||||
DateTime.now().month,
|
||||
DateTime.now().day,
|
||||
),
|
||||
lastDate: DateTime.now().add(Duration(days: 365 * 2)),
|
||||
is24HourMode: true,
|
||||
isShowSeconds: false,
|
||||
type: type,
|
||||
barrierColor: Colors.grey,
|
||||
);
|
||||
|
||||
if (picked != null) {
|
||||
onChanged(picked);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
175
lib/widgets/schedule_dialog.dart
Normal file
175
lib/widgets/schedule_dialog.dart
Normal file
@@ -0,0 +1,175 @@
|
||||
import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:omni_datetime_picker/omni_datetime_picker.dart';
|
||||
import 'package:task_hub/models/Schedule.dart';
|
||||
import 'package:task_hub/widgets/common.dart';
|
||||
|
||||
class ScheduleDialog extends StatefulWidget {
|
||||
final Schedule? schedule;
|
||||
final ValueChanged<Schedule> onSave;
|
||||
|
||||
const ScheduleDialog({super.key, this.schedule, required this.onSave});
|
||||
|
||||
@override
|
||||
State<ScheduleDialog> createState() => _ScheduleDialogState();
|
||||
}
|
||||
|
||||
class _ScheduleDialogState extends State<ScheduleDialog> {
|
||||
late TextEditingController _titleController;
|
||||
DateTime? _selectedStartTime;
|
||||
DateTime? _selectedEndTime;
|
||||
DateTime? _selectedScheduleDate;
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_titleController = TextEditingController(text: widget.schedule?.title ?? '');
|
||||
_selectedStartTime = widget.schedule?.startTime;
|
||||
_selectedEndTime = widget.schedule?.endTime;
|
||||
_selectedScheduleDate = widget.schedule?.scheduleDate;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void onConfirmClick()async {
|
||||
if (_selectedStartTime == null) {
|
||||
await displayInfoBar(context, builder: (context, close) {
|
||||
return InfoBar(
|
||||
title: const Text('请选择开始时间'),
|
||||
severity: InfoBarSeverity.error,
|
||||
);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (_selectedEndTime == null) {
|
||||
await displayInfoBar(context, builder: (context, close) {
|
||||
return InfoBar(
|
||||
title: const Text('请选择结束时间'),
|
||||
severity: InfoBarSeverity.error,
|
||||
);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_selectedEndTime!.isAfter(_selectedStartTime!)) {
|
||||
await displayInfoBar(context, builder: (context, close) {
|
||||
return InfoBar(
|
||||
title: const Text('结束时间必须晚于开始时间'),
|
||||
severity: InfoBarSeverity.error,
|
||||
);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (_formKey.currentState!.validate()) {
|
||||
final schedule = Schedule(
|
||||
id: widget.schedule?.id,
|
||||
title: _titleController.text,
|
||||
startTime: _selectedStartTime!,
|
||||
endTime: _selectedEndTime!,
|
||||
scheduleDate: _selectedScheduleDate,
|
||||
);
|
||||
widget.onSave(schedule);
|
||||
Navigator.pop(context);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ContentDialog(
|
||||
title: Text(widget.schedule == null ? '添加日程' : '编辑日程'),
|
||||
content: _buildForm(),
|
||||
actions: [
|
||||
Button(
|
||||
child: const Text('取消'),
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
FilledButton(child: const Text('保存'), onPressed: onConfirmClick),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildForm() {
|
||||
return Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 标题
|
||||
buildFormItem(
|
||||
label: '日程标题',
|
||||
required: true,
|
||||
child: TextFormBox(
|
||||
controller: _titleController,
|
||||
placeholder: '请输入日程标题',
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '任务标题不能为空';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 开始时间
|
||||
buildFormItem(
|
||||
label: '开始时间',
|
||||
required: true,
|
||||
child: buildOmniDateButton(
|
||||
context: context,
|
||||
selectedDate: _selectedStartTime,
|
||||
placeholder: '请选择开始时间',
|
||||
type: OmniDateTimePickerType.dateAndTime,
|
||||
onChanged: (date) {
|
||||
setState(() {
|
||||
_selectedStartTime = date!;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 结束时间
|
||||
buildFormItem(
|
||||
label: '结束时间',
|
||||
required: true,
|
||||
child: buildOmniDateButton(
|
||||
context: context,
|
||||
selectedDate: _selectedEndTime,
|
||||
placeholder: '请选择结束时间',
|
||||
type: OmniDateTimePickerType.dateAndTime,
|
||||
onChanged: (date) {
|
||||
setState(() {
|
||||
_selectedEndTime = date!;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 提醒日期
|
||||
buildFormItem(
|
||||
label: '提醒日期',
|
||||
child: buildOmniDateButton(
|
||||
context: context,
|
||||
selectedDate: _selectedScheduleDate,
|
||||
placeholder: '请选择提醒日期',
|
||||
type: OmniDateTimePickerType.dateAndTime,
|
||||
onChanged: (date) {
|
||||
setState(() {
|
||||
_selectedScheduleDate = date;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
176
lib/widgets/task_dialog.dart
Normal file
176
lib/widgets/task_dialog.dart
Normal file
@@ -0,0 +1,176 @@
|
||||
import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:omni_datetime_picker/omni_datetime_picker.dart';
|
||||
import 'package:task_hub/models/task.dart';
|
||||
import 'package:task_hub/widgets/common.dart';
|
||||
|
||||
class TaskDialog extends StatefulWidget {
|
||||
final Task? task;
|
||||
final ValueChanged<Task> onSave;
|
||||
|
||||
const TaskDialog({super.key, this.task, required this.onSave});
|
||||
|
||||
@override
|
||||
State<TaskDialog> createState() => _TaskDialogState();
|
||||
}
|
||||
|
||||
class _TaskDialogState extends State<TaskDialog> {
|
||||
late TextEditingController _titleController;
|
||||
late TextEditingController _descController;
|
||||
late Priority _selectedPriority;
|
||||
DateTime? _selectedDueDate;
|
||||
DateTime? _selectedScheduleDate;
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_titleController = TextEditingController(text: widget.task?.title ?? '');
|
||||
_descController = TextEditingController(text: widget.task?.desc ?? '');
|
||||
_selectedPriority = widget.task?.priority ?? Priority.medium;
|
||||
_selectedDueDate = widget.task?.dueDate;
|
||||
_selectedScheduleDate = widget.task?.scheduleDate;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
_descController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void onConfirmClick() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
final task = Task(
|
||||
id: widget.task?.id,
|
||||
title: _titleController.text,
|
||||
desc: _descController.text.isEmpty ? null : _descController.text,
|
||||
dueDate: _selectedDueDate,
|
||||
scheduleDate: _selectedScheduleDate,
|
||||
priority: _selectedPriority,
|
||||
isCompleted: widget.task?.isCompleted ?? false,
|
||||
);
|
||||
widget.onSave(task);
|
||||
Navigator.pop(context);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ContentDialog(
|
||||
title: Text(widget.task == null ? '添加任务' : '编辑任务'),
|
||||
content: _buildForm(),
|
||||
actions: [
|
||||
Button(
|
||||
child: const Text('取消'),
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
FilledButton(child: const Text('保存'), onPressed: onConfirmClick),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildForm() {
|
||||
return Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 标题
|
||||
buildFormItem(
|
||||
label: '任务标题',
|
||||
required: true,
|
||||
child: TextFormBox(
|
||||
controller: _titleController,
|
||||
placeholder: '请输入任务标题',
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '任务标题不能为空';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 描述
|
||||
buildFormItem(
|
||||
label: '任务描述',
|
||||
child: TextFormBox(
|
||||
controller: _descController,
|
||||
placeholder: '请输入任务描述',
|
||||
maxLines: 3,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 优先级
|
||||
buildFormItem(
|
||||
label: '优先级',
|
||||
child: RadioGroup<Priority>(
|
||||
groupValue: _selectedPriority,
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
RadioButton<Priority>(
|
||||
value: Priority.low,
|
||||
content: Text('低'),
|
||||
),
|
||||
RadioButton<Priority>(
|
||||
value: Priority.medium,
|
||||
content: Text('中'),
|
||||
),
|
||||
RadioButton<Priority>(
|
||||
value: Priority.high,
|
||||
content: Text('高'),
|
||||
),
|
||||
],
|
||||
),
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_selectedPriority = value;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 截止日期
|
||||
buildFormItem(
|
||||
label: '截止日期',
|
||||
child: buildOmniDateButton(
|
||||
context: context,
|
||||
selectedDate: _selectedDueDate,
|
||||
placeholder: '请选择截止日期',
|
||||
type: OmniDateTimePickerType.date,
|
||||
onChanged: (date) {
|
||||
setState(() {
|
||||
_selectedDueDate = date;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 提醒日期
|
||||
buildFormItem(
|
||||
label: '提醒日期',
|
||||
child: buildOmniDateButton(
|
||||
context: context,
|
||||
selectedDate: _selectedScheduleDate,
|
||||
placeholder: '请选择提醒日期',
|
||||
type: OmniDateTimePickerType.dateAndTime,
|
||||
onChanged: (date) {
|
||||
setState(() {
|
||||
_selectedScheduleDate = date;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
188
lib/widgets/task_item.dart
Normal file
188
lib/widgets/task_item.dart
Normal file
@@ -0,0 +1,188 @@
|
||||
import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:task_hub/models/task.dart';
|
||||
|
||||
class TaskItem extends StatelessWidget {
|
||||
final Task task;
|
||||
final VoidCallback onToggleComplete;
|
||||
final VoidCallback onEdit;
|
||||
final VoidCallback onDelete;
|
||||
|
||||
const TaskItem({
|
||||
super.key,
|
||||
required this.task,
|
||||
required this.onToggleComplete,
|
||||
required this.onEdit,
|
||||
required this.onDelete,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Row(
|
||||
children: [
|
||||
Checkbox(
|
||||
checked: task.isCompleted,
|
||||
onChanged: (value) => onToggleComplete(),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
// 任务信息
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
_buildPriorityIndicator(task.priority),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(child: _buildTitle()),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
if (task.desc != null && task.desc!.isNotEmpty) _buildDesc(),
|
||||
const SizedBox(height: 2),
|
||||
if (task.dueDate != null || task.scheduleDate != null)
|
||||
_buildDate(),
|
||||
],
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(FluentIcons.edit, size: 16),
|
||||
onPressed: onEdit,
|
||||
),
|
||||
_buildDeleteButton(),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTitle() {
|
||||
return Text(
|
||||
task.title,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
decoration: task.isCompleted ? TextDecoration.lineThrough : null,
|
||||
color: task.isCompleted ? Colors.grey : Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDesc() {
|
||||
return Text(
|
||||
task.desc!,
|
||||
style: TextStyle(
|
||||
color: Colors.grey,
|
||||
fontSize: 14,
|
||||
decoration: task.isCompleted ? TextDecoration.lineThrough : null,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDate() {
|
||||
return Row(
|
||||
children: [
|
||||
if (task.dueDate != null) ...[
|
||||
const Icon(FluentIcons.calendar, size: 12, color: Colors.grey),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'截止日期: ${_formatDate(task.dueDate!)}',
|
||||
style: const TextStyle(color: Colors.grey, fontSize: 12),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
],
|
||||
if (task.scheduleDate != null) ...[
|
||||
const Icon(FluentIcons.clock, size: 12, color: Colors.grey),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'提醒日期: ${_formatDate(task.scheduleDate!)}',
|
||||
style: const TextStyle(color: Colors.grey, fontSize: 12),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPriorityIndicator(Priority priority) {
|
||||
final Map<Priority, (Color, String)> priorityInfo = {
|
||||
Priority.low: (Colors.grey, '低'),
|
||||
Priority.medium: (Colors.green, '中'),
|
||||
Priority.high: (Colors.red, '高'),
|
||||
};
|
||||
|
||||
final (color, label) = priorityInfo[priority]!;
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 3),
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDeleteButton() {
|
||||
final controller = FlyoutController();
|
||||
|
||||
return FlyoutTarget(
|
||||
controller: controller,
|
||||
child: IconButton(
|
||||
icon: const Icon(FluentIcons.delete, size: 16),
|
||||
onPressed: () {
|
||||
controller.showFlyout<void>(
|
||||
builder: (context) {
|
||||
return FlyoutContent(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('确认删除该任务?'),
|
||||
const SizedBox(height: 12.0),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Button(
|
||||
onPressed: () {
|
||||
onDelete();
|
||||
Flyout.of(context).close();
|
||||
},
|
||||
child: const Text('确认'),
|
||||
),
|
||||
const SizedBox(width: 5),
|
||||
Button(
|
||||
onPressed: Flyout.of(context).close,
|
||||
child: const Text('取消'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user