- 引入 DarkMode 工具类,提供系统栏、颜色、色调的暗色适配能力 - AppColors 调色板改为 CupertinoDynamicColor.withBrightness 自适应 - app.dart 增加 AnnotatedRegion 与 Material Theme 包裹以联动暗色系统栏 - 移除 system_ui_util 中硬编码的浅色图标配置,改由主题驱动 - CardBox、AppDivider、SectionTitle 等公共组件适配深色卡片与分割线 - settings_ui、change_mobile、change_password、login_devices、wechat_binding 等页面全面接入 DarkMode 颜色 - dio_client 新增 allowStale 响应缓存机制,网络异常时回退本地缓存 - token_storage 增加 currentAccountId(JWT 解析)及按用户隔离的设备偏好存取 - ApiException 改为通过私有构造持有真实 AppError 实例,appError getter 返回结构化错误 - 闹钟模块迁移至 FutureProvider.autoDispose.family,消除手动 loading 状态管理 - 闹钟编辑页 CupertinoTimerPicker 替换为 CupertinoDatePicker(24h 格式),标签输入改为 TextEditingController
249 lines
7.7 KiB
Dart
249 lines
7.7 KiB
Dart
import 'package:cupertino_ui/cupertino_ui.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:material_ui/material_ui.dart';
|
|
|
|
import 'package:ttstd_family_care/core/widgets/settings_ui.dart';
|
|
import 'package:ttstd_family_care/features/contacts/domain/contact_models.dart';
|
|
import 'package:ttstd_family_care/features/contacts/data/contact_providers.dart';
|
|
import 'package:ttstd_family_care/l10n/app_localizations.dart';
|
|
import 'package:ttstd_family_care/app/theme/dark_mode.dart';
|
|
import 'package:ttstd_family_care/core/widgets/app_page_scaffold.dart';
|
|
|
|
import '../../../core/widgets/feature_ui.dart';
|
|
import '../../device/data/device_providers.dart';
|
|
|
|
/// 新增/编辑联系人整页。
|
|
///
|
|
/// 表单收集结果后以 [context.pop] 返回 [Contact],由调用方(列表页 / 详情对话框)
|
|
/// 决定新增还是更新;删除成功后返回 [deletedContactSentinel]。
|
|
class ContactEditPage extends ConsumerStatefulWidget {
|
|
const ContactEditPage({this.contact, super.key});
|
|
|
|
final Contact? contact;
|
|
|
|
@override
|
|
ConsumerState<ContactEditPage> createState() => _ContactEditPageState();
|
|
}
|
|
|
|
class _ContactEditPageState extends ConsumerState<ContactEditPage> {
|
|
final _nameController = TextEditingController();
|
|
final _nickController = TextEditingController();
|
|
final _phoneController = TextEditingController();
|
|
late bool _emergency;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final c = widget.contact;
|
|
_nameController.text = c?.name ?? '';
|
|
_nickController.text = c?.nickName ?? '';
|
|
_phoneController.text = c?.phoneNumber ?? '';
|
|
_emergency = c?.emergency ?? false;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_nameController.dispose();
|
|
_nickController.dispose();
|
|
_phoneController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _save() {
|
|
final name = _nameController.text.trim();
|
|
final phone = _phoneController.text.trim();
|
|
if (name.isEmpty) {
|
|
AppDialogs.alert(
|
|
context,
|
|
message: AppLocalizations.of(context).nameRequired,
|
|
confirmText: AppLocalizations.of(context).commonConfirm,
|
|
);
|
|
return;
|
|
}
|
|
if (phone.isEmpty) {
|
|
AppDialogs.alert(
|
|
context,
|
|
message: AppLocalizations.of(context).phoneRequired,
|
|
confirmText: AppLocalizations.of(context).commonConfirm,
|
|
);
|
|
return;
|
|
}
|
|
final nick = _nickController.text.trim();
|
|
final updated = Contact(
|
|
id: widget.contact?.id ?? 0,
|
|
name: name,
|
|
nickName: nick.isEmpty ? null : nick,
|
|
phoneNumber: phone,
|
|
avatar: widget.contact?.avatar,
|
|
position: widget.contact?.position ?? 0,
|
|
emergency: _emergency,
|
|
showDesktop: widget.contact?.showDesktop ?? true,
|
|
bindPhone: widget.contact?.bindPhone,
|
|
bindSn: widget.contact?.bindSn,
|
|
createTime: widget.contact?.createTime,
|
|
updateTime: widget.contact?.updateTime,
|
|
);
|
|
context.pop(updated);
|
|
}
|
|
|
|
Future<void> _delete() async {
|
|
final c = widget.contact;
|
|
if (c == null) return;
|
|
final l10n = AppLocalizations.of(context);
|
|
final sn = ref.read(selectedDeviceSnProvider);
|
|
if (sn == null || sn.isEmpty) return;
|
|
|
|
final ok = await AppDialogs.confirm(
|
|
context,
|
|
message: l10n.deleteConfirm(c.name),
|
|
confirmText: l10n.commonDelete,
|
|
cancelText: l10n.commonCancel,
|
|
);
|
|
if (!ok || !mounted) return;
|
|
|
|
final repo = ref.read(contactRepositoryProvider);
|
|
final err = await repo.deleteContact(sn, c.id);
|
|
if (!mounted) return;
|
|
if (err != null) {
|
|
AppDialogs.alert(
|
|
context,
|
|
message: err,
|
|
confirmText: l10n.commonConfirm,
|
|
);
|
|
return;
|
|
}
|
|
ref.invalidate(contactsProvider);
|
|
context.pop(deletedContactSentinel);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final c = widget.contact;
|
|
final isEdit = c != null;
|
|
return AppPageScaffold(
|
|
backgroundColor: DarkMode.color(context, AppColors.surface),
|
|
navigationBar: CupertinoNavigationBar(
|
|
middle: Text(isEdit ? l10n.commonEdit : l10n.commonAdd),
|
|
leading: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
onPressed: () => context.pop(),
|
|
child: Text(l10n.commonCancel),
|
|
),
|
|
trailing: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
onPressed: _save,
|
|
child: Text(l10n.commonSave),
|
|
),
|
|
),
|
|
child: SafeArea(
|
|
child: ListView(
|
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
|
children: [
|
|
if (c != null)
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 16),
|
|
child: Center(
|
|
child: _EditAvatar(name: c.name),
|
|
),
|
|
),
|
|
LabeledField(
|
|
label: l10n.contactName,
|
|
controller: _nameController,
|
|
placeholder: l10n.contactName,
|
|
),
|
|
LabeledField(
|
|
label: l10n.contactNick,
|
|
controller: _nickController,
|
|
placeholder: l10n.contactNick,
|
|
),
|
|
LabeledField(
|
|
label: l10n.contactPhone,
|
|
controller: _phoneController,
|
|
placeholder: l10n.contactPhone,
|
|
keyboardType: TextInputType.phone,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Container(
|
|
color: DarkMode.color(context, AppColors.surface),
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: Text(
|
|
l10n.contactEmergency,
|
|
style: TextStyle(
|
|
fontSize: 14.5,
|
|
fontWeight: FontWeight.w600,
|
|
color: DarkMode.color(context, AppColors.ink),
|
|
),
|
|
),
|
|
),
|
|
CupertinoSwitch(
|
|
value: _emergency,
|
|
onChanged: (v) => setState(() => _emergency = v),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
child: PrimaryButton(
|
|
text: isEdit ? l10n.commonSave : l10n.commonAdd,
|
|
onPressed: _save,
|
|
),
|
|
),
|
|
if (isEdit) ...[
|
|
const SizedBox(height: 12),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
child: PrimaryButton(
|
|
text: l10n.commonDelete,
|
|
color: AppColors.red,
|
|
onPressed: _delete,
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 编辑页顶部由姓名首字生成的头像(与详情对话框一致的样式)。
|
|
class _EditAvatar extends StatelessWidget {
|
|
const _EditAvatar({required this.name});
|
|
|
|
final String name;
|
|
|
|
static const List<Color> _palette = [
|
|
Color(0xFFF2994A),
|
|
Color(0xFFEB5757),
|
|
Color(0xFF27AE60),
|
|
Color(0xFF2D9CDB),
|
|
Color(0xFF9B51E0),
|
|
Color(0xFF56CCF2),
|
|
Color(0xFF6FCF97),
|
|
Color(0xFFF2C94C),
|
|
];
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final text = name.trim();
|
|
final first = text.isNotEmpty ? text.characters.first.toUpperCase() : '?';
|
|
final index = text.isEmpty ? 0 : (text.codeUnitAt(0) % _palette.length);
|
|
final color = _palette[index];
|
|
return CircleAvatar(
|
|
radius: 36,
|
|
backgroundColor: color.withValues(alpha: 0.15),
|
|
child: Text(
|
|
first,
|
|
style: TextStyle(color: color, fontSize: 28, fontWeight: FontWeight.w600),
|
|
),
|
|
);
|
|
}
|
|
}
|