feat: refactor code
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../../core/locator/locator.dart';
|
||||
import '../../../../../core/network/api_provider_imp.dart';
|
||||
|
||||
class ProfileApiProvider {
|
||||
ApiProviderImp get _api => locator<ApiProviderImp>();
|
||||
|
||||
Future<Response> getMe() => _api.get('/me');
|
||||
Future<Response> getWallet() => _api.get('/wallet');
|
||||
Future<Response> getStats() => _api.get('/stats');
|
||||
|
||||
Future<Response> updateProfile(String firstName, String avatar) =>
|
||||
_api.post('/profile', body: {'first_name': firstName, 'avatar': avatar});
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import '../../domain/entities/profile_entity.dart';
|
||||
|
||||
/// نگاشتِ پاسخهای /me، /wallet و /stats به ProfileEntity.
|
||||
class ProfileModel {
|
||||
static ProfileEntity fromJson(
|
||||
Map<String, dynamic> user,
|
||||
Map<String, dynamic> wallet,
|
||||
Map<String, dynamic> stats,
|
||||
) {
|
||||
final name = (user['first_name'] as String?)?.trim();
|
||||
final avatar = (user['avatar'] as String?)?.trim();
|
||||
final s = stats['stats'] as Map?;
|
||||
return ProfileEntity(
|
||||
name: (name == null || name.isEmpty) ? 'بازیکن' : name,
|
||||
avatar: (avatar == null || avatar.isEmpty) ? 'hakem-1' : avatar,
|
||||
mobile: (user['mobile'] as String?) ?? '',
|
||||
level: (wallet['level'] ?? 1) as int,
|
||||
trophies: (wallet['trophies'] ?? 0) as int,
|
||||
xpInto: (wallet['xp_into_level'] ?? 0) as int,
|
||||
xpNext: (wallet['xp_for_next'] ?? 1) as int,
|
||||
vip: (stats['vip'] ?? false) as bool,
|
||||
stats: s == null
|
||||
? null
|
||||
: ProfileStats(
|
||||
games: (s['games'] ?? 0) as int,
|
||||
wins: (s['wins'] ?? 0) as int,
|
||||
losses: (s['losses'] ?? 0) as int,
|
||||
kotMade: (s['kot_made'] ?? 0) as int,
|
||||
kotReceived: (s['kot_received'] ?? 0) as int,
|
||||
cuts: (s['cuts'] ?? 0) as int,
|
||||
hakemCount: (s['hakem_count'] ?? 0) as int,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../core/error/custom_error.dart';
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../../domain/entities/profile_entity.dart';
|
||||
import '../../domain/repository/profile_repository.dart';
|
||||
import '../data_source/remote/profile_api_provider.dart';
|
||||
import '../model/profile_model.dart';
|
||||
|
||||
class ProfileRepositoryImpl extends ProfileRepository {
|
||||
final ProfileApiProvider api;
|
||||
ProfileRepositoryImpl(this.api);
|
||||
|
||||
@override
|
||||
Future<DataState<ProfileEntity>> getProfile() async {
|
||||
final results =
|
||||
await Future.wait([api.getMe(), api.getWallet(), api.getStats()]);
|
||||
final Response me = results[0];
|
||||
final Response wallet = results[1];
|
||||
final Response stats = results[2];
|
||||
if (me.statusCode == 200 &&
|
||||
wallet.statusCode == 200 &&
|
||||
stats.statusCode == 200) {
|
||||
return DataSuccess(ProfileModel.fromJson(
|
||||
Map<String, dynamic>.from((me.data['user'] ?? {}) as Map),
|
||||
Map<String, dynamic>.from(wallet.data as Map),
|
||||
Map<String, dynamic>.from(stats.data as Map),
|
||||
));
|
||||
}
|
||||
return DataError(errorConvertor(me.statusCode, null));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DataState<String>> updateProfile(ProfileParams params) async {
|
||||
final Response res = await api.updateProfile(params.firstName, params.avatar);
|
||||
if (res.statusCode == 200) return const DataSuccess('ok');
|
||||
final d = res.data;
|
||||
final msg = (d is Map && d['message'] != null) ? d['message'].toString() : null;
|
||||
return DataError(errorConvertor(res.statusCode, msg));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/// آمار بازیِ کاربر (در صورت قفل بودن، null است).
|
||||
class ProfileStats {
|
||||
final int games;
|
||||
final int wins;
|
||||
final int losses;
|
||||
final int kotMade;
|
||||
final int kotReceived;
|
||||
final int cuts;
|
||||
final int hakemCount;
|
||||
const ProfileStats({
|
||||
required this.games,
|
||||
required this.wins,
|
||||
required this.losses,
|
||||
required this.kotMade,
|
||||
required this.kotReceived,
|
||||
required this.cuts,
|
||||
required this.hakemCount,
|
||||
});
|
||||
}
|
||||
|
||||
/// موجودیتِ کاملِ پروفایل (نام/آواتار + خلاصهی اقتصادی + آمار).
|
||||
class ProfileEntity {
|
||||
final String name;
|
||||
final String avatar;
|
||||
final String mobile;
|
||||
final int level;
|
||||
final int trophies;
|
||||
final int xpInto;
|
||||
final int xpNext;
|
||||
final bool vip;
|
||||
final ProfileStats? stats; // null یعنی قفل (غیر VIP)
|
||||
|
||||
const ProfileEntity({
|
||||
required this.name,
|
||||
required this.avatar,
|
||||
required this.mobile,
|
||||
required this.level,
|
||||
required this.trophies,
|
||||
required this.xpInto,
|
||||
required this.xpNext,
|
||||
required this.vip,
|
||||
required this.stats,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../entities/profile_entity.dart';
|
||||
|
||||
abstract class ProfileRepository {
|
||||
Future<DataState<ProfileEntity>> getProfile();
|
||||
Future<DataState<String>> updateProfile(ProfileParams params);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../entities/profile_entity.dart';
|
||||
import '../repository/profile_repository.dart';
|
||||
|
||||
class GetProfileUseCase implements UseCase<DataState<ProfileEntity>, NoParams> {
|
||||
final ProfileRepository repository;
|
||||
GetProfileUseCase(this.repository);
|
||||
|
||||
@override
|
||||
Future<DataState<ProfileEntity>> call(NoParams params) =>
|
||||
repository.getProfile();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../repository/profile_repository.dart';
|
||||
|
||||
class SaveProfileUseCase implements UseCase<DataState<String>, ProfileParams> {
|
||||
final ProfileRepository repository;
|
||||
SaveProfileUseCase(this.repository);
|
||||
|
||||
@override
|
||||
Future<DataState<String>> call(ProfileParams params) =>
|
||||
repository.updateProfile(params);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../../domain/use_cases/get_profile_usecase.dart';
|
||||
import '../../domain/use_cases/save_profile_usecase.dart';
|
||||
import 'profile_event.dart';
|
||||
import 'profile_state.dart';
|
||||
import 'profile_status.dart';
|
||||
|
||||
class ProfileBloc extends Bloc<ProfileEvent, ProfileBlocState> {
|
||||
final GetProfileUseCase getProfileUseCase;
|
||||
final SaveProfileUseCase saveProfileUseCase;
|
||||
|
||||
ProfileBloc(this.getProfileUseCase, this.saveProfileUseCase)
|
||||
: super(ProfileBlocState.initial()) {
|
||||
on<LoadProfileEvent>((event, emit) => _load(emit));
|
||||
|
||||
on<SaveProfileEvent>((event, emit) async {
|
||||
emit(state.copyWith(saveStatus: ProfileSaveLoading()));
|
||||
final res = await saveProfileUseCase(
|
||||
ProfileParams(event.firstName, event.avatar));
|
||||
if (res is DataSuccess) {
|
||||
emit(state.copyWith(saveStatus: ProfileSaveSuccess()));
|
||||
await _load(emit);
|
||||
} else {
|
||||
emit(state.copyWith(saveStatus: ProfileSaveError(res.error!)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _load(Emitter<ProfileBlocState> emit) async {
|
||||
emit(state.copyWith(loadStatus: ProfileLoadLoading()));
|
||||
final res = await getProfileUseCase(const NoParams());
|
||||
if (res is DataSuccess) {
|
||||
emit(state.copyWith(loadStatus: ProfileLoadLoaded(res.data!)));
|
||||
} else {
|
||||
emit(state.copyWith(loadStatus: ProfileLoadError(res.error!)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
abstract class ProfileEvent {}
|
||||
|
||||
class LoadProfileEvent extends ProfileEvent {}
|
||||
|
||||
class SaveProfileEvent extends ProfileEvent {
|
||||
final String firstName;
|
||||
final String avatar;
|
||||
SaveProfileEvent(this.firstName, this.avatar);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'profile_status.dart';
|
||||
|
||||
class ProfileBlocState {
|
||||
final ProfileLoadStatus loadStatus;
|
||||
final ProfileSaveStatus saveStatus;
|
||||
|
||||
ProfileBlocState({required this.loadStatus, required this.saveStatus});
|
||||
|
||||
factory ProfileBlocState.initial() => ProfileBlocState(
|
||||
loadStatus: ProfileLoadInitial(),
|
||||
saveStatus: ProfileSaveIdle(),
|
||||
);
|
||||
|
||||
ProfileBlocState copyWith({
|
||||
ProfileLoadStatus? loadStatus,
|
||||
ProfileSaveStatus? saveStatus,
|
||||
}) =>
|
||||
ProfileBlocState(
|
||||
loadStatus: loadStatus ?? this.loadStatus,
|
||||
saveStatus: saveStatus ?? this.saveStatus,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import '../../domain/entities/profile_entity.dart';
|
||||
|
||||
abstract class ProfileLoadStatus {}
|
||||
|
||||
class ProfileLoadInitial extends ProfileLoadStatus {}
|
||||
|
||||
class ProfileLoadLoading extends ProfileLoadStatus {}
|
||||
|
||||
class ProfileLoadLoaded extends ProfileLoadStatus {
|
||||
final ProfileEntity profile;
|
||||
ProfileLoadLoaded(this.profile);
|
||||
}
|
||||
|
||||
class ProfileLoadError extends ProfileLoadStatus {
|
||||
final String message;
|
||||
ProfileLoadError(this.message);
|
||||
}
|
||||
|
||||
/// وضعیتِ ذخیرهی ویرایش پروفایل.
|
||||
abstract class ProfileSaveStatus {}
|
||||
|
||||
class ProfileSaveIdle extends ProfileSaveStatus {}
|
||||
|
||||
class ProfileSaveLoading extends ProfileSaveStatus {}
|
||||
|
||||
class ProfileSaveSuccess extends ProfileSaveStatus {}
|
||||
|
||||
class ProfileSaveError extends ProfileSaveStatus {
|
||||
final String message;
|
||||
ProfileSaveError(this.message);
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart' show LengthLimitingTextInputFormatter;
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:random_avatar/random_avatar.dart';
|
||||
|
||||
import '../../../../core/theme/app_theme.dart';
|
||||
import '../../../../core/widgets/game_ui.dart';
|
||||
import '../../../wallet/presentation/bloc/wallet_bloc.dart';
|
||||
import '../../../wallet/presentation/bloc/wallet_event.dart';
|
||||
import '../../domain/entities/profile_entity.dart';
|
||||
import '../bloc/profile_bloc.dart';
|
||||
import '../bloc/profile_event.dart';
|
||||
import '../bloc/profile_state.dart';
|
||||
import '../bloc/profile_status.dart';
|
||||
|
||||
/// صفحهی پروفایل: نام، آواتار (قابل ویرایش)، سطح، جام و آمارِ بازی (ویژهی VIP).
|
||||
class ProfileScreen extends StatelessWidget {
|
||||
const ProfileScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: GameBackground(
|
||||
child: SafeArea(
|
||||
child: BlocConsumer<ProfileBloc, ProfileBlocState>(
|
||||
listenWhen: (a, b) => a.saveStatus != b.saveStatus,
|
||||
listener: (context, state) {
|
||||
final s = state.saveStatus;
|
||||
if (s is ProfileSaveSuccess) {
|
||||
context.read<WalletBloc>().add(LoadWalletEvent());
|
||||
} else if (s is ProfileSaveError) {
|
||||
ScaffoldMessenger.of(context)
|
||||
.showSnackBar(SnackBar(content: Text(s.message)));
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
final st = state.loadStatus;
|
||||
if (st is ProfileLoadError) {
|
||||
return Center(
|
||||
child: Column(mainAxisSize: MainAxisSize.min, children: [
|
||||
Text(st.message,
|
||||
style: const TextStyle(color: Colors.white70)),
|
||||
const SizedBox(height: 12),
|
||||
GameButton(
|
||||
label: 'تلاش دوباره',
|
||||
onTap: () =>
|
||||
context.read<ProfileBloc>().add(LoadProfileEvent())),
|
||||
]),
|
||||
);
|
||||
}
|
||||
if (st is! ProfileLoadLoaded) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(color: AppColors.gold));
|
||||
}
|
||||
return _content(context, st.profile);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _editProfile(BuildContext context, ProfileEntity d) async {
|
||||
final result = await showModalBottomSheet<Map<String, String>>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
backgroundColor: Colors.transparent,
|
||||
builder: (_) => _EditProfileSheet(name: d.name, avatar: d.avatar),
|
||||
);
|
||||
if (result == null || !context.mounted) return;
|
||||
context
|
||||
.read<ProfileBloc>()
|
||||
.add(SaveProfileEvent(result['name']!, result['avatar']!));
|
||||
}
|
||||
|
||||
Widget _content(BuildContext context, ProfileEntity d) {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(children: [
|
||||
IconButton(
|
||||
onPressed: () => context.pop(),
|
||||
icon: const Icon(Icons.arrow_back, color: AppColors.gold),
|
||||
),
|
||||
const Spacer(),
|
||||
const GlowText('پروفایل', size: 24),
|
||||
const Spacer(),
|
||||
const SizedBox(width: 48),
|
||||
]),
|
||||
const SizedBox(height: 8),
|
||||
GamePanel(
|
||||
child: Column(children: [
|
||||
Stack(children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(6),
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: AppColors.gold, width: 2.5),
|
||||
),
|
||||
child: RandomAvatar(d.avatar, height: 92, width: 92),
|
||||
),
|
||||
Positioned(
|
||||
bottom: 0,
|
||||
right: 0,
|
||||
child: GestureDetector(
|
||||
onTap: () => _editProfile(context, d),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(6),
|
||||
decoration: const BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
gradient: LinearGradient(
|
||||
colors: [Color(0xFFFFD54F), Color(0xFFB8860B)]),
|
||||
),
|
||||
child: const Icon(Icons.edit,
|
||||
color: Color(0xFF3A0A12), size: 18),
|
||||
),
|
||||
),
|
||||
),
|
||||
]),
|
||||
const SizedBox(height: 10),
|
||||
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
||||
Flexible(child: GlowText(d.name, size: 22)),
|
||||
if (d.vip) ...[const SizedBox(width: 8), const _VipBadge()],
|
||||
]),
|
||||
if (d.mobile.isNotEmpty)
|
||||
Text(d.mobile,
|
||||
style:
|
||||
const TextStyle(color: Colors.white38, fontSize: 12)),
|
||||
const SizedBox(height: 14),
|
||||
Row(children: [
|
||||
Expanded(
|
||||
child: _MiniStat(
|
||||
icon: Icons.star, label: 'سطح', value: '${d.level}')),
|
||||
Expanded(
|
||||
child: _MiniStat(
|
||||
icon: Icons.emoji_events,
|
||||
label: 'جام',
|
||||
value: '${d.trophies}')),
|
||||
]),
|
||||
const SizedBox(height: 10),
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
child: LinearProgressIndicator(
|
||||
value: d.xpNext == 0 ? 0 : d.xpInto / d.xpNext,
|
||||
minHeight: 8,
|
||||
backgroundColor: Colors.white10,
|
||||
valueColor: const AlwaysStoppedAnimation(AppColors.gold),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Text('${d.xpInto} / ${d.xpNext} XP',
|
||||
style: const TextStyle(color: Colors.white38, fontSize: 11)),
|
||||
),
|
||||
]),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: GlowText('آمار بازی', size: 18)),
|
||||
const SizedBox(height: 8),
|
||||
_statsSection(context, d),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _statsSection(BuildContext context, ProfileEntity d) {
|
||||
final s = d.stats;
|
||||
final rows = <Widget>[
|
||||
_StatRow('بازی کل', s?.games, Icons.casino),
|
||||
_StatRow('برد کل', s?.wins, Icons.thumb_up),
|
||||
_StatRow('باخت کل', s?.losses, Icons.thumb_down),
|
||||
_StatRow('کُت کردن', s?.kotMade, Icons.flash_on),
|
||||
_StatRow('کُت شدن', s?.kotReceived, Icons.flash_off),
|
||||
_StatRow('بریدن', s?.cuts, Icons.bolt),
|
||||
_StatRow('دست حاکم', s?.hakemCount, Icons.workspace_premium),
|
||||
];
|
||||
final panel = GamePanel(child: Column(children: rows));
|
||||
if (d.vip) return panel;
|
||||
|
||||
return Stack(children: [
|
||||
Opacity(opacity: 0.35, child: IgnorePointer(child: panel)),
|
||||
Positioned.fill(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black.withValues(alpha: 0.45),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: AppColors.goldDark),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.lock, color: AppColors.gold, size: 36),
|
||||
const SizedBox(height: 8),
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 24),
|
||||
child: Text('مشاهدهی آمار ویژهی کاربران VIP است',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Colors.white, fontSize: 14)),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
GameButton(
|
||||
label: 'تهیه اشتراک VIP',
|
||||
icon: Icons.workspace_premium,
|
||||
colors: const [Color(0xFFFFC107), Color(0xFFB8860B)],
|
||||
onTap: () async {
|
||||
await context.push('/vip');
|
||||
if (context.mounted) {
|
||||
context.read<ProfileBloc>().add(LoadProfileEvent());
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
class _StatRow extends StatelessWidget {
|
||||
final String label;
|
||||
final Object? value;
|
||||
final IconData icon;
|
||||
const _StatRow(this.label, this.value, this.icon);
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 7),
|
||||
child: Row(children: [
|
||||
Icon(icon, color: AppColors.gold, size: 20),
|
||||
const SizedBox(width: 10),
|
||||
Text(label, style: const TextStyle(color: Colors.white, fontSize: 15)),
|
||||
const Spacer(),
|
||||
Text('${value ?? '—'}',
|
||||
style: const TextStyle(
|
||||
color: AppColors.gold,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold)),
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MiniStat extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final String label;
|
||||
final String value;
|
||||
const _MiniStat(
|
||||
{required this.icon, required this.label, required this.value});
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(children: [
|
||||
Icon(icon, color: AppColors.gold, size: 22),
|
||||
const SizedBox(height: 2),
|
||||
Text(value,
|
||||
style: const TextStyle(
|
||||
color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold)),
|
||||
Text(label, style: const TextStyle(color: Colors.white54, fontSize: 12)),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
class _VipBadge extends StatelessWidget {
|
||||
const _VipBadge();
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
||||
decoration: BoxDecoration(
|
||||
gradient:
|
||||
const LinearGradient(colors: [Color(0xFFFFD54F), Color(0xFFB8860B)]),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Text('VIP',
|
||||
style: TextStyle(
|
||||
color: Color(0xFF3A0A12),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 12)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// شیتِ ویرایش نام و آواتار (با تأیید، مقدار جدید را برمیگرداند).
|
||||
class _EditProfileSheet extends StatefulWidget {
|
||||
final String name;
|
||||
final String avatar;
|
||||
const _EditProfileSheet({required this.name, required this.avatar});
|
||||
|
||||
@override
|
||||
State<_EditProfileSheet> createState() => _EditProfileSheetState();
|
||||
}
|
||||
|
||||
class _EditProfileSheetState extends State<_EditProfileSheet> {
|
||||
late final TextEditingController _name;
|
||||
late final List<String> _seeds;
|
||||
late String _selected;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_name = TextEditingController(text: widget.name);
|
||||
_seeds = List.generate(12, (i) => 'hakem-${i + 1}');
|
||||
if (!_seeds.contains(widget.avatar)) _seeds.insert(0, widget.avatar);
|
||||
_selected = widget.avatar;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_name.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool get _valid => _name.text.trim().length >= 2;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
|
||||
child: Container(
|
||||
decoration: const BoxDecoration(
|
||||
color: AppColors.bgDark,
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
|
||||
border: Border(top: BorderSide(color: AppColors.gold, width: 2)),
|
||||
),
|
||||
padding: const EdgeInsets.all(18),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(mainAxisSize: MainAxisSize.min, children: [
|
||||
const GlowText('ویرایش پروفایل', size: 20),
|
||||
const SizedBox(height: 14),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(4),
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: AppColors.gold, width: 2),
|
||||
),
|
||||
child: RandomAvatar(_selected, height: 72, width: 72),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _name,
|
||||
textAlign: TextAlign.center,
|
||||
maxLength: 20,
|
||||
inputFormatters: [LengthLimitingTextInputFormatter(20)],
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'نام نمایشی (۲ تا ۲۰ حرف)', counterText: ''),
|
||||
onChanged: (_) => setState(() {}),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
const Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child:
|
||||
Text('انتخاب آواتار', style: TextStyle(color: AppColors.gold)),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
GridView.count(
|
||||
crossAxisCount: 4,
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
mainAxisSpacing: 10,
|
||||
crossAxisSpacing: 10,
|
||||
children: [
|
||||
for (final s in _seeds)
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _selected = s),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: AppColors.panel,
|
||||
border: Border.all(
|
||||
color: _selected == s
|
||||
? AppColors.gold
|
||||
: Colors.transparent,
|
||||
width: 2.5,
|
||||
),
|
||||
),
|
||||
child: RandomAvatar(s),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
GameButton(
|
||||
label: 'تأیید',
|
||||
width: double.infinity,
|
||||
colors: const [Color(0xFF3FA34D), Color(0xFF1B5E20)],
|
||||
onTap: _valid
|
||||
? () => Navigator.pop(
|
||||
context, {'name': _name.text.trim(), 'avatar': _selected})
|
||||
: null,
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user