2026-03-02 17:28:21 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:lucide_icons/lucide_icons.dart';
|
2026-03-11 15:28:29 +08:00
|
|
|
import '../../../../core/di/injection.dart';
|
2026-03-11 17:16:11 +08:00
|
|
|
import '../../../../core/notifications/local_notification_service.dart';
|
2026-03-02 17:28:21 +08:00
|
|
|
import '../../../../core/theme/design_tokens.dart';
|
2026-03-16 16:11:28 +08:00
|
|
|
import '../../../../shared/widgets/app_loading_indicator.dart';
|
2026-03-20 01:30:34 +08:00
|
|
|
import '../../../../shared/widgets/app_selection_sheet.dart';
|
2026-03-16 16:11:28 +08:00
|
|
|
import '../../../../shared/widgets/app_sheet_input_field.dart';
|
2026-03-19 00:51:51 +08:00
|
|
|
import '../../../../shared/widgets/back_title_page_header.dart';
|
2026-03-16 16:11:28 +08:00
|
|
|
import '../../../../shared/widgets/toast/toast.dart';
|
|
|
|
|
import '../../../../shared/widgets/toast/toast_type.dart';
|
2026-03-19 00:51:51 +08:00
|
|
|
import 'date_time_picker_sheet.dart';
|
2026-03-02 17:28:21 +08:00
|
|
|
import '../../data/models/schedule_item_model.dart';
|
2026-03-12 16:41:45 +08:00
|
|
|
import '../../data/services/calendar_service.dart';
|
2026-03-02 17:28:21 +08:00
|
|
|
|
|
|
|
|
class CreateEventSheet extends StatefulWidget {
|
|
|
|
|
final DateTime? initialDate;
|
|
|
|
|
final ScheduleItemModel? editingEvent;
|
|
|
|
|
final VoidCallback? onSaved;
|
2026-03-19 00:51:51 +08:00
|
|
|
final bool pageMode;
|
2026-03-02 17:28:21 +08:00
|
|
|
|
|
|
|
|
const CreateEventSheet({
|
|
|
|
|
super.key,
|
|
|
|
|
this.initialDate,
|
|
|
|
|
this.editingEvent,
|
|
|
|
|
this.onSaved,
|
2026-03-19 00:51:51 +08:00
|
|
|
this.pageMode = false,
|
2026-03-02 17:28:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
static Future<void> show(
|
|
|
|
|
BuildContext context, {
|
|
|
|
|
DateTime? initialDate,
|
|
|
|
|
VoidCallback? onSaved,
|
|
|
|
|
}) {
|
|
|
|
|
return showModalBottomSheet(
|
|
|
|
|
context: context,
|
|
|
|
|
isScrollControlled: true,
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
builder: (context) =>
|
|
|
|
|
CreateEventSheet(initialDate: initialDate, onSaved: onSaved),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Future<void> edit(
|
|
|
|
|
BuildContext context,
|
|
|
|
|
ScheduleItemModel event, {
|
|
|
|
|
VoidCallback? onSaved,
|
|
|
|
|
}) {
|
|
|
|
|
return showModalBottomSheet(
|
|
|
|
|
context: context,
|
|
|
|
|
isScrollControlled: true,
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
builder: (context) =>
|
|
|
|
|
CreateEventSheet(editingEvent: event, onSaved: onSaved),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<CreateEventSheet> createState() => _CreateEventSheetState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _CreateEventSheetState extends State<CreateEventSheet>
|
|
|
|
|
with SingleTickerProviderStateMixin {
|
2026-03-18 13:35:25 +08:00
|
|
|
static const List<int?> _defaultReminderOptions = [
|
|
|
|
|
null,
|
|
|
|
|
0,
|
|
|
|
|
5,
|
|
|
|
|
10,
|
|
|
|
|
15,
|
|
|
|
|
30,
|
|
|
|
|
60,
|
|
|
|
|
120,
|
|
|
|
|
];
|
|
|
|
|
|
2026-03-02 17:28:21 +08:00
|
|
|
late TabController _tabController;
|
|
|
|
|
final _titleController = TextEditingController();
|
|
|
|
|
final _descriptionController = TextEditingController();
|
|
|
|
|
final _locationController = TextEditingController();
|
|
|
|
|
final _notesController = TextEditingController();
|
|
|
|
|
|
|
|
|
|
late DateTime _startDate;
|
|
|
|
|
late DateTime _startTime;
|
|
|
|
|
DateTime? _endDate;
|
|
|
|
|
DateTime? _endTime;
|
|
|
|
|
String _selectedColor = '#3B82F6';
|
2026-03-11 17:16:11 +08:00
|
|
|
int? _reminderMinutes = 15;
|
2026-03-11 15:28:29 +08:00
|
|
|
bool _saving = false;
|
2026-03-02 17:28:21 +08:00
|
|
|
|
|
|
|
|
bool get _isEditing => widget.editingEvent != null;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_tabController = TabController(length: 2, vsync: this);
|
|
|
|
|
|
|
|
|
|
if (_isEditing) {
|
|
|
|
|
final event = widget.editingEvent!;
|
|
|
|
|
_titleController.text = event.title;
|
|
|
|
|
_descriptionController.text = event.description ?? '';
|
|
|
|
|
_locationController.text = event.metadata?.location ?? '';
|
|
|
|
|
_notesController.text = event.metadata?.notes ?? '';
|
|
|
|
|
_startDate = event.startAt;
|
|
|
|
|
_startTime = event.startAt;
|
|
|
|
|
_endDate = event.endAt;
|
|
|
|
|
_endTime = event.endAt;
|
|
|
|
|
_selectedColor = event.metadata?.color ?? '#3B82F6';
|
2026-03-18 13:35:25 +08:00
|
|
|
_reminderMinutes = _sanitizeReminderMinutes(
|
|
|
|
|
event.metadata?.reminderMinutes,
|
|
|
|
|
);
|
2026-03-02 17:28:21 +08:00
|
|
|
} else {
|
2026-03-20 01:30:34 +08:00
|
|
|
final now = DateTime.now();
|
|
|
|
|
final initial = widget.initialDate;
|
|
|
|
|
final rounded = _roundToNearestMinute(now, 5);
|
|
|
|
|
_startDate = initial != null
|
|
|
|
|
? DateTime(
|
|
|
|
|
initial.year,
|
|
|
|
|
initial.month,
|
|
|
|
|
initial.day,
|
|
|
|
|
rounded.hour,
|
|
|
|
|
rounded.minute,
|
|
|
|
|
)
|
|
|
|
|
: rounded;
|
|
|
|
|
_startTime = _startDate;
|
|
|
|
|
_endDate = _startDate;
|
|
|
|
|
_endTime = _startDate.add(const Duration(hours: 1));
|
2026-03-02 17:28:21 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_tabController.dispose();
|
|
|
|
|
_titleController.dispose();
|
|
|
|
|
_descriptionController.dispose();
|
|
|
|
|
_locationController.dispose();
|
|
|
|
|
_notesController.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 17:16:11 +08:00
|
|
|
DateTime _roundToNearestMinute(DateTime dt, int interval) {
|
2026-03-11 17:32:00 +08:00
|
|
|
final totalMinutes = dt.hour * 60 + dt.minute;
|
|
|
|
|
final rounded = ((totalMinutes / interval).round() * interval);
|
|
|
|
|
final hours = rounded ~/ 60;
|
|
|
|
|
final minutes = rounded % 60;
|
|
|
|
|
final dayOffset = hours >= 24 ? 1 : 0;
|
|
|
|
|
final newHour = hours % 24;
|
|
|
|
|
return DateTime(dt.year, dt.month, dt.day + dayOffset, newHour, minutes);
|
2026-03-11 17:16:11 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-02 17:28:21 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2026-03-19 00:51:51 +08:00
|
|
|
if (widget.pageMode) {
|
2026-03-20 01:30:34 +08:00
|
|
|
return GestureDetector(
|
|
|
|
|
behavior: HitTestBehavior.translucent,
|
|
|
|
|
onTap: () => FocusScope.of(context).unfocus(),
|
|
|
|
|
child: Container(
|
|
|
|
|
color: AppColors.background,
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
|
children: [
|
|
|
|
|
_buildPageHeader(),
|
|
|
|
|
_buildTabBar(),
|
|
|
|
|
Expanded(child: _buildTabContent()),
|
|
|
|
|
],
|
|
|
|
|
),
|
2026-03-19 00:51:51 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 16:11:28 +08:00
|
|
|
return AnimatedPadding(
|
|
|
|
|
duration: const Duration(milliseconds: 150),
|
|
|
|
|
curve: Curves.easeOut,
|
|
|
|
|
padding: EdgeInsets.only(
|
|
|
|
|
bottom: MediaQuery.of(context).viewInsets.bottom,
|
2026-03-02 17:28:21 +08:00
|
|
|
),
|
2026-03-16 16:11:28 +08:00
|
|
|
child: Container(
|
|
|
|
|
height: MediaQuery.of(context).size.height * 0.85,
|
|
|
|
|
decoration: const BoxDecoration(
|
|
|
|
|
color: AppColors.white,
|
|
|
|
|
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
|
|
|
|
|
),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
|
children: [
|
|
|
|
|
const SizedBox(height: AppSpacing.sm),
|
|
|
|
|
Center(
|
|
|
|
|
child: Container(
|
|
|
|
|
width: AppSpacing.xl + AppSpacing.sm,
|
|
|
|
|
height: AppSpacing.xs,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: AppColors.slate200,
|
|
|
|
|
borderRadius: BorderRadius.circular(AppRadius.full),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: AppSpacing.sm),
|
|
|
|
|
_buildHeader(),
|
|
|
|
|
_buildTabBar(),
|
|
|
|
|
Expanded(child: _buildTabContent()),
|
|
|
|
|
],
|
|
|
|
|
),
|
2026-03-02 17:28:21 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 00:51:51 +08:00
|
|
|
Widget _buildPageHeader() {
|
|
|
|
|
return BackTitlePageHeader(
|
|
|
|
|
title: _isEditing ? '编辑日程' : '新建日程',
|
|
|
|
|
onBack: () => Navigator.of(context).pop(),
|
|
|
|
|
trailing: ValueListenableBuilder<TextEditingValue>(
|
|
|
|
|
valueListenable: _titleController,
|
|
|
|
|
builder: (context, value, child) {
|
|
|
|
|
final enabled = value.text.trim().isNotEmpty && !_saving;
|
|
|
|
|
return SizedBox(
|
|
|
|
|
height: AppSpacing.xxl * 2,
|
|
|
|
|
child: TextButton(
|
|
|
|
|
onPressed: enabled ? _saveEvent : null,
|
|
|
|
|
style: TextButton.styleFrom(
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.md),
|
|
|
|
|
minimumSize: const Size(AppSpacing.none, AppSpacing.none),
|
|
|
|
|
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
|
|
|
|
),
|
|
|
|
|
child: _saving
|
|
|
|
|
? const AppLoadingIndicator(
|
|
|
|
|
variant: AppLoadingVariant.button,
|
|
|
|
|
size: 18,
|
|
|
|
|
trackColor: AppColors.blue200,
|
|
|
|
|
)
|
|
|
|
|
: Text(
|
|
|
|
|
'保存',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
color: enabled ? AppColors.blue600 : AppColors.slate400,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 17:28:21 +08:00
|
|
|
Widget _buildHeader() {
|
|
|
|
|
return Container(
|
|
|
|
|
height: 56,
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
2026-03-12 18:26:10 +08:00
|
|
|
SizedBox(
|
|
|
|
|
width: AppSpacing.xxl * 2,
|
|
|
|
|
height: AppSpacing.xxl * 2,
|
|
|
|
|
child: TextButton(
|
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
|
style: TextButton.styleFrom(
|
|
|
|
|
padding: const EdgeInsets.all(AppSpacing.none),
|
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
|
borderRadius: BorderRadius.circular(AppRadius.full),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: const Icon(
|
|
|
|
|
LucideIcons.x,
|
|
|
|
|
size: AppSpacing.xxl,
|
|
|
|
|
color: AppColors.slate700,
|
|
|
|
|
),
|
2026-03-02 17:28:21 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Text(
|
|
|
|
|
_isEditing ? '编辑日程' : '新建日程',
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontSize: 17,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
color: AppColors.slate900,
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-03-11 20:51:56 +08:00
|
|
|
ValueListenableBuilder<TextEditingValue>(
|
|
|
|
|
valueListenable: _titleController,
|
|
|
|
|
builder: (context, value, child) {
|
2026-03-16 16:11:28 +08:00
|
|
|
final enabled = value.text.trim().isNotEmpty && !_saving;
|
2026-03-12 18:26:10 +08:00
|
|
|
return SizedBox(
|
|
|
|
|
height: AppSpacing.xxl * 2,
|
|
|
|
|
child: TextButton(
|
|
|
|
|
onPressed: enabled ? _saveEvent : null,
|
|
|
|
|
style: TextButton.styleFrom(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: AppSpacing.md,
|
|
|
|
|
),
|
|
|
|
|
minimumSize: const Size(AppSpacing.none, AppSpacing.none),
|
|
|
|
|
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
|
|
|
|
),
|
2026-03-16 16:11:28 +08:00
|
|
|
child: _saving
|
|
|
|
|
? const AppLoadingIndicator(
|
|
|
|
|
variant: AppLoadingVariant.button,
|
|
|
|
|
size: 18,
|
|
|
|
|
trackColor: AppColors.blue200,
|
|
|
|
|
)
|
|
|
|
|
: Text(
|
|
|
|
|
'保存',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 17,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
color: enabled
|
|
|
|
|
? AppColors.blue600
|
|
|
|
|
: AppColors.slate400,
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-03-11 20:51:56 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-03-02 17:28:21 +08:00
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildTabBar() {
|
|
|
|
|
return Container(
|
|
|
|
|
decoration: const BoxDecoration(
|
|
|
|
|
border: Border(bottom: BorderSide(color: AppColors.border)),
|
|
|
|
|
),
|
|
|
|
|
child: TabBar(
|
|
|
|
|
controller: _tabController,
|
|
|
|
|
labelColor: AppColors.blue600,
|
|
|
|
|
unselectedLabelColor: AppColors.slate600,
|
|
|
|
|
indicatorColor: AppColors.blue600,
|
|
|
|
|
tabs: const [
|
|
|
|
|
Tab(text: '基础'),
|
|
|
|
|
Tab(text: '进阶'),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildTabContent() {
|
|
|
|
|
return TabBarView(
|
|
|
|
|
controller: _tabController,
|
|
|
|
|
children: [_buildBasicTab(), _buildAdvancedTab()],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildBasicTab() {
|
|
|
|
|
return SingleChildScrollView(
|
2026-03-16 16:11:28 +08:00
|
|
|
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
|
2026-03-02 17:28:21 +08:00
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
2026-03-20 01:30:34 +08:00
|
|
|
_buildTextField('标题', _titleController, '请输入日程标题'),
|
2026-03-02 17:28:21 +08:00
|
|
|
const SizedBox(height: 20),
|
|
|
|
|
_buildDateTimePicker('开始', _startDate, _startTime, (date, time) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_startDate = date;
|
|
|
|
|
_startTime = time;
|
2026-03-11 15:28:29 +08:00
|
|
|
if (_endDate != null && _endTime != null) {
|
|
|
|
|
final endDateTime = DateTime(
|
|
|
|
|
_endDate!.year,
|
|
|
|
|
_endDate!.month,
|
|
|
|
|
_endDate!.day,
|
|
|
|
|
_endTime!.hour,
|
|
|
|
|
_endTime!.minute,
|
|
|
|
|
);
|
|
|
|
|
final startDateTime = DateTime(
|
|
|
|
|
date.year,
|
|
|
|
|
date.month,
|
|
|
|
|
date.day,
|
|
|
|
|
time.hour,
|
|
|
|
|
time.minute,
|
|
|
|
|
);
|
2026-03-11 20:51:56 +08:00
|
|
|
if (endDateTime.isBefore(startDateTime) ||
|
|
|
|
|
endDateTime.isAtSameMomentAs(startDateTime)) {
|
2026-03-11 15:28:29 +08:00
|
|
|
_endDate = date;
|
2026-03-11 20:51:56 +08:00
|
|
|
_endTime = time.add(const Duration(hours: 1));
|
2026-03-11 15:28:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-02 17:28:21 +08:00
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
|
_buildDateTimePicker(
|
|
|
|
|
'结束',
|
|
|
|
|
_endDate ?? _startDate,
|
|
|
|
|
_endTime ?? _startTime,
|
|
|
|
|
(date, time) {
|
|
|
|
|
setState(() {
|
2026-03-11 17:32:00 +08:00
|
|
|
final startDateTime = DateTime(
|
|
|
|
|
_startDate.year,
|
|
|
|
|
_startDate.month,
|
|
|
|
|
_startDate.day,
|
|
|
|
|
_startTime.hour,
|
|
|
|
|
_startTime.minute,
|
|
|
|
|
);
|
|
|
|
|
final endDateTime = DateTime(
|
|
|
|
|
date.year,
|
|
|
|
|
date.month,
|
|
|
|
|
date.day,
|
|
|
|
|
time.hour,
|
|
|
|
|
time.minute,
|
|
|
|
|
);
|
2026-03-11 20:51:56 +08:00
|
|
|
if (endDateTime.isBefore(startDateTime) ||
|
|
|
|
|
endDateTime.isAtSameMomentAs(startDateTime)) {
|
2026-03-11 17:32:00 +08:00
|
|
|
_endDate = _startDate;
|
2026-03-11 20:51:56 +08:00
|
|
|
_endTime = _startTime.add(const Duration(hours: 1));
|
2026-03-11 17:32:00 +08:00
|
|
|
} else {
|
|
|
|
|
_endDate = date;
|
|
|
|
|
_endTime = time;
|
|
|
|
|
}
|
2026-03-02 17:28:21 +08:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
isOptional: true,
|
2026-03-11 20:51:56 +08:00
|
|
|
minTime: DateTime(
|
|
|
|
|
_startDate.year,
|
|
|
|
|
_startDate.month,
|
|
|
|
|
_startDate.day,
|
|
|
|
|
_startTime.hour,
|
|
|
|
|
_startTime.minute,
|
|
|
|
|
),
|
2026-03-02 17:28:21 +08:00
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildAdvancedTab() {
|
|
|
|
|
return SingleChildScrollView(
|
2026-03-16 16:11:28 +08:00
|
|
|
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
|
2026-03-02 17:28:21 +08:00
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
_buildTextField('描述', _descriptionController, '请输入描述'),
|
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
|
_buildTextField('地点', _locationController, '请输入地点'),
|
|
|
|
|
const SizedBox(height: 20),
|
2026-03-11 17:16:11 +08:00
|
|
|
_buildReminderPicker(),
|
|
|
|
|
const SizedBox(height: 20),
|
2026-03-02 17:28:21 +08:00
|
|
|
_buildColorPicker(),
|
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
|
_buildTextField('备注', _notesController, '请输入备注', maxLines: 3),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildTextField(
|
|
|
|
|
String label,
|
|
|
|
|
TextEditingController controller,
|
|
|
|
|
String hint, {
|
|
|
|
|
int maxLines = 1,
|
2026-03-16 16:11:28 +08:00
|
|
|
bool autofocus = false,
|
2026-03-02 17:28:21 +08:00
|
|
|
}) {
|
2026-03-16 16:11:28 +08:00
|
|
|
return AppSheetInputField(
|
|
|
|
|
controller: controller,
|
|
|
|
|
label: label,
|
|
|
|
|
hint: hint,
|
|
|
|
|
maxLines: maxLines,
|
|
|
|
|
autofocus: autofocus,
|
2026-03-02 17:28:21 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildDateTimePicker(
|
|
|
|
|
String label,
|
|
|
|
|
DateTime date,
|
|
|
|
|
DateTime time,
|
|
|
|
|
Function(DateTime, DateTime) onChanged, {
|
|
|
|
|
bool isOptional = false,
|
2026-03-11 20:51:56 +08:00
|
|
|
DateTime? minTime,
|
2026-03-02 17:28:21 +08:00
|
|
|
}) {
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
label + (isOptional ? '(可选)' : ''),
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontSize: 14,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
color: AppColors.slate700,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
2026-03-11 15:28:29 +08:00
|
|
|
InkWell(
|
|
|
|
|
onTap: () async {
|
2026-03-11 20:51:56 +08:00
|
|
|
final picked = await _pickDateTime(date, time, minTime: minTime);
|
2026-03-11 15:28:29 +08:00
|
|
|
if (picked == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
onChanged(picked.$1, picked.$2);
|
|
|
|
|
},
|
|
|
|
|
borderRadius: BorderRadius.circular(AppRadius.md),
|
|
|
|
|
child: Container(
|
|
|
|
|
padding: const EdgeInsets.all(AppSpacing.md),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: AppColors.slate50,
|
|
|
|
|
borderRadius: BorderRadius.circular(AppRadius.md),
|
|
|
|
|
border: Border.all(color: AppColors.borderSecondary),
|
2026-03-02 17:28:21 +08:00
|
|
|
),
|
2026-03-11 15:28:29 +08:00
|
|
|
child: Row(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
const Icon(
|
|
|
|
|
LucideIcons.calendar,
|
|
|
|
|
size: 16,
|
|
|
|
|
color: AppColors.slate600,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: AppSpacing.sm),
|
|
|
|
|
Expanded(
|
2026-03-02 17:28:21 +08:00
|
|
|
child: Text(
|
2026-03-11 15:28:29 +08:00
|
|
|
_formatDateTimeLabel(date, time),
|
2026-03-02 17:28:21 +08:00
|
|
|
style: const TextStyle(
|
|
|
|
|
fontSize: 15,
|
2026-03-11 15:28:29 +08:00
|
|
|
fontWeight: FontWeight.w500,
|
2026-03-02 17:28:21 +08:00
|
|
|
color: AppColors.slate900,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-03-11 15:28:29 +08:00
|
|
|
const Icon(
|
|
|
|
|
LucideIcons.chevronRight,
|
|
|
|
|
size: 16,
|
|
|
|
|
color: AppColors.slate400,
|
|
|
|
|
),
|
|
|
|
|
],
|
2026-03-02 17:28:21 +08:00
|
|
|
),
|
2026-03-11 15:28:29 +08:00
|
|
|
),
|
2026-03-02 17:28:21 +08:00
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 15:28:29 +08:00
|
|
|
String _formatDateTimeLabel(DateTime date, DateTime time) {
|
|
|
|
|
return '${date.year}年${date.month}月${date.day}日 ${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<(DateTime, DateTime)?> _pickDateTime(
|
|
|
|
|
DateTime date,
|
2026-03-11 20:51:56 +08:00
|
|
|
DateTime time, {
|
|
|
|
|
DateTime? minTime,
|
|
|
|
|
}) async {
|
2026-03-11 15:28:29 +08:00
|
|
|
final result = await showModalBottomSheet<(DateTime, DateTime)>(
|
|
|
|
|
context: context,
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
isScrollControlled: true,
|
2026-03-19 00:51:51 +08:00
|
|
|
builder: (context) => DateTimePickerSheet(
|
2026-03-11 20:51:56 +08:00
|
|
|
initialDate: date,
|
|
|
|
|
initialTime: time,
|
|
|
|
|
minTime: minTime,
|
|
|
|
|
),
|
2026-03-11 15:28:29 +08:00
|
|
|
);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 17:28:21 +08:00
|
|
|
Widget _buildColorPicker() {
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
const Text(
|
|
|
|
|
'颜色',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 14,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
color: AppColors.slate700,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
Row(
|
|
|
|
|
children: defaultColors.map((color) {
|
|
|
|
|
final colorHex =
|
2026-03-03 10:12:46 +08:00
|
|
|
'#${color.toARGB32().toRadixString(16).substring(2).toUpperCase()}';
|
2026-03-02 17:28:21 +08:00
|
|
|
final isSelected = _selectedColor == colorHex;
|
|
|
|
|
return GestureDetector(
|
|
|
|
|
onTap: () => setState(() => _selectedColor = colorHex),
|
|
|
|
|
child: Container(
|
|
|
|
|
margin: const EdgeInsets.only(right: 12),
|
|
|
|
|
width: 32,
|
|
|
|
|
height: 32,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: color,
|
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
|
border: isSelected
|
|
|
|
|
? Border.all(color: AppColors.slate900, width: 2)
|
|
|
|
|
: null,
|
|
|
|
|
),
|
|
|
|
|
child: isSelected
|
|
|
|
|
? const Icon(Icons.check, size: 16, color: Colors.white)
|
|
|
|
|
: null,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 17:16:11 +08:00
|
|
|
Widget _buildReminderPicker() {
|
|
|
|
|
String labelOf(int? value) {
|
|
|
|
|
if (value == null) {
|
|
|
|
|
return '无提醒';
|
|
|
|
|
}
|
|
|
|
|
if (value == 0) {
|
|
|
|
|
return '准时提醒';
|
|
|
|
|
}
|
|
|
|
|
return '开始前$value分钟';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
const Text(
|
|
|
|
|
'提醒时间',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 14,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
color: AppColors.slate700,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
2026-03-20 01:30:34 +08:00
|
|
|
InkWell(
|
|
|
|
|
onTap: () async {
|
|
|
|
|
final options = _buildReminderOptions();
|
|
|
|
|
final selected = await showAppSelectionSheet<int?>(
|
|
|
|
|
context,
|
|
|
|
|
title: '选择提醒时间',
|
2026-03-11 17:16:11 +08:00
|
|
|
items: options
|
2026-03-20 01:30:34 +08:00
|
|
|
.map((v) => AppSelectionItem(value: v, label: labelOf(v)))
|
2026-03-11 17:16:11 +08:00
|
|
|
.toList(),
|
2026-03-20 01:30:34 +08:00
|
|
|
selectedValue: _reminderMinutes,
|
|
|
|
|
);
|
|
|
|
|
if (selected != null) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_reminderMinutes = selected;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
borderRadius: BorderRadius.circular(AppRadius.md),
|
|
|
|
|
child: Container(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
padding: const EdgeInsets.all(AppSpacing.md),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: AppColors.slate50,
|
|
|
|
|
borderRadius: BorderRadius.circular(AppRadius.md),
|
|
|
|
|
border: Border.all(color: AppColors.borderSecondary),
|
|
|
|
|
),
|
|
|
|
|
child: Row(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
|
|
|
|
child: Text(
|
|
|
|
|
labelOf(_reminderMinutes),
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontSize: 15,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
color: AppColors.slate900,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const Icon(
|
|
|
|
|
LucideIcons.chevronRight,
|
|
|
|
|
size: 16,
|
|
|
|
|
color: AppColors.slate400,
|
|
|
|
|
),
|
|
|
|
|
],
|
2026-03-11 17:16:11 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 13:35:25 +08:00
|
|
|
int? _sanitizeReminderMinutes(int? minutes) {
|
2026-03-19 00:51:51 +08:00
|
|
|
return (minutes != null && minutes >= 0) ? minutes : null;
|
2026-03-18 13:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<int?> _buildReminderOptions() {
|
|
|
|
|
final current = _sanitizeReminderMinutes(_reminderMinutes);
|
|
|
|
|
final nonNull = _defaultReminderOptions.whereType<int>().toSet();
|
|
|
|
|
if (current != null) {
|
|
|
|
|
nonNull.add(current);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final sorted = nonNull.toList()..sort();
|
|
|
|
|
return [null, ...sorted];
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 15:28:29 +08:00
|
|
|
Future<void> _saveEvent() async {
|
|
|
|
|
if (_titleController.text.trim().isEmpty || _saving) return;
|
|
|
|
|
setState(() {
|
|
|
|
|
_saving = true;
|
|
|
|
|
});
|
2026-03-02 17:28:21 +08:00
|
|
|
|
|
|
|
|
final startAt = DateTime(
|
|
|
|
|
_startDate.year,
|
|
|
|
|
_startDate.month,
|
|
|
|
|
_startDate.day,
|
|
|
|
|
_startTime.hour,
|
|
|
|
|
_startTime.minute,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
DateTime? endAt;
|
|
|
|
|
if (_endDate != null && _endTime != null) {
|
|
|
|
|
endAt = DateTime(
|
|
|
|
|
_endDate!.year,
|
|
|
|
|
_endDate!.month,
|
|
|
|
|
_endDate!.day,
|
|
|
|
|
_endTime!.hour,
|
|
|
|
|
_endTime!.minute,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final metadata = ScheduleMetadata(
|
|
|
|
|
color: _selectedColor,
|
|
|
|
|
location: _locationController.text.trim().isNotEmpty
|
|
|
|
|
? _locationController.text.trim()
|
|
|
|
|
: null,
|
|
|
|
|
notes: _notesController.text.trim().isNotEmpty
|
|
|
|
|
? _notesController.text.trim()
|
|
|
|
|
: null,
|
2026-03-11 17:16:11 +08:00
|
|
|
reminderMinutes: _reminderMinutes,
|
2026-03-16 16:11:28 +08:00
|
|
|
attachments: const [],
|
2026-03-11 15:28:29 +08:00
|
|
|
version: widget.editingEvent?.metadata?.version ?? 1,
|
2026-03-02 17:28:21 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final event = ScheduleItemModel(
|
|
|
|
|
id: _isEditing
|
|
|
|
|
? widget.editingEvent!.id
|
|
|
|
|
: 'evt_${DateTime.now().millisecondsSinceEpoch}',
|
2026-03-11 20:51:56 +08:00
|
|
|
ownerId: widget.editingEvent?.ownerId ?? '',
|
2026-03-02 17:28:21 +08:00
|
|
|
title: _titleController.text.trim(),
|
|
|
|
|
description: _descriptionController.text.trim().isNotEmpty
|
|
|
|
|
? _descriptionController.text.trim()
|
|
|
|
|
: null,
|
|
|
|
|
startAt: startAt,
|
|
|
|
|
endAt: endAt,
|
|
|
|
|
metadata: metadata,
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-11 15:28:29 +08:00
|
|
|
try {
|
|
|
|
|
final service = sl<CalendarService>();
|
2026-03-11 17:16:11 +08:00
|
|
|
late final ScheduleItemModel saved;
|
2026-03-11 15:28:29 +08:00
|
|
|
|
|
|
|
|
if (_isEditing) {
|
2026-03-11 17:16:11 +08:00
|
|
|
saved = await service.updateEvent(event);
|
2026-03-11 15:28:29 +08:00
|
|
|
} else {
|
2026-03-11 17:16:11 +08:00
|
|
|
saved = await service.addEvent(event);
|
2026-03-11 15:28:29 +08:00
|
|
|
}
|
2026-03-11 20:51:56 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final notificationService = sl<LocalNotificationService>();
|
|
|
|
|
await notificationService.upsertEventReminder(saved);
|
2026-03-18 19:12:47 +08:00
|
|
|
} catch (_) {
|
2026-03-17 18:05:49 +08:00
|
|
|
if (mounted) {
|
2026-03-18 19:12:47 +08:00
|
|
|
Toast.show(context, '提醒创建失败,请检查通知权限', type: ToastType.warning);
|
2026-03-17 18:05:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-11 15:28:29 +08:00
|
|
|
|
|
|
|
|
widget.onSaved?.call();
|
|
|
|
|
if (mounted) {
|
2026-03-20 16:45:08 +08:00
|
|
|
Navigator.pop(context, true);
|
2026-03-11 15:28:29 +08:00
|
|
|
}
|
2026-03-11 17:16:11 +08:00
|
|
|
} catch (e) {
|
2026-03-11 15:28:29 +08:00
|
|
|
if (mounted) {
|
2026-03-16 16:11:28 +08:00
|
|
|
Toast.show(context, '保存失败: $e', type: ToastType.error);
|
2026-03-11 15:28:29 +08:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_saving = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-03-02 17:28:21 +08:00
|
|
|
}
|
2026-03-11 15:28:29 +08:00
|
|
|
}
|
|
|
|
|
}
|