feat: 增强日历功能并集成 AgentScope 代理服务

This commit is contained in:
qzl
2026-03-11 17:16:11 +08:00
parent e20e7d2a02
commit 85b314cf64
53 changed files with 3642 additions and 297 deletions
@@ -112,6 +112,7 @@ class ScheduleMetadata {
final String? color;
final String? location;
final String? notes;
final int? reminderMinutes;
final List<Attachment> attachments;
final int version;
final Map<String, dynamic> raw;
@@ -120,6 +121,7 @@ class ScheduleMetadata {
this.color,
this.location,
this.notes,
this.reminderMinutes,
List<Attachment>? attachments,
this.version = 1,
Map<String, dynamic>? raw,
@@ -130,6 +132,7 @@ class ScheduleMetadata {
String? color,
String? location,
String? notes,
int? reminderMinutes,
List<Attachment>? attachments,
int? version,
Map<String, dynamic>? raw,
@@ -138,6 +141,7 @@ class ScheduleMetadata {
color: color ?? this.color,
location: location ?? this.location,
notes: notes ?? this.notes,
reminderMinutes: reminderMinutes ?? this.reminderMinutes,
attachments: attachments ?? this.attachments,
version: version ?? this.version,
raw: raw ?? this.raw,
@@ -156,6 +160,7 @@ class ScheduleMetadata {
color: json['color'] as String?,
location: json['location'] as String?,
notes: json['notes'] as String?,
reminderMinutes: json['reminder_minutes'] as int?,
attachments: attachments,
version: (json['version'] as int?) ?? 1,
raw: Map<String, dynamic>.from(json),
@@ -167,6 +172,7 @@ class ScheduleMetadata {
'color': color,
'location': location,
'notes': notes,
'reminder_minutes': reminderMinutes,
'attachments': attachments.map((item) => item.toJson()).toList(),
'version': version,
};
@@ -24,11 +24,19 @@ class CalendarDayWeekScreen extends StatefulWidget {
State<CalendarDayWeekScreen> createState() => _CalendarDayWeekScreenState();
}
class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen>
with WidgetsBindingObserver {
static const double _dayItemWidth = 44;
static const double _dayItemGap = 12;
static const double _hourHeight = 34;
static const double _eventLeftOffset = 52;
static const double _defaultHourHeight = 34.0;
static const double _minHourHeight = 17.0;
static const double _maxHourHeight = 68.0;
double _hourHeight = _defaultHourHeight;
final Map<int, Offset> _activePointers = {};
double? _pinchStartDistance;
double _pinchStartHourHeight = _defaultHourHeight;
late final CalendarStateManager _calendarManager;
late DateTime _selectedDate;
@@ -36,10 +44,12 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
final ScrollController _dayStripController = ScrollController();
Key _eventsKey = UniqueKey();
List<ScheduleItemModel> _events = const [];
String? _lastRoute;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_calendarManager = sl<CalendarStateManager>();
if (widget.resetToToday) {
@@ -52,9 +62,22 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollToSelectedDate();
_lastRoute = GoRouterState.of(context).uri.toString();
});
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
final currentRoute = GoRouterState.of(context).uri.toString();
if (_lastRoute != null && _lastRoute != currentRoute) {
if (!currentRoute.contains('/events/')) {
_loadEvents();
}
}
_lastRoute = currentRoute;
}
void _updateMonthDates() {
_monthDates = monthDatesFor(_selectedDate);
}
@@ -71,47 +94,135 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_dayStripController.dispose();
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_loadEvents();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.todoBg,
body: SafeArea(
child: Column(
children: [
_buildHeader(),
Expanded(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(
left: AppSpacing.lg,
right: AppSpacing.lg,
top: 2,
bottom: 104,
),
child: Column(
children: [
_buildWeekStrip(),
const SizedBox(height: 8),
KeyedSubtree(
body: PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) {
if (!didPop) {
context.go('/calendar/month?date=${formatYmd(_selectedDate)}');
}
},
child: SafeArea(
child: Stack(
children: [
Positioned.fill(
top: 154,
bottom: 84,
child: Listener(
onPointerDown: _handlePointerDown,
onPointerMove: _handlePointerMove,
onPointerUp: _handlePointerUp,
onPointerCancel: _handlePointerCancel,
behavior: HitTestBehavior.translucent,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(
left: AppSpacing.lg,
right: AppSpacing.lg,
top: 2,
),
child: KeyedSubtree(
key: _eventsKey,
child: _buildTimelineBoard(),
),
],
),
),
),
),
),
_buildBottomDock(),
],
Positioned(top: 0, left: 0, right: 0, child: _buildHeader()),
Positioned(top: 68, left: 0, right: 0, child: _buildWeekStrip()),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: _buildBottomDock(),
),
],
),
),
),
);
}
void _goToToday() {
final today = DateTime.now();
setState(() {
_selectedDate = today;
_hourHeight = _defaultHourHeight;
});
_calendarManager.setSelectedDate(today);
_updateMonthDates();
_scrollToSelectedDate(animate: true);
_loadEvents();
}
void _handlePointerDown(PointerDownEvent event) {
_activePointers[event.pointer] = event.position;
if (_activePointers.length == 2) {
final pointers = _activePointers.values.toList(growable: false);
_pinchStartDistance = (pointers[0] - pointers[1]).distance;
_pinchStartHourHeight = _hourHeight;
}
}
void _handlePointerMove(PointerMoveEvent event) {
if (!_activePointers.containsKey(event.pointer)) {
return;
}
_activePointers[event.pointer] = event.position;
if (_activePointers.length != 2 || _pinchStartDistance == null) {
return;
}
final pointers = _activePointers.values.toList(growable: false);
final currentDistance = (pointers[0] - pointers[1]).distance;
final startDistance = _pinchStartDistance!;
if (startDistance <= 0) {
return;
}
final nextHeight =
(_pinchStartHourHeight * (currentDistance / startDistance)).clamp(
_minHourHeight,
_maxHourHeight,
);
if ((nextHeight - _hourHeight).abs() < 0.1) {
return;
}
setState(() {
_hourHeight = nextHeight;
});
}
void _handlePointerUp(PointerUpEvent event) {
_activePointers.remove(event.pointer);
if (_activePointers.length < 2) {
_pinchStartDistance = null;
}
}
void _handlePointerCancel(PointerCancelEvent event) {
_activePointers.remove(event.pointer);
if (_activePointers.length < 2) {
_pinchStartDistance = null;
}
}
Widget _buildHeader() {
return SizedBox(
height: 68,
@@ -151,6 +262,31 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
),
),
const Spacer(),
if (!isSameDay(_selectedDate, DateTime.now()))
GestureDetector(
onTap: _goToToday,
child: Container(
height: 36,
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: AppColors.messageBtnWrap,
borderRadius: BorderRadius.circular(AppRadius.xl),
border: Border.all(color: AppColors.messageBtnBorder),
),
child: const Center(
child: Text(
'今天',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: AppColors.slate700,
),
),
),
),
),
if (!isSameDay(_selectedDate, DateTime.now()))
const SizedBox(width: 8),
GestureDetector(
onTap: () => CreateEventSheet.show(
context,
@@ -440,7 +576,7 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
bool isDisabled = false,
}) {
return SizedBox(
height: 34,
height: _hourHeight,
child: Row(
children: [
SizedBox(
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:lucide_icons/lucide_icons.dart';
import 'package:go_router/go_router.dart';
import '../../../../core/di/injection.dart';
import '../../../../core/notifications/local_notification_service.dart';
import '../../../../core/theme/design_tokens.dart';
import '../../data/services/mock_calendar_service.dart';
import '../../data/models/schedule_item_model.dart';
@@ -161,7 +162,10 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
const SizedBox(height: 14),
_buildDetailField('时间范围', timeStr),
const SizedBox(height: 14),
_buildDetailField('提醒时间', '开始前30分钟'),
_buildDetailField(
'提醒时间',
_formatReminderText(event.metadata?.reminderMinutes),
),
const SizedBox(height: 14),
_buildColorField(event.metadata?.color),
const SizedBox(height: 14),
@@ -176,8 +180,6 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
if (event.metadata?.notes != null) ...[
_buildNotesField(event.metadata!.notes!),
],
const SizedBox(height: 14),
_buildMetadataSection(event.metadata),
],
),
),
@@ -186,6 +188,16 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
);
}
String _formatReminderText(int? reminderMinutes) {
if (reminderMinutes == null) {
return '';
}
if (reminderMinutes == 0) {
return '准时提醒';
}
return '开始前$reminderMinutes分钟';
}
String _getWeekday(int weekday) {
const weekdays = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
return weekdays[weekday - 1];
@@ -290,6 +302,9 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
TextButton(
onPressed: () async {
await sl<CalendarService>().deleteEvent(widget.eventId);
await sl<LocalNotificationService>().cancelEventReminder(
widget.eventId,
);
if (!context.mounted) {
return;
}
@@ -385,40 +400,6 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
);
}
Widget _buildMetadataSection(ScheduleMetadata? metadata) {
final raw = metadata?.raw ?? const <String, dynamic>{};
if (raw.isEmpty) {
return _buildDetailField('metadata', '');
}
final rows = <String>[];
raw.forEach((key, value) {
rows.add('$key: $value');
});
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'metadata',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: AppColors.slate400,
),
),
const SizedBox(height: 6),
...rows.map(
(row) => Padding(
padding: const EdgeInsets.only(bottom: 4),
child: Text(
row,
style: const TextStyle(fontSize: 13, color: AppColors.slate700),
),
),
),
],
);
}
Color _parseColor(String? hex) {
if (hex == null || hex.isEmpty) return AppColors.blue600;
try {
@@ -20,16 +20,19 @@ class CalendarMonthScreen extends StatefulWidget {
State<CalendarMonthScreen> createState() => _CalendarMonthScreenState();
}
class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
class _CalendarMonthScreenState extends State<CalendarMonthScreen>
with WidgetsBindingObserver {
late final CalendarStateManager _calendarManager;
late DateTime _currentMonth;
late DateTime _selectedDate;
Key _eventsKey = UniqueKey();
final Map<String, List<ScheduleItemModel>> _eventsByDay = {};
String? _lastRoute;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_calendarManager = sl<CalendarStateManager>();
if (widget.resetToToday) {
@@ -40,6 +43,22 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
_selectedDate = savedDate;
_currentMonth = DateTime(savedDate.year, savedDate.month, 1);
_loadMonthEvents();
WidgetsBinding.instance.addPostFrameCallback((_) {
_lastRoute = GoRouterState.of(context).uri.toString();
});
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
final currentRoute = GoRouterState.of(context).uri.toString();
if (_lastRoute != null && _lastRoute != currentRoute) {
if (!currentRoute.contains('/events/')) {
_loadMonthEvents();
}
}
_lastRoute = currentRoute;
}
Future<void> _loadMonthEvents() async {
@@ -64,24 +83,45 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
setState(() {});
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_loadMonthEvents();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.todoBg,
body: SafeArea(
child: Column(
children: [
_buildHeader(),
Expanded(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(bottom: 84),
child: _buildMonthContent(),
body: PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) {
if (!didPop) {
context.go('/home');
}
},
child: SafeArea(
child: Column(
children: [
_buildHeader(),
Expanded(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(bottom: 84),
child: _buildMonthContent(),
),
),
),
),
_buildBottomDock(),
],
_buildBottomDock(),
],
),
),
),
);
@@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lucide_icons/lucide_icons.dart';
import '../../../../core/di/injection.dart';
import '../../../../core/notifications/local_notification_service.dart';
import '../../../../core/theme/design_tokens.dart';
import '../../data/models/schedule_item_model.dart';
import '../../data/services/mock_calendar_service.dart';
@@ -63,6 +64,7 @@ class _CreateEventSheetState extends State<CreateEventSheet>
DateTime? _endDate;
DateTime? _endTime;
String _selectedColor = '#3B82F6';
int? _reminderMinutes = 15;
bool _saving = false;
List<Attachment> _attachments = const [];
@@ -84,11 +86,13 @@ class _CreateEventSheetState extends State<CreateEventSheet>
_endDate = event.endAt;
_endTime = event.endAt;
_selectedColor = event.metadata?.color ?? '#3B82F6';
_reminderMinutes = event.metadata?.reminderMinutes ?? 15;
_attachments = List<Attachment>.from(
event.metadata?.attachments ?? const [],
);
} else {
final now = widget.initialDate ?? DateTime.now();
final now =
widget.initialDate ?? _roundToNearestMinute(DateTime.now(), 5);
_startDate = now;
_startTime = now;
_endDate = now;
@@ -108,6 +112,11 @@ class _CreateEventSheetState extends State<CreateEventSheet>
super.dispose();
}
DateTime _roundToNearestMinute(DateTime dt, int interval) {
final minute = (dt.minute / interval).round() * interval;
return DateTime(dt.year, dt.month, dt.day, dt.hour, minute % 60);
}
@override
Widget build(BuildContext context) {
return Container(
@@ -254,6 +263,8 @@ class _CreateEventSheetState extends State<CreateEventSheet>
const SizedBox(height: 20),
_buildTextField('地点', _locationController, '请输入地点'),
const SizedBox(height: 20),
_buildReminderPicker(),
const SizedBox(height: 20),
_buildColorPicker(),
const SizedBox(height: 20),
_buildAttachmentsSection(),
@@ -706,6 +717,68 @@ class _CreateEventSheetState extends State<CreateEventSheet>
);
}
Widget _buildReminderPicker() {
const options = <int?>[null, 0, 5, 10, 15, 30, 60, 120];
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),
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(AppRadius.lg),
border: Border.all(color: AppColors.border),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<int?>(
value: _reminderMinutes,
isExpanded: true,
items: options
.map(
(value) => DropdownMenuItem<int?>(
value: value,
child: Text(
labelOf(value),
style: const TextStyle(
fontSize: 14,
color: AppColors.slate700,
),
),
),
)
.toList(),
onChanged: (value) {
setState(() {
_reminderMinutes = value;
});
},
),
),
),
],
);
}
Future<void> _saveEvent() async {
if (_titleController.text.trim().isEmpty || _saving) return;
setState(() {
@@ -739,6 +812,7 @@ class _CreateEventSheetState extends State<CreateEventSheet>
notes: _notesController.text.trim().isNotEmpty
? _notesController.text.trim()
: null,
reminderMinutes: _reminderMinutes,
attachments: _attachments,
version: widget.editingEvent?.metadata?.version ?? 1,
);
@@ -758,22 +832,21 @@ class _CreateEventSheetState extends State<CreateEventSheet>
try {
final service = sl<CalendarService>();
debugPrint('CalendarService: $service');
debugPrint('Is mock: ${service.runtimeType}');
final notificationService = sl<LocalNotificationService>();
late final ScheduleItemModel saved;
if (_isEditing) {
await service.updateEvent(event);
saved = await service.updateEvent(event);
} else {
await service.addEvent(event);
saved = await service.addEvent(event);
}
await notificationService.upsertEventReminder(saved);
widget.onSaved?.call();
if (mounted) {
Navigator.pop(context);
}
} catch (e, stack) {
debugPrint('Save error: $e');
debugPrint('Stack: $stack');
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(
context,
@@ -45,13 +45,21 @@ class _TodoQuadrantsScreenState extends State<TodoQuadrantsScreen> {
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.todoBg,
body: SafeArea(
child: Column(
children: [
_buildHeader(),
Expanded(child: _buildContent()),
_buildBottomDock(),
],
body: PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) {
if (!didPop) {
context.go('/home');
}
},
child: SafeArea(
child: Column(
children: [
_buildHeader(),
Expanded(child: _buildContent()),
_buildBottomDock(),
],
),
),
),
);