feat: 添加日历事件订阅者功能及权限重构

This commit is contained in:
zl-q
2026-03-30 09:05:50 +08:00
parent f126d7a547
commit 1aac62f39e
13 changed files with 498 additions and 74 deletions
@@ -149,6 +149,10 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
const SizedBox(height: AppSpacing.md),
_buildExtraSurface(event),
],
if (event.subscribers.isNotEmpty) ...[
const SizedBox(height: AppSpacing.md),
_buildSubscribersSurface(event),
],
],
),
),
@@ -465,6 +469,131 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
);
}
Widget _buildSubscribersSurface(ScheduleItemModel event) {
if (event.subscribers.isEmpty) {
return const SizedBox.shrink();
}
return Container(
padding: const EdgeInsets.all(AppSpacing.lg),
decoration: BoxDecoration(
color: _colorScheme.surface,
borderRadius: BorderRadius.circular(AppRadius.xl),
border: Border.all(color: _colorScheme.outlineVariant),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.l10n.calendarDetailSubscribers(event.subscribers.length),
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: _colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: AppSpacing.md),
...event.subscribers.map(
(sub) => _buildSubscriberRow(sub, event.ownerId),
),
],
),
);
}
Widget _buildSubscriberRow(Subscriber subscriber, String ownerId) {
final palette = Theme.of(context).extension<AppColorPalette>()!;
final avatarColor =
palette.avatarColors[subscriber.userId.hashCode.abs() %
palette.avatarColors.length];
final isOwner = subscriber.userId == ownerId;
return Padding(
padding: const EdgeInsets.symmetric(vertical: AppSpacing.xs),
child: Row(
children: [
Container(
width: 32,
height: 32,
decoration: BoxDecoration(
color: _colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(16),
),
child: subscriber.avatarUrl != null
? ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Image.network(
subscriber.avatarUrl!,
width: 32,
height: 32,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) =>
Icon(Icons.person, size: 16, color: avatarColor),
),
)
: Icon(Icons.person, size: 16, color: avatarColor),
),
const SizedBox(width: AppSpacing.sm),
Expanded(
child: Row(
children: [
Flexible(
child: Text(
subscriber.phone ??
subscriber.username ??
subscriber.userId.substring(0, 8),
style: TextStyle(
fontSize: 14,
color: _colorScheme.onSurface,
),
overflow: TextOverflow.ellipsis,
),
),
if (isOwner) ...[
const SizedBox(width: AppSpacing.xs),
Container(
padding: const EdgeInsets.symmetric(
horizontal: AppSpacing.xs,
vertical: 2,
),
decoration: BoxDecoration(
color: _colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(AppRadius.full),
),
child: Text(
context.l10n.calendarOwnerBadge,
style: TextStyle(
fontSize: 10,
fontWeight: FontWeight.w700,
color: _colorScheme.onPrimaryContainer,
),
),
),
],
],
),
),
if (subscriber.canEdit)
Padding(
padding: const EdgeInsets.only(left: AppSpacing.xs),
child: Icon(
LucideIcons.pencil,
size: 14,
color: _colorScheme.primary,
),
),
if (subscriber.canInvite)
Padding(
padding: const EdgeInsets.only(left: AppSpacing.xs),
child: Icon(
LucideIcons.userPlus,
size: 14,
color: _colorScheme.primary,
),
),
],
),
);
}
String _formatReminderText(int? reminderMinutes) {
if (reminderMinutes == null) {
return context.l10n.calendarDetailReminderNone;
@@ -1,9 +1,12 @@
import 'package:flutter/material.dart' hide BackButton;
import 'package:flutter/services.dart';
import 'package:social_app/core/l10n/l10n.dart';
import '../../../../app/di/injection.dart';
import '../../../../data/models/dial_codes.dart';
import '../../../../core/theme/design_tokens.dart';
import '../../../../shared/widgets/app_button.dart';
import '../../../../shared/widgets/phone_prefix_selector.dart';
import '../../../../shared/widgets/toast/toast.dart';
import '../../../../shared/widgets/toast/toast_type.dart';
import '../../data/apis/calendar_api.dart';
@@ -50,6 +53,7 @@ class CalendarShareDialog extends StatefulWidget {
class _CalendarShareDialogState extends State<CalendarShareDialog> {
final _phoneController = TextEditingController();
String _dialCode = kDialCodes.first.value;
final bool _permissionView = true;
bool _permissionEdit = false;
bool _permissionInvite = false;
@@ -73,13 +77,14 @@ class _CalendarShareDialogState extends State<CalendarShareDialog> {
return;
}
final fullPhone = '$_dialCode$phone';
setState(() => _isLoading = true);
try {
final api = sl<CalendarApi>();
await api.share(
widget.eventId,
phone: phone,
phone: fullPhone,
view: _permissionView,
edit: _permissionEdit,
invite: _permissionInvite,
@@ -147,14 +152,37 @@ class _CalendarShareDialogState extends State<CalendarShareDialog> {
const SizedBox(height: AppSpacing.lg),
TextField(
controller: _phoneController,
keyboardType: TextInputType.phone,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
LengthLimitingTextInputFormatter(14),
],
style: TextStyle(fontSize: 16, color: colorScheme.onSurface),
decoration: InputDecoration(
labelText: l10n.calendarSharePhoneLabel,
hintText: l10n.calendarSharePhoneHint,
filled: true,
fillColor: colorScheme.surface,
contentPadding: const EdgeInsets.symmetric(
horizontal: AppSpacing.lg,
vertical: AppSpacing.lg,
),
prefixIcon: PhonePrefixSelector(
value: _dialCode,
onChanged: (value) => setState(() => _dialCode = value),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(AppRadius.md),
borderRadius: BorderRadius.circular(AppRadius.lg),
borderSide: BorderSide.none,
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(AppRadius.lg),
borderSide: BorderSide(color: colorScheme.outlineVariant),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(AppRadius.lg),
borderSide: BorderSide(color: colorScheme.primary),
),
),
keyboardType: TextInputType.phone,
),
const SizedBox(height: AppSpacing.lg),
Text(
@@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:lucide_icons/lucide_icons.dart';
import 'package:social_app/core/l10n/l10n.dart';
import '../../../../app/di/injection.dart';
import '../../../../app/router/app_routes.dart';
import '../../../../core/theme/design_tokens.dart';
import '../../../../shared/widgets/app_loading_indicator.dart';
import '../../../../shared/widgets/app_selection_sheet.dart';
@@ -215,7 +217,7 @@ class _CreateEventSheetState extends State<CreateEventSheet>
title: _isEditing
? context.l10n.calendarCreateEditTitle
: context.l10n.calendarCreateNewTitle,
onBack: () => Navigator.of(context).pop(),
onBack: () => _closeSheet(),
trailing: ValueListenableBuilder<TextEditingValue>(
valueListenable: _titleController,
builder: (context, value, child) {
@@ -264,7 +266,7 @@ class _CreateEventSheetState extends State<CreateEventSheet>
width: AppSpacing.xxl * 2,
height: AppSpacing.xxl * 2,
child: TextButton(
onPressed: () => Navigator.pop(context),
onPressed: _closeSheet,
style: TextButton.styleFrom(
padding: const EdgeInsets.all(AppSpacing.none),
shape: RoundedRectangleBorder(
@@ -783,6 +785,8 @@ class _CreateEventSheetState extends State<CreateEventSheet>
metadata: metadata,
);
var saved = false;
try {
final service = sl<CalendarService>();
@@ -792,10 +796,9 @@ class _CreateEventSheetState extends State<CreateEventSheet>
await service.addEvent(event);
}
saved = true;
widget.onSaved?.call();
if (mounted) {
Navigator.pop(context, true);
}
} catch (e) {
if (mounted) {
Toast.show(
@@ -811,5 +814,29 @@ class _CreateEventSheetState extends State<CreateEventSheet>
});
}
}
if (saved && mounted) {
_closeSheet(result: true);
}
}
void _closeSheet({Object? result}) {
if (!mounted) {
return;
}
final navigator = Navigator.of(context);
if (navigator.canPop()) {
navigator.pop(result);
return;
}
final router = GoRouter.of(context);
if (router.canPop()) {
context.pop(result);
return;
}
context.go(AppRoutes.homeMain);
}
}