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

This commit is contained in:
qzl
2026-03-11 15:28:29 +08:00
parent e55e445906
commit e20e7d2a02
85 changed files with 5175 additions and 885 deletions
@@ -35,6 +35,7 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
late List<DateTime> _monthDates;
final ScrollController _dayStripController = ScrollController();
Key _eventsKey = UniqueKey();
List<ScheduleItemModel> _events = const [];
@override
void initState() {
@@ -47,6 +48,7 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
_selectedDate = _calendarManager.selectedDate;
_updateMonthDates();
_loadEvents();
WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollToSelectedDate();
@@ -57,6 +59,16 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
_monthDates = monthDatesFor(_selectedDate);
}
Future<void> _loadEvents() async {
final events = await sl<CalendarService>().getEventsForDay(_selectedDate);
if (!mounted) {
return;
}
setState(() {
_events = events;
});
}
@override
void dispose() {
_dayStripController.dispose();
@@ -147,6 +159,7 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
setState(() {
_eventsKey = UniqueKey();
});
_loadEvents();
},
),
child: Container(
@@ -191,6 +204,7 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
_calendarManager.setSelectedDate(date);
_updateMonthDates();
_scrollToSelectedDate(animate: true);
_loadEvents();
},
child: SizedBox(
width: _dayItemWidth,
@@ -267,7 +281,7 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen> {
Widget _buildTimelineBoard() {
final now = DateTime.now();
final showCurrent = shouldShowCurrentMarker(_selectedDate, now);
final events = CalendarService().getEventsForDay(_selectedDate);
final events = _events;
final eventColumns = _calculateEventColumns(events);
@@ -1,6 +1,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/theme/design_tokens.dart';
import '../../data/services/mock_calendar_service.dart';
import '../../data/models/schedule_item_model.dart';
@@ -18,6 +19,7 @@ class CalendarEventDetailScreen extends StatefulWidget {
class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
ScheduleItemModel? _event;
bool _loading = true;
@override
void initState() {
@@ -25,17 +27,26 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
_loadEvent();
}
void _loadEvent() {
Future<void> _loadEvent() async {
try {
_event = CalendarService().getEventById(widget.eventId);
_event = await sl<CalendarService>().getEventById(widget.eventId);
} catch (e) {
_event = null;
} finally {
_loading = false;
}
if (mounted) {
setState(() {});
}
setState(() {});
}
@override
Widget build(BuildContext context) {
if (_loading) {
return const Scaffold(
body: SafeArea(child: Center(child: CircularProgressIndicator())),
);
}
if (_event == null) {
return Scaffold(
backgroundColor: const Color(0xFFF8FAFC),
@@ -165,6 +176,8 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
if (event.metadata?.notes != null) ...[
_buildNotesField(event.metadata!.notes!),
],
const SizedBox(height: 14),
_buildMetadataSection(event.metadata),
],
),
),
@@ -275,8 +288,11 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
child: const Text('取消'),
),
TextButton(
onPressed: () {
CalendarService().deleteEvent(widget.eventId);
onPressed: () async {
await sl<CalendarService>().deleteEvent(widget.eventId);
if (!context.mounted) {
return;
}
Navigator.pop(context);
context.pop();
},
@@ -369,6 +385,40 @@ 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 {
@@ -8,6 +8,7 @@ import '../calendar_state_manager.dart';
import '../calendar_time_utils.dart';
import '../widgets/bottom_dock.dart';
import '../widgets/create_event_sheet.dart';
import '../../data/models/schedule_item_model.dart';
import '../../data/services/mock_calendar_service.dart';
class CalendarMonthScreen extends StatefulWidget {
@@ -24,6 +25,7 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
late DateTime _currentMonth;
late DateTime _selectedDate;
Key _eventsKey = UniqueKey();
final Map<String, List<ScheduleItemModel>> _eventsByDay = {};
@override
void initState() {
@@ -37,6 +39,29 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
final savedDate = _calendarManager.selectedDate;
_selectedDate = savedDate;
_currentMonth = DateTime(savedDate.year, savedDate.month, 1);
_loadMonthEvents();
}
Future<void> _loadMonthEvents() async {
final start = DateTime(_currentMonth.year, _currentMonth.month, 1);
final end = DateTime(
_currentMonth.year,
_currentMonth.month + 1,
0,
23,
59,
59,
);
final events = await sl<CalendarService>().getEventsForRange(start, end);
if (!mounted) {
return;
}
_eventsByDay.clear();
for (final event in events) {
final key = formatYmd(event.startAt);
_eventsByDay[key] = [...(_eventsByDay[key] ?? const []), event];
}
setState(() {});
}
@override
@@ -102,6 +127,7 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
setState(() {
_eventsKey = UniqueKey();
});
_loadMonthEvents();
},
),
child: Container(
@@ -280,7 +306,7 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
}
final date = weekFirstDate.add(Duration(days: index));
final events = CalendarService().getEventsForDay(date);
final events = _eventsByDay[formatYmd(date)] ?? const [];
final displayEvents = events.take(2).toList();
final remainingCount = events.length - 2;
@@ -391,6 +417,7 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
1,
);
});
_loadMonthEvents();
},
children: List.generate(20, (index) {
return Center(child: Text('${2020 + index}'));
@@ -411,6 +438,7 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
1,
);
});
_loadMonthEvents();
},
children: List.generate(12, (index) {
return Center(child: Text('${index + 1}'));
@@ -1,6 +1,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/theme/design_tokens.dart';
import '../../data/models/schedule_item_model.dart';
import '../../data/services/mock_calendar_service.dart';
@@ -62,6 +63,8 @@ class _CreateEventSheetState extends State<CreateEventSheet>
DateTime? _endDate;
DateTime? _endTime;
String _selectedColor = '#3B82F6';
bool _saving = false;
List<Attachment> _attachments = const [];
bool get _isEditing => widget.editingEvent != null;
@@ -81,6 +84,9 @@ class _CreateEventSheetState extends State<CreateEventSheet>
_endDate = event.endAt;
_endTime = event.endAt;
_selectedColor = event.metadata?.color ?? '#3B82F6';
_attachments = List<Attachment>.from(
event.metadata?.attachments ?? const [],
);
} else {
final now = widget.initialDate ?? DateTime.now();
_startDate = now;
@@ -198,6 +204,26 @@ class _CreateEventSheetState extends State<CreateEventSheet>
setState(() {
_startDate = date;
_startTime = time;
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,
);
if (endDateTime.isBefore(startDateTime)) {
_endDate = date;
_endTime = time;
}
}
});
}),
const SizedBox(height: 20),
@@ -230,12 +256,289 @@ class _CreateEventSheetState extends State<CreateEventSheet>
const SizedBox(height: 20),
_buildColorPicker(),
const SizedBox(height: 20),
_buildAttachmentsSection(),
const SizedBox(height: 20),
_buildTextField('备注', _notesController, '请输入备注', maxLines: 3),
],
),
);
}
Widget _buildAttachmentsSection() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'附件',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: AppColors.slate700,
),
),
InkWell(
onTap: _showAddAttachmentDialog,
borderRadius: BorderRadius.circular(AppRadius.full),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: AppSpacing.md,
vertical: AppSpacing.xs,
),
decoration: BoxDecoration(
color: AppColors.blue50,
borderRadius: BorderRadius.circular(AppRadius.full),
border: Border.all(color: AppColors.borderQuaternary),
),
child: const Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(LucideIcons.plus, size: 14, color: AppColors.blue600),
SizedBox(width: AppSpacing.xs),
Text(
'添加附件',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: AppColors.blue600,
),
),
],
),
),
),
],
),
const SizedBox(height: AppSpacing.sm),
if (_attachments.isEmpty)
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: const Text(
'暂无附件,点击右上角添加',
style: TextStyle(color: AppColors.slate500, fontSize: 13),
),
),
..._attachments.asMap().entries.map((entry) {
final index = entry.key;
final item = entry.value;
return Container(
margin: const EdgeInsets.only(top: AppSpacing.sm),
padding: const EdgeInsets.all(AppSpacing.md),
decoration: BoxDecoration(
color: AppColors.white,
borderRadius: BorderRadius.circular(AppRadius.md),
border: Border.all(color: AppColors.messageCardBorder),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Text(
item.name,
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: AppColors.slate800,
),
),
),
Container(
padding: const EdgeInsets.symmetric(
horizontal: AppSpacing.sm,
vertical: AppSpacing.xs,
),
decoration: BoxDecoration(
color: AppColors.surfaceInfo,
borderRadius: BorderRadius.circular(AppRadius.full),
),
child: Text(
item.type,
style: const TextStyle(
fontSize: 11,
fontWeight: FontWeight.w600,
color: AppColors.blue600,
),
),
),
const SizedBox(width: AppSpacing.sm),
GestureDetector(
onTap: () {
setState(() {
final next = List<Attachment>.from(_attachments);
next.removeAt(index);
_attachments = next;
});
},
child: const Icon(
LucideIcons.trash,
size: 16,
color: AppColors.red500,
),
),
],
),
if ((item.url ?? '').isNotEmpty) ...[
const SizedBox(height: AppSpacing.xs),
Text(
'链接: ${item.url}',
style: const TextStyle(
fontSize: 12,
color: AppColors.slate500,
),
),
],
if ((item.note ?? '').isNotEmpty) ...[
const SizedBox(height: AppSpacing.xs),
Text(
'备注: ${item.note}',
style: const TextStyle(
fontSize: 12,
color: AppColors.slate500,
),
),
],
],
),
);
}),
],
);
}
Future<void> _showAddAttachmentDialog() async {
final nameController = TextEditingController();
final urlController = TextEditingController();
final noteController = TextEditingController();
final contentController = TextEditingController();
var type = 'document';
try {
final created = await showModalBottomSheet<Attachment>(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (sheetContext) => StatefulBuilder(
builder: (sheetContext, setSheetState) {
return Container(
padding: EdgeInsets.only(
left: AppSpacing.lg,
right: AppSpacing.lg,
top: AppSpacing.lg,
bottom:
MediaQuery.of(sheetContext).viewInsets.bottom +
AppSpacing.lg,
),
decoration: const BoxDecoration(
color: AppColors.white,
borderRadius: BorderRadius.vertical(
top: Radius.circular(AppRadius.xxl),
),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'添加附件',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w700,
color: AppColors.slate900,
),
),
const SizedBox(height: AppSpacing.md),
_buildTextField('名称', nameController, '例如:会议纪要.pdf'),
const SizedBox(height: AppSpacing.md),
_buildTextField('链接', urlController, 'https://...'),
const SizedBox(height: AppSpacing.md),
_buildTextField('备注', noteController, '备注信息'),
const SizedBox(height: AppSpacing.md),
_buildTextField('内容', contentController, '提醒内容', maxLines: 2),
const SizedBox(height: AppSpacing.md),
Wrap(
spacing: AppSpacing.sm,
children: ['document', 'reminder'].map((item) {
final selected = item == type;
return ChoiceChip(
label: Text(item),
selected: selected,
onSelected: (_) {
setSheetState(() {
type = item;
});
},
);
}).toList(),
),
const SizedBox(height: AppSpacing.lg),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: OutlinedButton(
onPressed: () => Navigator.pop(sheetContext),
child: const Text('取消'),
),
),
const SizedBox(width: AppSpacing.sm),
Expanded(
child: ElevatedButton(
onPressed: () {
final name = nameController.text.trim();
if (name.isEmpty) {
return;
}
Navigator.pop(
sheetContext,
Attachment(
name: name,
url: urlController.text.trim().isEmpty
? null
: urlController.text.trim(),
note: noteController.text.trim().isEmpty
? null
: noteController.text.trim(),
content: contentController.text.trim().isEmpty
? null
: contentController.text.trim(),
type: type,
),
);
},
child: const Text('确认添加'),
),
),
],
),
],
),
);
},
),
);
if (created != null && mounted) {
setState(() {
_attachments = [..._attachments, created];
});
}
} finally {
nameController.dispose();
urlController.dispose();
noteController.dispose();
contentController.dispose();
}
}
Widget _buildTextField(
String label,
TextEditingController controller,
@@ -295,63 +598,72 @@ class _CreateEventSheetState extends State<CreateEventSheet>
),
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: GestureDetector(
onTap: () => _showDatePicker(date, (newDate) {
onChanged(newDate, time);
}),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 12,
),
decoration: BoxDecoration(
color: const Color(0xFFF1F5F9),
borderRadius: BorderRadius.circular(10),
),
InkWell(
onTap: () async {
final picked = await _pickDateTime(date, time);
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),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(
LucideIcons.calendar,
size: 16,
color: AppColors.slate600,
),
const SizedBox(width: AppSpacing.sm),
Expanded(
child: Text(
'${date.year}${date.month}${date.day}',
_formatDateTimeLabel(date, time),
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: AppColors.slate900,
),
),
),
),
),
const SizedBox(width: 12),
Expanded(
child: GestureDetector(
onTap: () => _showTimePicker(time, (newTime) {
onChanged(date, newTime);
}),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 12,
),
decoration: BoxDecoration(
color: const Color(0xFFF1F5F9),
borderRadius: BorderRadius.circular(10),
),
child: Text(
'${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}',
style: const TextStyle(
fontSize: 15,
color: AppColors.slate900,
),
),
const Icon(
LucideIcons.chevronRight,
size: 16,
color: AppColors.slate400,
),
),
],
),
],
),
),
],
);
}
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,
DateTime time,
) async {
final result = await showModalBottomSheet<(DateTime, DateTime)>(
context: context,
backgroundColor: Colors.transparent,
isScrollControlled: true,
builder: (context) =>
_DateTimePickerSheet(initialDate: date, initialTime: time),
);
return result;
}
Widget _buildColorPicker() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
@@ -394,76 +706,11 @@ class _CreateEventSheetState extends State<CreateEventSheet>
);
}
void _showDatePicker(DateTime initial, Function(DateTime) onChanged) {
showModalBottomSheet(
context: context,
builder: (context) => Container(
height: 280,
color: Colors.white,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('取消'),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('确定'),
),
],
),
Expanded(
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
initialDateTime: initial,
onDateTimeChanged: onChanged,
),
),
],
),
),
);
}
void _showTimePicker(DateTime initial, Function(DateTime) onChanged) {
showModalBottomSheet(
context: context,
builder: (context) => Container(
height: 280,
color: Colors.white,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('取消'),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('确定'),
),
],
),
Expanded(
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.time,
initialDateTime: initial,
onDateTimeChanged: onChanged,
),
),
],
),
),
);
}
void _saveEvent() {
if (_titleController.text.trim().isEmpty) return;
Future<void> _saveEvent() async {
if (_titleController.text.trim().isEmpty || _saving) return;
setState(() {
_saving = true;
});
final startAt = DateTime(
_startDate.year,
@@ -492,6 +739,8 @@ class _CreateEventSheetState extends State<CreateEventSheet>
notes: _notesController.text.trim().isNotEmpty
? _notesController.text.trim()
: null,
attachments: _attachments,
version: widget.editingEvent?.metadata?.version ?? 1,
);
final event = ScheduleItemModel(
@@ -507,14 +756,338 @@ class _CreateEventSheetState extends State<CreateEventSheet>
metadata: metadata,
);
final service = CalendarService();
if (_isEditing) {
service.updateEvent(event);
} else {
service.addEvent(event);
}
try {
final service = sl<CalendarService>();
debugPrint('CalendarService: $service');
debugPrint('Is mock: ${service.runtimeType}');
widget.onSaved?.call();
Navigator.pop(context);
if (_isEditing) {
await service.updateEvent(event);
} else {
await service.addEvent(event);
}
widget.onSaved?.call();
if (mounted) {
Navigator.pop(context);
}
} catch (e, stack) {
debugPrint('Save error: $e');
debugPrint('Stack: $stack');
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('保存失败: $e')));
}
} finally {
if (mounted) {
setState(() {
_saving = false;
});
}
}
}
}
class _DateTimePickerSheet extends StatefulWidget {
final DateTime initialDate;
final DateTime initialTime;
const _DateTimePickerSheet({
required this.initialDate,
required this.initialTime,
});
@override
State<_DateTimePickerSheet> createState() => _DateTimePickerSheetState();
}
class _DateTimePickerSheetState extends State<_DateTimePickerSheet> {
late int _selectedYear;
late int _selectedMonth;
late int _selectedDay;
late int _selectedHour;
late int _selectedMinute;
late FixedExtentScrollController _yearController;
late FixedExtentScrollController _monthController;
late FixedExtentScrollController _dayController;
late FixedExtentScrollController _hourController;
late FixedExtentScrollController _minuteController;
static final int _baseYear = DateTime.now().year;
static final List<int> _years = List.generate(21, (i) => _baseYear - 10 + i);
static final List<int> _months = List.generate(12, (i) => i + 1);
static final List<int> _hours = List.generate(24, (i) => i);
static final List<int> _minutes = List.generate(60, (i) => i);
List<int> _days = [];
@override
void initState() {
super.initState();
_selectedYear = widget.initialDate.year;
_selectedMonth = widget.initialDate.month;
_selectedDay = widget.initialDate.day;
_selectedHour = widget.initialTime.hour;
_selectedMinute = widget.initialTime.minute;
_updateDays();
_yearController = FixedExtentScrollController(
initialItem: _years.indexOf(_selectedYear),
);
_monthController = FixedExtentScrollController(
initialItem: _selectedMonth - 1,
);
_dayController = FixedExtentScrollController(initialItem: _selectedDay - 1);
_hourController = FixedExtentScrollController(initialItem: _selectedHour);
_minuteController = FixedExtentScrollController(
initialItem: _selectedMinute,
);
}
void _updateDays() {
_days = List.generate(
DateTime(_selectedYear, _selectedMonth + 1, 0).day,
(i) => i + 1,
);
}
@override
void dispose() {
_yearController.dispose();
_monthController.dispose();
_dayController.dispose();
_hourController.dispose();
_minuteController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
height: 420,
decoration: const BoxDecoration(
color: AppColors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
),
child: Column(
children: [
_buildHeader(),
Expanded(
child: Row(
children: [
Expanded(
flex: 3,
child: Column(
children: [
_buildPickerLabel('日期'),
Expanded(
child: Row(
children: [
Expanded(
child: _buildPicker(_years, _yearController, (v) {
setState(() {
_selectedYear = v;
_updateDays();
if (_selectedDay > _days.length) {
_selectedDay = _days.length;
_dayController.jumpToItem(_selectedDay - 1);
}
});
}, (v) => '$v'),
),
const Text(
'',
style: TextStyle(
fontSize: 14,
color: AppColors.slate600,
),
),
Expanded(
child: _buildPicker(_months, _monthController, (
v,
) {
setState(() {
_selectedMonth = v;
_updateDays();
if (_selectedDay > _days.length) {
_selectedDay = _days.length;
_dayController.jumpToItem(_selectedDay - 1);
}
});
}, (v) => '$v'),
),
const Text(
'',
style: TextStyle(
fontSize: 14,
color: AppColors.slate600,
),
),
Expanded(
child: _buildPicker(
_days,
_dayController,
(v) => setState(() => _selectedDay = v),
(v) => '$v',
),
),
const Text(
'',
style: TextStyle(
fontSize: 14,
color: AppColors.slate600,
),
),
],
),
),
],
),
),
Container(width: 1, height: 180, color: AppColors.border),
Expanded(
flex: 2,
child: Column(
children: [
_buildPickerLabel('时间'),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: _buildPicker(
_hours,
_hourController,
(v) => setState(() => _selectedHour = v),
(v) => v.toString().padLeft(2, '0'),
itemExtent: 50,
),
),
const Text(
' : ',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: AppColors.slate600,
),
),
Expanded(
child: _buildPicker(
_minutes,
_minuteController,
(v) => setState(() => _selectedMinute = v),
(v) => v.toString().padLeft(2, '0'),
itemExtent: 50,
),
),
],
),
),
],
),
),
],
),
),
],
),
);
}
Widget _buildHeader() {
return Container(
height: 56,
padding: const EdgeInsets.symmetric(horizontal: 16),
decoration: const BoxDecoration(
border: Border(bottom: BorderSide(color: AppColors.border)),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
onTap: () => Navigator.pop(context),
child: const Text(
'取消',
style: TextStyle(fontSize: 17, color: AppColors.slate600),
),
),
const Text(
'选择时间',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w600,
color: AppColors.slate900,
),
),
GestureDetector(
onTap: () {
Navigator.pop(context, (
DateTime(_selectedYear, _selectedMonth, _selectedDay),
DateTime(2000, 1, 1, _selectedHour, _selectedMinute),
));
},
child: const Text(
'确定',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w600,
color: AppColors.blue600,
),
),
),
],
),
);
}
Widget _buildPickerLabel(String label) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 12),
child: Text(
label,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: AppColors.slate700,
),
),
);
}
Widget _buildPicker(
List<int> items,
FixedExtentScrollController controller,
ValueChanged<int> onChanged,
String Function(int) formatter, {
double itemExtent = 40,
}) {
return CupertinoPicker(
scrollController: controller,
itemExtent: itemExtent,
magnification: 1.2,
squeeze: 0.8,
useMagnifier: true,
onSelectedItemChanged: (index) => onChanged(items[index]),
selectionOverlay: Container(
decoration: BoxDecoration(
border: Border.symmetric(
horizontal: BorderSide(
color: AppColors.blue100.withValues(alpha: 0.5),
width: 1,
),
),
),
),
children: List<Widget>.generate(items.length, (index) {
return Center(
child: Text(
formatter(items[index]),
style: const TextStyle(fontSize: 18, color: AppColors.slate900),
),
);
}),
);
}
}