- 引入 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
286 lines
9.5 KiB
Dart
286 lines
9.5 KiB
Dart
import 'package:ttstd_family_care/core/network/cached_network_image.dart';
|
||
import 'package:azlistview_plus/azlistview_plus.dart';
|
||
import 'package:cupertino_ui/cupertino_ui.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
import 'package:lpinyin/lpinyin.dart';
|
||
import 'package:material_ui/material_ui.dart';
|
||
|
||
import 'package:ttstd_family_care/core/widgets/feature_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/features/contacts/presentation/contact_detail_page.dart';
|
||
import 'package:ttstd_family_care/features/device/presentation/device_switcher.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 '../../device/data/device_providers.dart';
|
||
|
||
/// 联系人列表的 A-Z 索引包装项。
|
||
class _ContactAzItem implements ISuspensionBean {
|
||
_ContactAzItem(this.contact);
|
||
|
||
final Contact contact;
|
||
|
||
@override
|
||
bool isShowSuspension = false;
|
||
|
||
/// 紧急联系人归入「★ 常用」分组并置顶;其余按姓名首字母(拼音首字母)分组。
|
||
@override
|
||
String getSuspensionTag() {
|
||
if (contact.emergency) return '★';
|
||
final name = contact.displayName.trim();
|
||
if (name.isEmpty) return '#';
|
||
final raw = PinyinHelper.getFirstWordPinyin(name).toUpperCase();
|
||
if (raw.isNotEmpty) {
|
||
final first = raw[0];
|
||
if (first.codeUnitAt(0) >= 65 && first.codeUnitAt(0) <= 90) {
|
||
return first;
|
||
}
|
||
}
|
||
return '#';
|
||
}
|
||
}
|
||
|
||
/// 联系人页面(家属端)。
|
||
///
|
||
/// 数据来源于 [ClientContactController](`/api/v1/client/sn/contact`)。
|
||
/// 分组规则:紧急联系人(`emergency=true`)归入「★ 常用」分组置顶,其余按姓名首字母(拼音首字母)分入 A-Z 索引分组,右侧提供字母索引条快速跳转。
|
||
class ContactsPage extends ConsumerStatefulWidget {
|
||
const ContactsPage({super.key});
|
||
|
||
@override
|
||
ConsumerState<ContactsPage> createState() => _ContactsPageState();
|
||
}
|
||
|
||
class _ContactsPageState extends ConsumerState<ContactsPage> {
|
||
|
||
Future<void> _refresh() async {
|
||
ref.invalidate(contactsProvider);
|
||
await ref.read(contactsProvider.future);
|
||
}
|
||
|
||
void _onTap(Contact contact) {
|
||
showContactDetailDialog(context, contact);
|
||
}
|
||
|
||
Future<void> _onAdd() async {
|
||
final sn = ref.read(selectedDeviceSnProvider);
|
||
if (sn == null || sn.isEmpty) return;
|
||
final result = await context.push<Contact?>('/contacts/edit');
|
||
if (result == null || !mounted) return;
|
||
|
||
final l10n = AppLocalizations.of(context);
|
||
final repo = ref.read(contactRepositoryProvider);
|
||
final err = await repo.addContact(sn, result);
|
||
if (!mounted) return;
|
||
if (err != null) {
|
||
AppDialogs.alert(context, message: err, confirmText: l10n.commonConfirm);
|
||
return;
|
||
}
|
||
AppDialogs.alert(
|
||
context,
|
||
message: l10n.contactAdded,
|
||
confirmText: l10n.commonConfirm,
|
||
);
|
||
_refresh();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final l10n = AppLocalizations.of(context);
|
||
final sn = ref.watch(selectedDeviceSnProvider);
|
||
final contactsAsync = ref.watch(contactsProvider);
|
||
|
||
return AppPageScaffold(
|
||
backgroundColor: DarkMode.color(context, AppColors.surface),
|
||
navigationBar: CupertinoNavigationBar(
|
||
middle: DeviceContextTitle(l10n.contactsTitle),
|
||
trailing: CupertinoButton(
|
||
padding: EdgeInsets.zero,
|
||
onPressed: sn != null && sn.isNotEmpty ? _onAdd : null,
|
||
child: const Icon(CupertinoIcons.add, size: 26),
|
||
),
|
||
),
|
||
child: SafeArea(
|
||
child: contactsAsync.when(
|
||
loading: () => const Center(child: CupertinoActivityIndicator()),
|
||
error: (e, _) => Center(
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
Text('$e'),
|
||
const SizedBox(height: 12),
|
||
CupertinoButton(
|
||
onPressed: _refresh,
|
||
child: Text(l10n.commonRetry),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
data: (contacts) {
|
||
if (sn == null || sn.isEmpty) {
|
||
return Center(child: Text(l10n.contactNoDevice));
|
||
}
|
||
if (contacts.isEmpty) {
|
||
return Center(
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
Text(l10n.contactListEmpty),
|
||
const SizedBox(height: 12),
|
||
CupertinoButton(
|
||
onPressed: _onAdd,
|
||
child: Text(l10n.commonAdd),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
final items = contacts
|
||
.map((c) => _ContactAzItem(c))
|
||
.toList()
|
||
..sort((a, b) {
|
||
// 「★ 常用」置顶,其余按分组标签(字母)排序。
|
||
if (a.getSuspensionTag() == '★') return -1;
|
||
if (b.getSuspensionTag() == '★') return 1;
|
||
return a.getSuspensionTag().compareTo(b.getSuspensionTag());
|
||
});
|
||
SuspensionUtil.setShowSuspensionStatus(items);
|
||
|
||
final indexBarData = _buildIndexBarData(items);
|
||
|
||
return AzListView(
|
||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||
data: items,
|
||
itemCount: items.length,
|
||
indexBarData: indexBarData,
|
||
indexBarMargin: const EdgeInsets.symmetric(vertical: 8),
|
||
susItemHeight: 36,
|
||
itemBuilder: (ctx, index) => _ContactTile(
|
||
contact: items[index].contact,
|
||
onTap: () => _onTap(items[index].contact),
|
||
),
|
||
susItemBuilder: (ctx, index) =>
|
||
_buildSusHeader(context, items[index].getSuspensionTag()),
|
||
);
|
||
},
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 根据索引项实际出现的分组标签,生成右侧 IndexBar 数据(★ 置顶,A-Z 居中,# 兜底)。
|
||
List<String> _buildIndexBarData(List<_ContactAzItem> items) {
|
||
final tags = items.map((e) => e.getSuspensionTag()).toSet();
|
||
const letters = <String>[
|
||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
||
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
|
||
];
|
||
final ordered = <String>[];
|
||
if (tags.contains('★')) ordered.add('★');
|
||
for (final l in letters) {
|
||
if (tags.contains(l)) ordered.add(l);
|
||
}
|
||
if (tags.contains('#')) ordered.add('#');
|
||
return ordered;
|
||
}
|
||
|
||
/// 分组头(吸顶),★ 分组显示「★ 常用」文案。
|
||
Widget _buildSusHeader(BuildContext context, String tag) {
|
||
final title = tag == '★' ? '★ 常用' : tag;
|
||
return SizedBox(
|
||
width: MediaQuery.of(context).size.width,
|
||
height: 36,
|
||
child: Container(
|
||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||
alignment: Alignment.centerLeft,
|
||
color: DarkMode.color(context, AppColors.surface),
|
||
child: Text(
|
||
title,
|
||
style: TextStyle(
|
||
fontSize: 13,
|
||
fontWeight: FontWeight.w600,
|
||
color: DarkMode.color(context, AppColors.sub),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 单个联系人条目(Cupertino 风格),点击进入详情页。
|
||
class _ContactTile extends StatelessWidget {
|
||
const _ContactTile({
|
||
required this.contact,
|
||
required this.onTap,
|
||
});
|
||
|
||
final Contact contact;
|
||
final VoidCallback onTap;
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final url = contact.avatar;
|
||
final hasAvatar = url != null && url.isNotEmpty;
|
||
|
||
final avatar = hasAvatar
|
||
? CircleAvatar(
|
||
radius: 22,
|
||
backgroundImage: cachedNetworkImage(url),
|
||
onBackgroundImageError: (_, _) {},
|
||
)
|
||
: CircleAvatar(
|
||
radius: 22,
|
||
backgroundColor: AppColors.green.withValues(alpha: 0.15),
|
||
child: Text(
|
||
contact.displayName.isNotEmpty
|
||
? contact.displayName.characters.first
|
||
: '?',
|
||
style: const TextStyle(color: AppColors.green, fontSize: 16),
|
||
),
|
||
);
|
||
|
||
return GestureDetector(
|
||
onTap: onTap,
|
||
behavior: HitTestBehavior.opaque,
|
||
child: Padding(
|
||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||
child: Row(
|
||
children: [
|
||
avatar,
|
||
const SizedBox(width: 12),
|
||
Expanded(
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Text(
|
||
contact.displayName,
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: TextStyle(
|
||
fontSize: 14.5,
|
||
fontWeight: FontWeight.w600,
|
||
color: DarkMode.color(context, AppColors.ink),
|
||
),
|
||
),
|
||
const SizedBox(height: 2),
|
||
Text(
|
||
contact.phoneNumber,
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: TextStyle(fontSize: 12, color: DarkMode.color(context, AppColors.sub)),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|