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 createState() => _ProfileEditPageState(); } class _ProfileEditPageState extends ConsumerState { 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( 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), ), ), ], ), ), ], ), ), ); } }