feat: get cards from backend

This commit is contained in:
2026-06-24 08:35:31 +03:30
parent d837aca96e
commit 108a972761
83 changed files with 593 additions and 38 deletions
@@ -0,0 +1,10 @@
import 'package:dio/dio.dart';
import '../../../../../core/locator/locator.dart';
import '../../../../../core/network/api_provider_imp.dart';
class RankedApiProvider {
ApiProviderImp get _api => locator<ApiProviderImp>();
Future<Response> getLeaderboard() => _api.get('/leaderboard');
}
@@ -0,0 +1,22 @@
import 'package:dio/dio.dart';
import '../../../../core/error/custom_error.dart';
import '../../../../core/resources/data_state.dart';
import '../../domain/entities/leaderboard.dart';
import '../../domain/repository/ranked_repository.dart';
import '../data_source/remote/ranked_api_provider.dart';
class RankedRepositoryImpl extends RankedRepository {
final RankedApiProvider api;
RankedRepositoryImpl(this.api);
@override
Future<DataState<Leaderboard>> getLeaderboard() async {
final Response res = await api.getLeaderboard();
if (res.statusCode == 200) {
return DataSuccess(
Leaderboard.fromJson(Map<String, dynamic>.from(res.data as Map)));
}
return DataError(errorConvertor(res.statusCode, null));
}
}
@@ -0,0 +1,29 @@
/// یک ردیف از جدولِ رتبه‌بندی.
class RankEntry {
final int rank;
final String name;
final String avatar;
final int rankPoints;
final String tier; // bronze..king
RankEntry.fromJson(Map<String, dynamic> j)
: rank = (j['rank'] ?? 0) as int,
name = (j['name'] ?? '') as String,
avatar = (j['avatar'] ?? '') as String,
rankPoints = (j['rank_points'] ?? 0) as int,
tier = (j['tier'] ?? 'bronze') as String;
}
/// جدولِ رتبه‌بندیِ فصلِ جاری.
class Leaderboard {
final int season;
final List<RankEntry> entries;
const Leaderboard(this.season, this.entries);
factory Leaderboard.fromJson(Map<String, dynamic> j) => Leaderboard(
(j['season'] ?? 1) as int,
((j['entries'] as List?) ?? [])
.map((e) => RankEntry.fromJson(Map<String, dynamic>.from(e as Map)))
.toList(),
);
}
@@ -0,0 +1,6 @@
import '../../../../core/resources/data_state.dart';
import '../entities/leaderboard.dart';
abstract class RankedRepository {
Future<DataState<Leaderboard>> getLeaderboard();
}
@@ -0,0 +1,14 @@
import '../../../../core/resources/data_state.dart';
import '../../../../core/usecase/use_case.dart';
import '../entities/leaderboard.dart';
import '../repository/ranked_repository.dart';
class GetLeaderboardUseCase
implements UseCase<DataState<Leaderboard>, NoParams> {
final RankedRepository repository;
GetLeaderboardUseCase(this.repository);
@override
Future<DataState<Leaderboard>> call(NoParams params) =>
repository.getLeaderboard();
}
@@ -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/entities/leaderboard.dart';
import '../../domain/use_cases/get_leaderboard_usecase.dart';
abstract class LeaderboardEvent {}
class LoadLeaderboardEvent extends LeaderboardEvent {}
abstract class LeaderboardState {}
class LeaderboardInitial extends LeaderboardState {}
class LeaderboardLoading extends LeaderboardState {}
class LeaderboardLoaded extends LeaderboardState {
final Leaderboard data;
LeaderboardLoaded(this.data);
}
class LeaderboardError extends LeaderboardState {
final String message;
LeaderboardError(this.message);
}
class LeaderboardBloc extends Bloc<LeaderboardEvent, LeaderboardState> {
final GetLeaderboardUseCase getLeaderboardUseCase;
LeaderboardBloc(this.getLeaderboardUseCase) : super(LeaderboardInitial()) {
on<LoadLeaderboardEvent>((event, emit) async {
emit(LeaderboardLoading());
final res = await getLeaderboardUseCase(const NoParams());
if (res is DataSuccess) {
emit(LeaderboardLoaded(res.data!));
} else {
emit(LeaderboardError(res.error!));
}
});
}
}
@@ -0,0 +1,132 @@
import 'package:flutter/material.dart';
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 '../../../../core/widgets/rank_badge.dart';
import '../../domain/entities/leaderboard.dart';
import '../bloc/leaderboard_bloc.dart';
/// جدولِ رتبه‌بندیِ فصل: برترین بازیکنان بر اساس امتیازِ رتبه.
class LeaderboardScreen extends StatelessWidget {
const LeaderboardScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: GameBackground(
child: SafeArea(
child: Column(
children: [
Row(children: [
IconButton(
onPressed: () => context.pop(),
icon: const Icon(Icons.arrow_back, color: AppColors.gold),
),
const Spacer(),
const GlowText('رتبه‌بندی فصل', size: 22),
const Spacer(),
const SizedBox(width: 48),
]),
Expanded(
child: BlocBuilder<LeaderboardBloc, LeaderboardState>(
builder: (context, state) {
if (state is LeaderboardError) {
return Center(
child: Column(mainAxisSize: MainAxisSize.min, children: [
Text(state.message,
style: const TextStyle(color: Colors.white70)),
TextButton(
onPressed: () => context
.read<LeaderboardBloc>()
.add(LoadLeaderboardEvent()),
child: const Text('تلاش مجدد'),
),
]),
);
}
if (state is! LeaderboardLoaded) {
return const Center(
child:
CircularProgressIndicator(color: AppColors.gold));
}
final lb = state.data;
if (lb.entries.isEmpty) {
return const Center(
child: Text('هنوز کسی در این فصل امتیاز نگرفته است',
style: TextStyle(color: Colors.white54)),
);
}
return Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Text('فصل ${lb.season}',
style: const TextStyle(
color: AppColors.gold, fontSize: 14)),
),
Expanded(
child: ListView.separated(
padding: const EdgeInsets.fromLTRB(12, 4, 12, 20),
itemCount: lb.entries.length,
separatorBuilder: (_, __) =>
const SizedBox(height: 8),
itemBuilder: (_, i) => _row(lb.entries[i]),
),
),
],
);
},
),
),
],
),
),
),
);
}
Widget _row(RankEntry e) {
final medal = e.rank <= 3;
return Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Color(0xFF4A0C16), Color(0xFF2A0710)],
),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: medal ? AppColors.gold : AppColors.goldDark,
width: medal ? 1.6 : 1),
),
child: Row(children: [
SizedBox(
width: 28,
child: Text('${e.rank}',
textAlign: TextAlign.center,
style: TextStyle(
color: medal ? AppColors.gold : Colors.white70,
fontWeight: FontWeight.bold,
fontSize: 16)),
),
const SizedBox(width: 8),
SizedBox(
width: 40,
height: 40,
child: ClipOval(
child: RandomAvatar(e.avatar.isEmpty ? e.name : e.avatar)),
),
const SizedBox(width: 10),
Expanded(
child: Text(e.name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(color: Colors.white, fontSize: 15)),
),
RankBadge(tier: e.tier, points: e.rankPoints, size: 13),
]),
);
}
}