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