flutter_sudoku/lib/screens/ranking_screen.dart

136 lines
4.7 KiB
Dart
Raw Permalink Normal View History

2025-11-07 17:07:22 +09:00
import 'package:flutter/material.dart';
2025-11-11 14:38:15 +09:00
import 'package:sudoku_app/models/game_level.dart';
2025-11-07 17:07:22 +09:00
import 'package:sudoku_app/models/game_rank_dto.dart';
import 'package:sudoku_app/services/puzzle_service.dart';
class RankingScreen extends StatefulWidget {
2025-11-11 14:38:15 +09:00
// 🔽 [수정] 홈 화면에서 전달받을 초기 난이도 이름
2025-11-10 18:02:01 +09:00
final String? initialDifficultyName;
const RankingScreen({
super.key,
2025-11-11 14:38:15 +09:00
this.initialDifficultyName, // 👈 [수정] 생성자에 이 파라미터 추가
2025-11-10 18:02:01 +09:00
});
2025-11-07 17:07:22 +09:00
@override
State<RankingScreen> createState() => _RankingScreenState();
}
class _RankingScreenState extends State<RankingScreen> {
final PuzzleService _puzzleService = PuzzleService();
late Future<List<GameRankDto>> _rankingFuture;
2025-11-11 14:38:15 +09:00
// 9단계 난이도에 맞는 (이름 -> ContextId) 맵
2025-11-10 18:02:01 +09:00
final Map<String, String> difficultyContexts = {
2025-11-11 14:38:15 +09:00
for (var level in AppLevels.allLevels) level.name : level.contextId
2025-11-10 18:02:01 +09:00
};
2025-11-11 14:38:15 +09:00
late String _selectedDifficultyName;
2025-11-10 18:02:01 +09:00
2025-11-07 17:07:22 +09:00
@override
void initState() {
super.initState();
2025-11-10 18:02:01 +09:00
String defaultDifficulty = widget.initialDifficultyName ?? "중급 (9x9)";
if (!difficultyContexts.containsKey(defaultDifficulty)) {
defaultDifficulty = "중급 (9x9)";
}
_fetchRanksForDifficulty(defaultDifficulty);
2025-11-07 17:07:22 +09:00
}
2025-11-10 18:02:01 +09:00
void _fetchRanksForDifficulty(String difficultyName) {
setState(() {
2025-11-11 14:38:15 +09:00
_selectedDifficultyName = difficultyName;
_rankingFuture = _puzzleService.fetchRanks('SUDOKU', difficultyContexts[_selectedDifficultyName]);
2025-11-10 18:02:01 +09:00
});
}
// 시간(초)을 'mm:ss' 형식으로 변환
String _formatTime(int seconds) {
2025-11-07 17:07:22 +09:00
final min = (seconds ~/ 60).toString().padLeft(2, '0');
final sec = (seconds % 60).toString().padLeft(2, '0');
return '$min:$sec';
}
2025-11-10 18:02:01 +09:00
// (5 - score)로 저장된 값을 -> "SCORE: 5"로 변환
String _formatScore(int? storedScore) {
int score = 5 - (storedScore ?? 5);
return 'SCORE: $score';
}
2025-11-07 17:07:22 +09:00
@override
Widget build(BuildContext context) {
return Scaffold(
2025-11-10 18:02:01 +09:00
appBar: AppBar(title: const Text('스도쿠 랭킹')),
body: Column(
children: [
// 1. 난이도 선택 Dropdown
Padding(
padding: const EdgeInsets.all(16.0),
child: DropdownButton<String>(
2025-11-11 14:38:15 +09:00
value: _selectedDifficultyName,
2025-11-10 18:02:01 +09:00
isExpanded: true,
2025-11-11 14:38:15 +09:00
items: AppLevels.allLevels.map((level) {
2025-11-10 18:02:01 +09:00
return DropdownMenuItem<String>(
2025-11-11 14:38:15 +09:00
value: level.name,
child: Text(level.name),
2025-11-10 18:02:01 +09:00
);
}).toList(),
onChanged: (String? newValue) {
if (newValue != null) {
_fetchRanksForDifficulty(newValue);
}
},
),
),
// 2. 랭킹 리스트
Expanded(
child: FutureBuilder<List<GameRankDto>>(
future: _rankingFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(child: Text('랭킹 로딩 실패: ${snapshot.error}'));
}
if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const Center(child: Text('등록된 랭킹이 없습니다.'));
}
2025-11-07 17:07:22 +09:00
2025-11-10 18:02:01 +09:00
final ranks = snapshot.data!;
return ListView.builder(
itemCount: ranks.length,
itemBuilder: (context, index) {
final rank = ranks[index];
return ListTile(
leading: Text(
'${index + 1}.',
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
title: Text(rank.playerName, style: const TextStyle(fontSize: 18)),
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
_formatTime(rank.primaryScore),
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.blue),
),
Text(
_formatScore(rank.secondaryScore),
style: const TextStyle(fontSize: 12, color: Colors.grey),
),
],
),
);
},
);
},
),
),
],
2025-11-07 17:07:22 +09:00
),
);
}
}