games_app/prediqo_mobile/lib/features/wallet/presentation/account_screen.dart
2026-10-25 16:42:52 +02:00

448 lines
17 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import '../../../core/errors/app_exception.dart';
import '../../../core/theme/app_theme.dart';
import '../../../data/repositories/auth_repository_impl.dart';
import '../../../domain/repositories/repositories.dart';
import '../../../l10n/app_strings.dart';
import '../../../shared/widgets/pred_card.dart';
import '../../../shared/widgets/pred_skeleton.dart';
import '../../../shared/widgets/prediqo_logo.dart';
import '../../../shared/widgets/section_error.dart';
import '../../auth/bloc/auth_bloc.dart';
class AccountScreen extends StatefulWidget {
const AccountScreen({super.key});
@override
State<AccountScreen> createState() => _AccountScreenState();
}
class _AccountScreenState extends State<AccountScreen> {
bool _profileLoading = true;
bool _profileRetrying = false;
String? _profileError;
UserProfile? _profile;
Future<void>? _profileInFlight;
@override
void initState() {
super.initState();
final cached = context.read<AuthRepository>().peekLastProfile();
if (cached != null) {
_profile = cached;
_profileLoading = false;
}
_loadProfile();
}
Future<void> _loadProfile({bool force = false}) {
if (_profileInFlight != null) return _profileInFlight!;
final keep = _profile != null && !force;
final future = () async {
if (!keep) {
setState(() {
_profileLoading = true;
// Keep SectionError mounted during retry so [retrying] can show.
if (!force) _profileError = null;
_profileRetrying = force;
});
}
try {
final profile =
await context.read<AuthRepository>().getProfile(force: force);
if (!mounted) return;
setState(() {
_profile = profile;
_profileLoading = false;
_profileError = null;
_profileRetrying = false;
});
} catch (_) {
if (!mounted) return;
setState(() {
_profileLoading = false;
_profileRetrying = false;
if (_profile == null) {
_profileError = AppException.connectionFailedMessage;
}
});
}
}();
_profileInFlight = future.whenComplete(() => _profileInFlight = null);
return _profileInFlight!;
}
String _displayName(AppStrings s) {
final p = _profile;
if (p == null) return s.accountTitle;
final parts = [
p.firstName?.trim(),
p.middleName?.trim(),
p.lastName?.trim(),
].whereType<String>().where((v) => v.isNotEmpty).toList();
if (parts.isNotEmpty) return parts.join(' ');
final email = p.email?.trim();
if (email != null && email.isNotEmpty) return email;
return s.accountTitle;
}
String _initials() {
final p = _profile;
if (p == null) return '?';
final first = (p.firstName ?? '').trim();
final last = (p.lastName ?? '').trim();
if (first.isNotEmpty && last.isNotEmpty) {
return '${first[0]}${last[0]}'.toUpperCase();
}
if (first.isNotEmpty) return first[0].toUpperCase();
final email = (p.email ?? '').trim();
if (email.isNotEmpty) return email[0].toUpperCase();
return '?';
}
Future<void> _openProfileEdit() async {
await context.push('/account/profile');
if (mounted) await _loadProfile(force: true);
}
@override
Widget build(BuildContext context) {
final s = context.strings;
final isDark = Theme.of(context).brightness == Brightness.dark;
final name = _displayName(s);
final email = _profile?.email?.trim();
final phone = _profile?.phone?.trim();
final country = _profile?.countryOfResidence?.trim();
final showHeaderSkeleton = _profileLoading && _profile == null;
final bottomInset = MediaQuery.paddingOf(context).bottom;
return BlocListener<AuthBloc, AuthState>(
listenWhen: (prev, curr) => prev.status != curr.status,
listener: (context, state) {
if (state.status == AuthStatus.unauthenticated && context.mounted) {
context.go('/welcome');
}
},
child: AnnotatedRegion<SystemUiOverlayStyle>(
value: const SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.light,
statusBarBrightness: Brightness.dark,
),
child: Scaffold(
key: const Key('account_screen'),
body: Column(
children: [
DecoratedBox(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xFF059669),
Color(0xFF047857),
Color(0xFF0F172A),
],
),
),
child: SafeArea(
bottom: false,
child: SizedBox(
height: kToolbarHeight,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
children: [
const PrediqoLogo(size: 28, showWordmark: false),
const SizedBox(width: 10),
Expanded(
child: Text(
s.accountTitle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontWeight: FontWeight.w900,
letterSpacing: 0.3,
color: Colors.white,
fontSize: 18,
),
),
),
],
),
),
),
),
),
Expanded(
child: RefreshIndicator(
onRefresh: () => _loadProfile(force: true),
child: ListView(
physics: const AlwaysScrollableScrollPhysics(
parent: BouncingScrollPhysics(),
),
padding: EdgeInsets.fromLTRB(20, 16, 20, 24 + bottomInset),
children: [
if (_profileError != null && _profile == null) ...[
SectionError(
message: _profileError!,
retrying: _profileRetrying,
onRetry: () => _loadProfile(force: true),
),
const SizedBox(height: 20),
] else ...[
Center(
child: showHeaderSkeleton
? const PredSkeleton(
width: 88,
height: 88,
borderRadius: 44,
)
: Container(
width: 88,
height: 88,
alignment: Alignment.center,
decoration: BoxDecoration(
gradient: AppTheme.goldAccentGradient,
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: AppColors.goldPrimary
.withValues(alpha: 0.35),
blurRadius: 16,
offset: const Offset(0, 4),
),
],
),
child: Text(
_initials(),
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.w800,
color: isDark
? AppColors.navy
: Colors.white,
),
),
),
),
const SizedBox(height: 16),
if (showHeaderSkeleton) ...[
const Center(
child: PredSkeletonLine(width: 160, height: 22),
),
const SizedBox(height: 8),
const Center(
child: PredSkeletonLine(width: 200, height: 14),
),
] else ...[
Text(
name,
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.titleLarge
?.copyWith(fontWeight: FontWeight.bold),
),
if (email != null &&
email.isNotEmpty &&
email != name) ...[
const SizedBox(height: 4),
Text(
email,
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(
color: isDark
? AppColors.textSecondary
: const Color(0xFF475569),
),
),
],
if ((phone != null && phone.isNotEmpty) ||
(country != null && country.isNotEmpty) ||
_profile?.totpEnabled == true) ...[
const SizedBox(height: 12),
Wrap(
alignment: WrapAlignment.center,
spacing: 8,
runSpacing: 8,
children: [
if (phone != null && phone.isNotEmpty)
_InfoChip(
icon: Icons.phone_outlined,
label: phone,
),
if (country != null && country.isNotEmpty)
_InfoChip(
icon: Icons.public_outlined,
label: country,
),
if (_profile?.totpEnabled == true)
_InfoChip(
icon: Icons.verified_user_outlined,
label: s.twoFactorEnabledShort,
),
],
),
] else if (email == null || email.isEmpty) ...[
const SizedBox(height: 4),
Text(
s.accountSubtitle,
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(
color: isDark
? AppColors.textSecondary
: const Color(0xFF475569),
),
),
],
],
const SizedBox(height: 28),
],
_AccountTile(
icon: Icons.badge_outlined,
title: s.updateProfile,
onTap: _openProfileEdit,
),
const SizedBox(height: 10),
_AccountTile(
icon: Icons.lock_reset_rounded,
title: s.changePassword,
onTap: () => context.push('/account/change-password'),
),
const SizedBox(height: 10),
_AccountTile(
icon: Icons.security_rounded,
title: s.twoFactor,
onTap: () => context.push('/totp-setup'),
),
const SizedBox(height: 10),
_AccountTile(
icon: Icons.card_giftcard_rounded,
title: s.referralProgram,
onTap: () => context.push('/referral'),
),
const SizedBox(height: 10),
_AccountTile(
icon: Icons.help_outline_rounded,
title: s.helpFaq,
onTap: () => context.push('/faq'),
),
const SizedBox(height: 10),
_AccountTile(
icon: Icons.logout_rounded,
title: s.logout,
isDestructive: true,
onTap: () => context
.read<AuthBloc>()
.add(const AuthLogoutRequested()),
),
],
),
),
),
],
),
),
),
);
}
}
class _InfoChip extends StatelessWidget {
const _InfoChip({required this.icon, required this.label});
final IconData icon;
final String label;
@override
Widget build(BuildContext context) {
final color = Theme.of(context).colorScheme.primary;
return Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: color.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(20),
border: Border.all(color: color.withValues(alpha: 0.25)),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon, size: 14, color: color),
const SizedBox(width: 6),
Text(
label,
style: Theme.of(context).textTheme.labelMedium?.copyWith(
color: color,
fontWeight: FontWeight.w600,
),
),
],
),
);
}
}
class _AccountTile extends StatelessWidget {
const _AccountTile({
required this.icon,
required this.title,
required this.onTap,
this.isDestructive = false,
});
final IconData icon;
final String title;
final VoidCallback onTap;
final bool isDestructive;
@override
Widget build(BuildContext context) {
final isDark = Theme.of(context).brightness == Brightness.dark;
final color = isDestructive
? Theme.of(context).colorScheme.error
: Theme.of(context).colorScheme.primary;
return PredCard(
onTap: onTap,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: color.withValues(alpha: isDark ? 0.15 : 0.12),
borderRadius: BorderRadius.circular(12),
),
child: Icon(icon, color: color, size: 24),
),
const SizedBox(width: 16),
Expanded(
child: Text(
title,
style: Theme.of(context).textTheme.titleSmall?.copyWith(
fontWeight: FontWeight.w600,
color: color,
),
),
),
Icon(
Icons.chevron_right_rounded,
color: isDestructive
? color
: (isDark ? AppColors.textMuted : const Color(0xFF94A3B8)),
),
],
),
);
}
}