Files
ttstd_family_care/lib/features/profile/presentation/profile_tab.dart
T

464 lines
15 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:cupertino_ui/cupertino_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.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 '../../../features/device/data/device_providers.dart';
import '../../../features/device/domain/device_models.dart';
import '../../../l10n/app_localizations.dart';
import '../../auth/presentation/auth_controller.dart';
import 'profile_avatar.dart';
import 'profile_controller.dart';
/// 我的 Tab 内容(特性模块:profile / presentation)。
///
/// 对应移动端设计稿的「我的」页:个人资料头部、绑定设备、
/// 个人资料、设置列表与退出登录。登出委托给 authController。
class ProfileTab extends ConsumerWidget {
const ProfileTab({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final l10n = AppLocalizations.of(context);
final profileState = ref.watch(profileControllerProvider);
final profile = profileState.profile;
return AppPageScaffold(
child: SafeArea(
child: ListView(
padding: const EdgeInsets.only(bottom: 96),
children: [
// 个人资料头部卡片
CardBox(
padding: const EdgeInsets.all(18),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GestureDetector(
onTap: profileState.saving
? null
: () => context.push('/profile/edit'),
child: Stack(
alignment: Alignment.center,
children: [
ProfileAvatar(
url: profile?.avatar,
name: profile?.displayName ?? '',
),
if (profileState.loading && profile == null)
const Positioned.fill(
child: DecoratedBox(
decoration: BoxDecoration(
color: Color(0x55000000),
shape: BoxShape.circle,
),
child: CupertinoActivityIndicator(
color: CupertinoColors.white,
),
),
),
],
),
),
const SizedBox(width: 14),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
key: const ValueKey('profileIdentity'),
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
profile?.displayName.isNotEmpty == true
? profile!.displayName
: l10n.piName,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w700,
color: DarkMode.color(context, AppColors.ink),
),
),
const SizedBox(height: 4),
Text(
profile == null ? '' : 'ID: ${profile.id}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12.5,
color: DarkMode.color(context, AppColors.sub),
),
),
const SizedBox(height: 4),
Text(
profile?.signature ?? '',
key: const ValueKey('profileSignature'),
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12,
height: 1.3,
color: DarkMode.color(context, AppColors.sub),
),
),
],
),
),
GestureDetector(
onTap: profileState.saving
? null
: () => context.push('/profile/edit'),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 14,
vertical: 7,
),
decoration: BoxDecoration(
color: AppColors.green.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(20),
),
child: Text(
l10n.profileEdit,
style: const TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w600,
color: AppColors.green,
),
),
),
),
],
),
),
SectionTitle(
l10n.deviceBound,
action: l10n.deviceManage,
onAction: () => context.push('/device/manage'),
),
_boundDeviceCard(context, ref, l10n),
SectionTitle(l10n.settingsTitle),
CardBox(
padding: EdgeInsets.zero,
child: Column(
children: [
_settingRow(
context,
'🔔',
AppColors.teal,
l10n.setNotify,
'消息、设备状态提醒',
),
const AppDivider(),
_settingRow(
context,
'🔒',
const Color(0xFF22C55E),
l10n.setPrivacy,
'定位、相册、权限管理',
),
const AppDivider(),
_settingRow(
context,
'🛡️',
AppColors.red,
l10n.setSecurity,
'登录设备、密码',
onTap: () => context.push('/security'),
),
const AppDivider(),
_settingRow(context, '❓', AppColors.gray, l10n.setHelp, null),
const AppDivider(),
_settingRow(
context,
'ℹ️',
const Color(0xFF64748B),
l10n.setAbout,
'平板管家 v3.2.1',
),
],
),
),
GestureDetector(
onTap: () {
ref.read(authControllerProvider.notifier).logout();
context.go('/login');
},
child: Container(
margin: const EdgeInsets.fromLTRB(16, 18, 16, 0),
height: 48,
decoration: BoxDecoration(
color: DarkMode.color(context, AppColors.card),
borderRadius: BorderRadius.circular(14),
boxShadow: const [
BoxShadow(
color: Color(0x14141E3C),
blurRadius: 16,
offset: Offset(0, 4),
),
],
),
child: Center(
child: Text(
l10n.logout,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w700,
color: DarkMode.tone(
context,
AppColors.red,
const Color(0xFFFF8A94),
),
),
),
),
),
),
],
),
),
);
}
Widget _boundDeviceCard(
BuildContext context,
WidgetRef ref,
AppLocalizations l10n,
) {
final devicesAsync = ref.watch(myDevicesProvider);
final device = ref.watch(currentDeviceProvider);
return CardBox(
padding: EdgeInsets.zero,
gradient: const LinearGradient(
colors: [Color(0xFFEEF6FF), Color(0xFFF3FBF6)],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
child: SizedBox(
height:
28 +
((MediaQuery.textScalerOf(context).scale(14.5) * 1.3)
.ceilToDouble() +
2 +
(MediaQuery.textScalerOf(context).scale(12) * 1.3)
.ceilToDouble())
.clamp(36.0, double.infinity),
child: devicesAsync.when(
loading: () => const Padding(
padding: EdgeInsets.symmetric(vertical: 24),
child: Center(child: CupertinoActivityIndicator()),
),
error: (e, _) => Padding(
padding: const EdgeInsets.all(16),
child: Text(
'${l10n.deviceLoadFailed}:$e',
style: TextStyle(
fontSize: 12,
color: DarkMode.color(context, AppColors.sub),
),
),
),
data: (_) {
if (device == null) {
// 无绑定设备时也可点击进入设备管理页添加设备。
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => context.push('/device/manage'),
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Expanded(
child: Text(
l10n.deviceNone,
style: TextStyle(
fontSize: 13,
color: DarkMode.color(context, AppColors.sub),
),
),
),
Text(
'${l10n.deviceManageAdd} ›',
style: const TextStyle(
fontSize: 13,
color: AppColors.green,
),
),
],
),
),
);
}
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => context.push('/device/manage'),
child: _deviceCard(context, device, l10n),
);
},
),
),
);
}
Widget _deviceCard(
BuildContext context,
DeviceBrief device,
AppLocalizations l10n,
) {
final status = device.isOnline ? l10n.deviceOnline : l10n.deviceOffline;
final sub = device.snModel?.isNotEmpty == true
? '$status · ${device.snModel}'
: status;
return _deviceRow(
context,
'📱',
device.isOnline
? AppColors.green
: DarkMode.color(context, AppColors.track),
device.snName?.isNotEmpty == true ? device.snName! : device.serialno,
sub,
l10n.deviceManage,
);
}
Widget _deviceRow(
BuildContext context,
String emoji,
Color color,
String title,
String sub,
String? action,
) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
child: Row(
children: [
Container(
width: 36,
height: 36,
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(11),
),
child: Center(
child: Text(emoji, style: const TextStyle(fontSize: 17)),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 14.5,
height: 1.3,
fontWeight: FontWeight.w600,
color: DarkMode.color(context, AppColors.ink),
),
),
const SizedBox(height: 2),
Text(
sub,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12,
height: 1.3,
color: DarkMode.color(context, AppColors.sub),
),
),
],
),
),
if (action != null)
Text(
'$action ›',
style: TextStyle(
fontSize: 13,
color: DarkMode.color(context, AppColors.sub),
),
)
else
const Text(
'›',
style: TextStyle(fontSize: 16, color: Color(0xFFCBD2DC)),
),
],
),
);
Widget _settingRow(
BuildContext context,
String emoji,
Color color,
String label,
String? sub, {
VoidCallback? onTap,
}) {
final row = Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 13),
child: Row(
children: [
Container(
width: 36,
height: 36,
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(11),
),
child: Center(
child: Text(emoji, style: const TextStyle(fontSize: 17)),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: TextStyle(
fontSize: 14.5,
fontWeight: FontWeight.w600,
color: DarkMode.color(context, AppColors.ink),
),
),
if (sub != null) ...[
const SizedBox(height: 2),
Text(
sub,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12,
height: 1.3,
color: DarkMode.color(context, AppColors.sub),
),
),
],
],
),
),
const Text(
'›',
style: TextStyle(fontSize: 16, color: Color(0xFFCBD2DC)),
),
],
),
);
if (onTap == null) return row;
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: onTap,
child: row,
);
}
}