feat:增加日程模块
This commit is contained in:
291
lib/widgets/calendar_form.dart
Normal file
291
lib/widgets/calendar_form.dart
Normal file
@@ -0,0 +1,291 @@
|
||||
import 'package:flisp_app/models/calendar.dart';
|
||||
import 'package:flisp_app/provider/calendar_provider.dart';
|
||||
import 'package:flisp_app/utils/date_utils.dart';
|
||||
import 'package:flisp_app/widgets/common.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class CalendarForm extends StatefulWidget {
|
||||
final GlobalKey<FormBuilderState> formKey;
|
||||
final bool isEditing;
|
||||
final Calendar? initialCalendar;
|
||||
|
||||
const CalendarForm({
|
||||
super.key,
|
||||
required this.formKey,
|
||||
required this.isEditing,
|
||||
this.initialCalendar,
|
||||
});
|
||||
|
||||
@override
|
||||
State<CalendarForm> createState() => _CalendarFormState();
|
||||
}
|
||||
|
||||
class _CalendarFormState extends State<CalendarForm> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final int maxTitleCount = 10;
|
||||
final provider = Provider.of<CalendarProvider>(context);
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
Widget buildTitle() {
|
||||
return Row(
|
||||
children: [
|
||||
Icon(
|
||||
widget.isEditing ? Icons.edit_note : Icons.add_task,
|
||||
color: colors.primary,
|
||||
size: 24,
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
widget.isEditing ? '编辑日程事项' : '添加日程事项',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: colors.primary,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
String? titleFieldValidator(value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '请输入标题';
|
||||
}
|
||||
if (value.length > maxTitleCount) {
|
||||
return '标题不能超过$maxTitleCount个字符';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
FormBuilderTextField buildTitleField() {
|
||||
return FormBuilderTextField(
|
||||
name: 'title',
|
||||
initialValue: provider.formItem.title,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
provider.formItem.title = value ?? '';
|
||||
});
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
label: RichText(
|
||||
text: TextSpan(
|
||||
text: '内容',
|
||||
style: TextStyle(color: Colors.grey.shade700, fontSize: 16),
|
||||
children: const [
|
||||
TextSpan(
|
||||
text: '*',
|
||||
style: TextStyle(
|
||||
color: Colors.red,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
hintText: '请输入日程标题...',
|
||||
hintStyle: TextStyle(color: Colors.grey),
|
||||
counterText: '',
|
||||
suffixText: '${provider.formItem.title.length}/$maxTitleCount',
|
||||
border: buildFormBoard(),
|
||||
enabledBorder: buildFormEnabledBoard(),
|
||||
focusedBorder: buildFormFocusedBoard(colors),
|
||||
filled: true,
|
||||
fillColor: colors.surface,
|
||||
prefixIcon: Icon(Icons.title, color: Colors.blue),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
),
|
||||
maxLength: maxTitleCount,
|
||||
validator: (value) => titleFieldValidator(value),
|
||||
);
|
||||
}
|
||||
|
||||
String? startTimeFieldValidator(value) {
|
||||
if (value == null) {
|
||||
return '请选择开始时间';
|
||||
}
|
||||
if (value.isBefore(DateTime.now())) {
|
||||
return '不能选择过去的日期';
|
||||
}
|
||||
|
||||
final endTime = provider.formItem.endTime;
|
||||
if (endTime != null && endTime.isBefore(value)) {
|
||||
return '结束时间不能早于开始时间';
|
||||
}
|
||||
if (endTime != null && endTime.isAtSameMomentAs(value)) {
|
||||
return '开始时间不能等于结束时间';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
FormBuilderDateTimePicker buildStartTimeField() {
|
||||
var startTime = provider.formItem.startTime;
|
||||
|
||||
return FormBuilderDateTimePicker(
|
||||
name: 'startTime',
|
||||
format: DateFormat('yyyy-MM-dd HH:mm:ss'),
|
||||
initialValue: startTime,
|
||||
inputType: InputType.both,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
provider.formItem.startTime = value!;
|
||||
});
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText:
|
||||
startTime == null ? '选择开始时间' : '开始时间: ${formatTime(startTime)}',
|
||||
labelStyle: TextStyle(
|
||||
color: startTime == null ? Colors.grey : Colors.black87,
|
||||
),
|
||||
prefixIcon: Icon(Icons.calendar_month, color: Colors.purple),
|
||||
border: buildFormBoard(),
|
||||
enabledBorder: buildFormEnabledBoard(),
|
||||
focusedBorder: buildFormFocusedBoard(colors),
|
||||
filled: true,
|
||||
fillColor: colors.surface,
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
),
|
||||
validator: (value) => startTimeFieldValidator(value),
|
||||
);
|
||||
}
|
||||
|
||||
String? endTimeFieldValidator(value) {
|
||||
if (value == null) {
|
||||
return '请选择结束时间';
|
||||
}
|
||||
if (value.isBefore(DateTime.now())) {
|
||||
return '不能选择过去的日期';
|
||||
}
|
||||
|
||||
final startTime = provider.formItem.startTime;
|
||||
if (startTime != null && value.isBefore(startTime)) {
|
||||
return '结束时间不能早于开始时间';
|
||||
}
|
||||
if (startTime != null && startTime.isAtSameMomentAs(value)) {
|
||||
return '开始时间不能等于结束时间';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
FormBuilderDateTimePicker buildEndTimeField() {
|
||||
var endTime = provider.formItem.endTime;
|
||||
|
||||
return FormBuilderDateTimePicker(
|
||||
name: 'endTime',
|
||||
format: DateFormat('yyyy-MM-dd HH:mm:ss'),
|
||||
initialValue: endTime,
|
||||
inputType: InputType.both,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
provider.formItem.endTime = value!;
|
||||
});
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText:
|
||||
endTime == null ? '选择结束时间' : '结束时间: ${formatTime(endTime)}',
|
||||
labelStyle: TextStyle(
|
||||
color: endTime == null ? Colors.grey : Colors.black87,
|
||||
),
|
||||
prefixIcon: Icon(Icons.calendar_month, color: Colors.purple),
|
||||
border: buildFormBoard(),
|
||||
enabledBorder: buildFormEnabledBoard(),
|
||||
focusedBorder: buildFormFocusedBoard(colors),
|
||||
filled: true,
|
||||
fillColor: colors.surface,
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
),
|
||||
validator: (value) => endTimeFieldValidator(value),
|
||||
);
|
||||
}
|
||||
|
||||
IconButton buildClearScheduled() {
|
||||
return IconButton(
|
||||
icon: Icon(Icons.clear, size: 18),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
provider.formItem.scheduledTime = null;
|
||||
});
|
||||
widget.formKey.currentState?.fields['scheduledTime']?.didChange(null);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
String? scheduledTimeFieldValidator(value) {
|
||||
if (value != null && value.isBefore(DateTime.now())) {
|
||||
return '不能选择过去的日期';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
FormBuilderDateTimePicker buildScheduledTimeField() {
|
||||
var scheduledTime = provider.formItem.scheduledTime;
|
||||
|
||||
return FormBuilderDateTimePicker(
|
||||
name: 'scheduledTime',
|
||||
format: DateFormat('yyyy-MM-dd HH:mm:ss'),
|
||||
initialValue: scheduledTime,
|
||||
inputType: InputType.both,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
provider.formItem.scheduledTime = value;
|
||||
});
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText:
|
||||
scheduledTime == null
|
||||
? '选择提醒日期'
|
||||
: '提醒: ${formatTime(scheduledTime)}',
|
||||
labelStyle: TextStyle(
|
||||
color: scheduledTime == null ? Colors.grey : Colors.black87,
|
||||
),
|
||||
prefixIcon: Icon(Icons.calendar_month, color: Colors.purple),
|
||||
border: buildFormBoard(),
|
||||
enabledBorder: buildFormEnabledBoard(),
|
||||
focusedBorder: buildFormFocusedBoard(colors),
|
||||
filled: true,
|
||||
fillColor: colors.surface,
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
suffixIcon: scheduledTime != null ? buildClearScheduled() : null,
|
||||
),
|
||||
validator: (value) => scheduledTimeFieldValidator(value),
|
||||
);
|
||||
}
|
||||
|
||||
FormBuilder buildForm() {
|
||||
return FormBuilder(
|
||||
key: widget.formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
buildTitleField(),
|
||||
SizedBox(height: 12),
|
||||
buildStartTimeField(),
|
||||
SizedBox(height: 12),
|
||||
buildEndTimeField(),
|
||||
SizedBox(height: 12),
|
||||
buildScheduledTimeField(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [buildTitle(), SizedBox(height: 20), buildForm()],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
89
lib/widgets/calendar_widget.dart
Normal file
89
lib/widgets/calendar_widget.dart
Normal file
@@ -0,0 +1,89 @@
|
||||
import 'package:flisp_app/provider/calendar_provider.dart';
|
||||
import 'package:flisp_app/widgets/common.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:syncfusion_flutter_calendar/calendar.dart';
|
||||
|
||||
final viewItems = [
|
||||
{'value': CalendarView.week, 'label': '周视图'},
|
||||
{'value': CalendarView.month, 'label': '月视图'},
|
||||
{'value': CalendarView.schedule, 'label': '日程视图'},
|
||||
];
|
||||
|
||||
class AppointmentDataSource extends CalendarDataSource {
|
||||
AppointmentDataSource(List<Appointment> source) {
|
||||
appointments = source;
|
||||
}
|
||||
}
|
||||
|
||||
Widget buildCalendarMenu({required void Function(CalendarView) onSelected}) {
|
||||
return PopupMenuButton<CalendarView>(
|
||||
onSelected: onSelected,
|
||||
itemBuilder: (BuildContext context) {
|
||||
return viewItems.map((item) {
|
||||
return PopupMenuItem<CalendarView>(
|
||||
value: item['value'] as CalendarView,
|
||||
child: Text(item['label'].toString()),
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget buildCalendar({
|
||||
required BuildContext context,
|
||||
required CalendarController controller,
|
||||
required CalendarDataSource dataSource,
|
||||
required VoidCallback onAdd,
|
||||
required ValueChanged<Appointment> onEdit,
|
||||
required ValueChanged<Appointment> onDelete,
|
||||
}) {
|
||||
final provider = Provider.of<CalendarProvider>(context);
|
||||
|
||||
return SfCalendar(
|
||||
view: CalendarView.week,
|
||||
controller: controller,
|
||||
dataSource: dataSource,
|
||||
firstDayOfWeek: 1,
|
||||
showDatePickerButton: true,
|
||||
showNavigationArrow: true,
|
||||
allowViewNavigation: true,
|
||||
// 月份视图设置
|
||||
monthViewSettings: MonthViewSettings(
|
||||
appointmentDisplayMode: MonthAppointmentDisplayMode.appointment,
|
||||
showAgenda: true,
|
||||
),
|
||||
// 时间区域设置
|
||||
timeSlotViewSettings: TimeSlotViewSettings(
|
||||
startHour: 7,
|
||||
endHour: 23,
|
||||
timeFormat: 'HH:mm',
|
||||
timeInterval: Duration(minutes: 30),
|
||||
timeRulerSize: 60,
|
||||
),
|
||||
// 选择日期回调
|
||||
onTap: (CalendarTapDetails details) {
|
||||
if (details.targetElement == CalendarElement.calendarCell) {
|
||||
if (!details.date!.isBefore(DateTime.now())) {
|
||||
provider.initFormByDate(details.date!);
|
||||
onAdd();
|
||||
}
|
||||
} else if (details.targetElement == CalendarElement.appointment) {
|
||||
if (details.appointments?.length == 1) {
|
||||
onEdit(details.appointments!.first);
|
||||
}
|
||||
}
|
||||
},
|
||||
// 选择日程回调
|
||||
onLongPress: (CalendarLongPressDetails details) async {
|
||||
if (details.targetElement == CalendarElement.appointment) {
|
||||
if (details.appointments?.length == 1) {
|
||||
final bool? result = await showDeleteConfirmationDialog(context);
|
||||
if (result == true) {
|
||||
onDelete(details.appointments!.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1,11 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:toggle_switch/toggle_switch.dart';
|
||||
|
||||
BoxDecoration buildBoxDecoration() {
|
||||
return BoxDecoration(
|
||||
color: Colors.white,
|
||||
OutlineInputBorder buildFormBoard() {
|
||||
return OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.grey[200]!, width: 1),
|
||||
borderSide: BorderSide(color: Colors.grey.shade200),
|
||||
);
|
||||
}
|
||||
|
||||
OutlineInputBorder buildFormEnabledBoard() {
|
||||
return OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: BorderSide(color: Colors.grey.shade200),
|
||||
);
|
||||
}
|
||||
|
||||
OutlineInputBorder buildFormFocusedBoard(ColorScheme colors) {
|
||||
return OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: BorderSide(color: colors.primary),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -130,7 +143,7 @@ Widget buildDismissible({
|
||||
background: _buildDismissBackground(),
|
||||
confirmDismiss: (direction) async {
|
||||
// 这里实现二次确认
|
||||
return await _showDeleteConfirmationDialog(context);
|
||||
return await showDeleteConfirmationDialog(context);
|
||||
},
|
||||
onDismissed: (direction) {
|
||||
onDelete();
|
||||
@@ -140,7 +153,7 @@ Widget buildDismissible({
|
||||
}
|
||||
|
||||
// 确认对话框
|
||||
Future<bool?> _showDeleteConfirmationDialog(BuildContext context) async {
|
||||
Future<bool?> showDeleteConfirmationDialog(BuildContext context) async {
|
||||
return showDialog<bool>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
|
||||
Reference in New Issue
Block a user