feat:增加日程模块
This commit is contained in:
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;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user