Files
ttstd_family_care/lib/features/profile/presentation/profile_edit_page.dart
T
tongtong e06e12fbbc feat(profile): 实现个人资料编辑与策略本地缓存
- 将资料编辑页重构为可交互表单,支持昵称、签名编辑与头像更换
- 新增 DioClient 本地缓存读取与写入方法,支持离线读取策略快照
- 设备策略切换后自动使本地缓存失效,避免数据不一致
- Home Tab 切换时选中态跟随可见页面,滑动过程中实时更新底栏高亮
- 系统设置页引入 loading 遮罩层,防止切换开关时重复操作
- 个人资料页接入 profileController 展示真实用户信息与加载状态
- 适配深色模式,修正 home_tab 中头像与文字颜色硬编码问题
2026-11-09 09:24:51 +08:00

183 lines
6.3 KiB
Dart

import 'package:cupertino_ui/cupertino_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.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 ConsumerStatefulWidget {
const ProfileEditPage({super.key});
@override
ConsumerState<ProfileEditPage> createState() => _ProfileEditPageState();
}
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),
),
],
),
);
}
@override
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),
),
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),
),
),
],
),
),
],
),
),
);
}
}