feat(profile): 实现个人资料编辑与策略本地缓存
- 将资料编辑页重构为可交互表单,支持昵称、签名编辑与头像更换 - 新增 DioClient 本地缓存读取与写入方法,支持离线读取策略快照 - 设备策略切换后自动使本地缓存失效,避免数据不一致 - Home Tab 切换时选中态跟随可见页面,滑动过程中实时更新底栏高亮 - 系统设置页引入 loading 遮罩层,防止切换开关时重复操作 - 个人资料页接入 profileController 展示真实用户信息与加载状态 - 适配深色模式,修正 home_tab 中头像与文字颜色硬编码问题
This commit is contained in:
1 parent
5f84a64c3e
commit
e06e12fbbc
30 files changed
+2059
-336
No files matched your search
@@ -1,144 +1,182 @@
|
||||
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 '../../../app/theme/dark_mode.dart';
|
||||
import '../../../core/widgets/app_page_scaffold.dart';
|
||||
import '../../../core/widgets/feature_ui.dart';
|
||||
import '../../../core/widgets/settings_ui.dart';
|
||||
import '../../../l10n/app_localizations.dart';
|
||||
import '../domain/profile_models.dart';
|
||||
import 'profile_avatar.dart';
|
||||
import 'profile_controller.dart';
|
||||
|
||||
/// 个人资料页(我的 → 编辑资料)。
|
||||
///
|
||||
/// 头像、昵称、个性签名的独立展示页面;从「我的」页头部的编辑资料入口进入。
|
||||
class ProfileEditPage extends StatelessWidget {
|
||||
class ProfileEditPage extends ConsumerStatefulWidget {
|
||||
const ProfileEditPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
ConsumerState<ProfileEditPage> createState() => _ProfileEditPageState();
|
||||
}
|
||||
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (didPop) return;
|
||||
if (context.canPop()) {
|
||||
context.pop();
|
||||
} else {
|
||||
context.go('/profile');
|
||||
}
|
||||
},
|
||||
child: AppPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(
|
||||
middle: Text(l10n.profileInfo),
|
||||
leading: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
child: const Icon(CupertinoIcons.back),
|
||||
onPressed: () => context.pop(),
|
||||
class _ProfileEditPageState extends ConsumerState<ProfileEditPage> {
|
||||
final _nickname = TextEditingController();
|
||||
final _signature = TextEditingController();
|
||||
bool _initialized = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_fill(ref.read(profileControllerProvider).profile);
|
||||
}
|
||||
|
||||
void _fill(UserProfile? profile) {
|
||||
if (_initialized || profile == null) return;
|
||||
_nickname.text = profile.nickname;
|
||||
_signature.text = profile.signature;
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nickname.dispose();
|
||||
_signature.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _alert(String message) {
|
||||
showCupertinoDialog<void>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoAlertDialog(
|
||||
content: Text(message),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(AppLocalizations.of(context).commonConfirm),
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.only(bottom: 24),
|
||||
children: [
|
||||
SectionTitle(l10n.profileInfo),
|
||||
CardBox(
|
||||
padding: EdgeInsets.zero,
|
||||
child: Column(
|
||||
children: [
|
||||
_infoRow(
|
||||
context,
|
||||
'🖼️',
|
||||
AppColors.purple,
|
||||
l10n.piAvatar,
|
||||
const _AvatarPreview(),
|
||||
),
|
||||
const AppDivider(),
|
||||
_infoRow(
|
||||
context,
|
||||
'📛',
|
||||
AppColors.blue,
|
||||
l10n.piName,
|
||||
const Text('用户名'),
|
||||
),
|
||||
const AppDivider(),
|
||||
_infoRow(
|
||||
context,
|
||||
'✍️',
|
||||
AppColors.orange,
|
||||
l10n.piSign,
|
||||
const Text('热爱生活'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _infoRow(
|
||||
BuildContext context,
|
||||
String emoji,
|
||||
Color color,
|
||||
String label,
|
||||
Widget trailing,
|
||||
) =>
|
||||
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),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 14.5,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: DarkMode.color(context, AppColors.ink),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
DefaultTextStyle.merge(
|
||||
style: TextStyle(fontSize: 13, color: DarkMode.color(context, AppColors.sub)),
|
||||
child: trailing,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 资料页头像预览(与「我的」页头部样式一致)。
|
||||
class _AvatarPreview extends StatelessWidget {
|
||||
const _AvatarPreview();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Container(
|
||||
width: 34,
|
||||
height: 34,
|
||||
decoration: const BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
gradient: LinearGradient(
|
||||
colors: [AppColors.green, AppColors.blue],
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
),
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final state = ref.watch(profileControllerProvider);
|
||||
ref.listen(profileControllerProvider, (previous, next) {
|
||||
_fill(next.profile);
|
||||
if (next.alert == null && next.issue == null) return;
|
||||
final message = switch (next.issue) {
|
||||
ProfileIssue.nicknameInvalid => l10n.profileNicknameInvalid,
|
||||
ProfileIssue.signatureInvalid => l10n.profileSignatureInvalid,
|
||||
ProfileIssue.avatarTooLarge => l10n.profileAvatarTooLarge,
|
||||
ProfileIssue.avatarUnsupported => l10n.profileAvatarUnsupported,
|
||||
null => next.alert!,
|
||||
};
|
||||
ref.read(profileControllerProvider.notifier).consumeAlert();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) _alert(message);
|
||||
});
|
||||
});
|
||||
final profile = state.profile;
|
||||
return AppPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(
|
||||
middle: Text(l10n.profileEdit),
|
||||
leading: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: state.saving ? null : () => context.pop(),
|
||||
child: const Icon(CupertinoIcons.back),
|
||||
),
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'U',
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: CupertinoColors.white,
|
||||
),
|
||||
),
|
||||
trailing: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: state.saving || profile == null
|
||||
? null
|
||||
: () async {
|
||||
final saved = await ref
|
||||
.read(profileControllerProvider.notifier)
|
||||
.save(_nickname.text, _signature.text);
|
||||
if (saved && context.mounted) context.pop();
|
||||
},
|
||||
child: state.saving
|
||||
? const CupertinoActivityIndicator()
|
||||
: Text(l10n.commonSave),
|
||||
),
|
||||
);
|
||||
}
|
||||
),
|
||||
child: SafeArea(
|
||||
child: profile == null
|
||||
? Center(
|
||||
child: state.loading
|
||||
? const CupertinoActivityIndicator()
|
||||
: CupertinoButton(
|
||||
onPressed: ref
|
||||
.read(profileControllerProvider.notifier)
|
||||
.reload,
|
||||
child: Text(l10n.commonRetry),
|
||||
),
|
||||
)
|
||||
: ListView(
|
||||
padding: const EdgeInsets.only(top: 16, bottom: 32),
|
||||
children: [
|
||||
Center(
|
||||
child: CupertinoButton(
|
||||
onPressed: state.saving
|
||||
? null
|
||||
: ref
|
||||
.read(profileControllerProvider.notifier)
|
||||
.changeAvatar,
|
||||
child: Column(
|
||||
children: [
|
||||
ProfileAvatar(
|
||||
url: profile.avatar,
|
||||
name: profile.displayName,
|
||||
size: 84,
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Text(l10n.profileChangeAvatar),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
LabeledField(
|
||||
label: l10n.piName,
|
||||
controller: _nickname,
|
||||
maxLength: 64,
|
||||
enabled: !state.saving,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 8,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
l10n.piSign,
|
||||
style: TextStyle(
|
||||
color: DarkMode.color(context, AppColors.sub),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
CupertinoTextField(
|
||||
controller: _signature,
|
||||
maxLength: 200,
|
||||
maxLines: 3,
|
||||
enabled: !state.saving,
|
||||
padding: const EdgeInsets.all(14),
|
||||
style: TextStyle(
|
||||
color: DarkMode.color(context, AppColors.ink),
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: DarkMode.color(context, AppColors.card),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user