flutter_sudoku/lib/screens/game_screen.dart

723 lines
24 KiB
Dart
Raw Permalink Normal View History

2025-11-07 17:07:22 +09:00
import 'dart:async';
2025-11-10 18:02:01 +09:00
import 'dart:developer';
2025-11-07 17:07:22 +09:00
import 'package:flutter/material.dart';
2025-11-11 17:45:02 +09:00
import 'package:sudoku_app/models/game_level.dart';
2025-11-07 17:07:22 +09:00
import 'package:sudoku_app/models/sudoku_game_dto.dart';
import 'package:sudoku_app/models/sudoku_theme.dart';
import 'package:sudoku_app/models/unified_rank_dto.dart';
import 'package:sudoku_app/services/puzzle_service.dart';
2025-11-10 18:02:01 +09:00
import 'package:sudoku_app/services/identity_service.dart';
2025-11-07 17:07:22 +09:00
import 'package:sudoku_app/widgets/ad_banner_widget.dart';
import 'package:sudoku_app/widgets/number_pad.dart';
import 'package:sudoku_app/widgets/sudoku_board.dart';
2025-11-10 18:02:01 +09:00
import 'package:sudoku_app/models/game_rank_dto.dart';
// 랭킹 팝업의 2단계 UI 상태를 관리하기 위한 enum
enum _RankSubmissionStep { enterName, submitting, showList }
2025-11-07 17:07:22 +09:00
class GameScreen extends StatefulWidget {
final SudokuGameDto gameData;
final String themeName;
2025-11-10 18:02:01 +09:00
final String userId;
final String? userName;
2025-11-11 17:45:02 +09:00
final int levelIndex;
2025-11-07 17:07:22 +09:00
const GameScreen({
super.key,
required this.gameData,
required this.themeName,
2025-11-10 18:02:01 +09:00
required this.userId,
required this.userName,
2025-11-11 14:38:15 +09:00
required this.levelIndex,
2025-11-07 17:07:22 +09:00
});
@override
State<GameScreen> createState() => _GameScreenState();
}
class _GameScreenState extends State<GameScreen> {
final PuzzleService _puzzleService = PuzzleService();
2025-11-10 18:02:01 +09:00
final IdentityService _identityService = IdentityService();
2025-11-07 17:07:22 +09:00
2025-11-11 17:45:02 +09:00
late final GameLevel currentLevel;
2025-11-07 17:07:22 +09:00
late final int blockSize;
late final int gridSize;
2025-11-11 17:45:02 +09:00
late final SudokuTheme activeTheme;
2025-11-07 17:07:22 +09:00
late List<int> puzzleCells;
late List<int> solutionCells;
late List<int> originalCells;
int? selectedIndex;
int score = 5;
int secondsElapsed = 0;
Timer? timer;
int? selectedNumberPad;
Set<int> incorrectCells = {};
bool isValidating = false;
2025-11-10 18:02:01 +09:00
_RankSubmissionStep _rankStep = _RankSubmissionStep.enterName;
List<GameRankDto> _rankingList = [];
String _submittedPlayerName = "";
2025-11-11 17:45:02 +09:00
// 줌 컨트롤러
late final TransformationController _transformationController;
2025-11-10 18:02:01 +09:00
2025-11-07 17:07:22 +09:00
// "A" -> 10 (파싱용)
int _charToInt(String char) {
if (char == '0') return 0;
if (char.codeUnitAt(0) >= '1'.codeUnitAt(0) && char.codeUnitAt(0) <= '9'.codeUnitAt(0)) {
return int.parse(char);
}
if (char.codeUnitAt(0) >= 'A'.codeUnitAt(0) && char.codeUnitAt(0) <= 'Z'.codeUnitAt(0)) {
return char.codeUnitAt(0) - 'A'.codeUnitAt(0) + 10;
}
return -1;
}
// 10 -> "A" (전송용)
String _intToChar(int num) {
if (num == 0) return '0';
if (num >= 1 && num <= 9) return num.toString();
if (num >= 10 && num <= 35) return String.fromCharCode('A'.codeUnitAt(0) + (num - 10));
return '?';
}
@override
void initState() {
super.initState();
2025-11-11 14:38:15 +09:00
currentLevel = AppLevels.getLevel(widget.levelIndex);
blockSize = currentLevel.blockSize;
gridSize = blockSize * blockSize;
2025-11-11 17:45:02 +09:00
_transformationController = TransformationController();
2025-11-11 14:38:15 +09:00
2025-11-11 17:45:02 +09:00
// 🔽 [수정] 'isEasyMode'를 올바르게 계산하여 전달
String themeForThisGame = widget.themeName;
2025-11-11 14:38:15 +09:00
bool isEasyMode = currentLevel.isSequentialNumbers || currentLevel.isSequentialLetters;
if (currentLevel.isSequentialNumbers) {
2025-11-11 17:45:02 +09:00
themeForThisGame = AppThemes.numbers;
2025-11-11 14:38:15 +09:00
} else if (currentLevel.isSequentialLetters) {
2025-11-11 17:45:02 +09:00
themeForThisGame = AppThemes.letters;
2025-11-11 14:38:15 +09:00
}
2025-11-11 17:45:02 +09:00
2025-11-11 14:38:15 +09:00
activeTheme = AppThemes.buildGameTheme(
themeForThisGame,
gridSize,
2025-11-11 17:45:02 +09:00
isEasyMode: isEasyMode, // 👈 수정된 bool 값
2025-11-11 14:38:15 +09:00
);
2025-11-07 17:07:22 +09:00
puzzleCells = widget.gameData.question.split('').map(_charToInt).toList();
solutionCells = widget.gameData.solution.split('').map(_charToInt).toList();
originalCells = widget.gameData.question.split('').map(_charToInt).toList();
startTimer();
}
@override
void dispose() {
timer?.cancel();
2025-11-11 17:45:02 +09:00
_transformationController.dispose();
2025-11-07 17:07:22 +09:00
super.dispose();
}
void startTimer() {
timer = Timer.periodic(const Duration(seconds: 1), (timer) {
setState(() {
secondsElapsed++;
});
});
}
void onCellTapped(int index) {
if (originalCells[index] == 0) {
2025-11-11 14:38:15 +09:00
if (incorrectCells.isNotEmpty && !incorrectCells.contains(index)) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('틀린 값을 먼저 수정해주세요. (되돌리기 ↩)'),
duration: Duration(seconds: 1),
),
);
return;
}
2025-11-07 17:07:22 +09:00
setState(() {
selectedIndex = index;
if (selectedNumberPad != null) {
final int numberValue = selectedNumberPad!;
puzzleCells[index] = numberValue;
if (numberValue != solutionCells[index]) {
if (!incorrectCells.contains(index)) {
if (score > 0) {
score--;
}
incorrectCells.add(index);
}
} else {
incorrectCells.remove(index);
}
_checkIfBoardIsFull();
}
});
}
}
void _checkIfBoardIsFull() {
if (!puzzleCells.contains(0) && !isValidating) {
_validateGame();
}
}
void onNumberTapped(int numberValue) {
setState(() {
if (selectedNumberPad == numberValue) {
selectedNumberPad = null;
} else {
selectedNumberPad = numberValue;
}
});
}
void onUndoTapped() {
2025-11-11 14:38:15 +09:00
if (incorrectCells.isNotEmpty) {
int errorIndex = incorrectCells.first;
setState(() {
puzzleCells[errorIndex] = 0;
incorrectCells.remove(errorIndex);
selectedIndex = errorIndex;
});
}
else if (selectedIndex != null && originalCells[selectedIndex!] == 0) {
setState(() {
2025-11-07 17:07:22 +09:00
puzzleCells[selectedIndex!] = 0;
2025-11-11 14:38:15 +09:00
});
}
2025-11-07 17:07:22 +09:00
}
void onHintTapped() {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('힌트 기능은 준비 중입니다.')),
);
}
2025-11-10 18:02:01 +09:00
void _onRestartGameTapped() {
setState(() {
puzzleCells = originalCells.toList();
incorrectCells.clear();
selectedIndex = null;
selectedNumberPad = null;
score = 5;
2025-11-11 17:45:02 +09:00
_resetBoardZoom();
2025-11-10 18:02:01 +09:00
timer?.cancel();
secondsElapsed = 0;
startTimer();
});
}
void _onQuitGameTapped() {
Navigator.of(context).pop();
}
2025-11-11 17:45:02 +09:00
void _resetBoardZoom() {
_transformationController.value = Matrix4.identity();
}
2025-11-10 18:02:01 +09:00
2025-11-07 17:07:22 +09:00
Future<void> _validateGame() async {
if (isValidating) return;
setState(() { isValidating = true; });
2025-11-10 18:02:01 +09:00
2025-11-07 17:07:22 +09:00
timer?.cancel();
String currentAnswer = puzzleCells.map(_intToChar).join('');
2025-11-10 18:02:01 +09:00
2025-11-07 17:07:22 +09:00
try {
final bool result = await _puzzleService.validateSolution(
2025-11-10 18:02:01 +09:00
widget.gameData.puzzleId,
currentAnswer,
2025-11-07 17:07:22 +09:00
);
2025-11-10 18:02:01 +09:00
2025-11-07 17:07:22 +09:00
if (result) {
if(mounted) _showRankingDialog();
} else {
if(mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('🤔 틀린 부분이 있습니다.')),
);
}
startTimer();
}
} catch (e) {
if(mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('오류: $e')),
);
}
startTimer();
} finally {
if(mounted) {
setState(() { isValidating = false; });
}
}
}
void _showRankingDialog() {
2025-11-10 18:02:01 +09:00
final nameController = TextEditingController(text: widget.userName);
2025-11-11 17:45:02 +09:00
// (상태 변수들은 변경 없음)
2025-11-07 17:07:22 +09:00
bool isSubmitting = false;
2025-11-11 14:38:15 +09:00
String? dialogErrorMessage;
2025-11-10 18:02:01 +09:00
_rankStep = _RankSubmissionStep.enterName;
_rankingList = [];
_submittedPlayerName = "";
2025-11-11 14:38:15 +09:00
final String contextId = currentLevel.contextId;
2025-11-10 18:02:01 +09:00
2025-11-07 17:07:22 +09:00
showDialog(
context: context,
barrierDismissible: false,
builder: (ctx) {
return StatefulBuilder(
builder: (context, setDialogState) {
2025-11-10 18:02:01 +09:00
2025-11-11 17:45:02 +09:00
// (위젯 빌드 로직은 변경 없음)
2025-11-10 18:02:01 +09:00
Widget closeButton = TextButton(
onPressed: () {
Navigator.of(ctx).pop();
Navigator.of(context).pop();
},
child: const Text('닫기'),
);
Widget rankListWidget = Expanded(
child: _rankingList.isEmpty
? const Center(child: Text("현재 랭킹이 없습니다."))
: ListView.builder(
itemCount: _rankingList.length,
shrinkWrap: true,
itemBuilder: (context, index) {
final rank = _rankingList[index];
final bool isMe = rank.playerName == _submittedPlayerName;
int displayScore = 5 - (rank.secondaryScore ?? 5);
final min = (rank.primaryScore ~/ 60).toString().padLeft(2, '0');
final sec = (rank.primaryScore % 60).toString().padLeft(2, '0');
final time = '$min:$sec';
return ListTile(
selected: isMe,
selectedTileColor: Colors.blue.shade100,
leading: Text('${index + 1}.', style: const TextStyle(fontWeight: FontWeight.bold)),
title: Text(rank.playerName, style: TextStyle(fontWeight: isMe ? FontWeight.bold : FontWeight.normal)),
trailing: Text('$time (Score: $displayScore)', style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.black87)),
);
},
2025-11-07 17:07:22 +09:00
),
2025-11-10 18:02:01 +09:00
);
Widget nameEntryWidget = Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('완료 시간: $secondsElapsed 초 / 남은 점수: $score'),
const SizedBox(height: 20),
TextField(
controller: nameController,
2025-11-11 14:38:15 +09:00
readOnly: false,
2025-11-10 18:02:01 +09:00
decoration: InputDecoration(
2025-11-11 14:38:15 +09:00
labelText: '이름 (10자 이내)',
2025-11-10 18:02:01 +09:00
border: const OutlineInputBorder(),
2025-11-11 14:38:15 +09:00
errorText: dialogErrorMessage,
2025-11-07 17:07:22 +09:00
),
2025-11-10 18:02:01 +09:00
maxLength: 10,
),
],
);
Widget submitButton = ElevatedButton(
onPressed: () async {
final playerName = nameController.text.trim();
if (playerName.isEmpty) {
setDialogState(() {
dialogErrorMessage = "이름을 입력해주세요.";
});
return;
}
setDialogState(() {
_rankStep = _RankSubmissionStep.submitting;
_submittedPlayerName = playerName;
2025-11-11 14:38:15 +09:00
dialogErrorMessage = null;
2025-11-10 18:02:01 +09:00
});
final rankDto = UnifiedRankDto(
userId: widget.userId,
gameType: 'SUDOKU',
contextId: contextId,
playerName: playerName,
primaryScore: secondsElapsed,
secondaryScore: (5 - score),
);
2025-11-11 17:45:02 +09:00
// 🔽 [수정] 여기가 핵심입니다.
2025-11-10 18:02:01 +09:00
try {
2025-11-11 17:45:02 +09:00
// 1. submitRank가 이제 랭킹 리스트를 직접 반환합니다.
final List<GameRankDto> ranks = await _puzzleService.submitRank(rankDto);
// 2. 랭킹 등록 성공 후 로컬 작업 수행
2025-11-11 14:38:15 +09:00
await _identityService.saveUserName(playerName);
final int currentMaxLevel = await _identityService.getMaxUnlockedLevel();
if (currentMaxLevel < 99) {
if (widget.levelIndex >= currentMaxLevel) {
int nextLevel = widget.levelIndex + 1;
if (nextLevel > AppLevels.allLevels.length) {
await _identityService.saveMaxUnlockedLevel(99);
} else {
await _identityService.saveMaxUnlockedLevel(nextLevel);
}
}
2025-11-10 18:02:01 +09:00
}
2025-11-11 17:45:02 +09:00
// 3. 🔽 [삭제] 별도로 랭킹을 다시 불러올 필요가 없습니다.
// final ranks = await _puzzleService.fetchRanks('SUDOKU', contextId);
2025-11-10 18:02:01 +09:00
2025-11-11 17:45:02 +09:00
// 4. submitRank가 반환한 랭킹 리스트로 즉시 UI 업데이트
2025-11-10 18:02:01 +09:00
setDialogState(() {
2025-11-11 17:45:02 +09:00
_rankingList = ranks; // 👈 반환된 랭킹 리스트 사용
2025-11-10 18:02:01 +09:00
_rankStep = _RankSubmissionStep.showList;
});
} catch (e) {
log("!!! 랭킹 등록 실패 !!!", error: e);
setDialogState(() {
2025-11-11 14:38:15 +09:00
_rankStep = _RankSubmissionStep.enterName;
dialogErrorMessage = e.toString().replaceFirst("Exception: ", "");
2025-11-10 18:02:01 +09:00
});
}
},
child: const Text('랭킹 등록'),
);
2025-11-11 17:45:02 +09:00
// (dialogContent, dialogActions 등 나머지 로직은 변경 없음)
// ...
// ...
2025-11-10 18:02:01 +09:00
Widget dialogContent;
if (_rankStep == _RankSubmissionStep.showList) {
dialogContent = rankListWidget;
} else {
dialogContent = nameEntryWidget;
}
List<Widget> dialogActions;
if (_rankStep == _RankSubmissionStep.enterName) {
dialogActions = [
2025-11-07 17:07:22 +09:00
TextButton(
onPressed: () {
Navigator.of(ctx).pop();
Navigator.of(context).pop();
},
child: const Text('닫기'),
),
2025-11-10 18:02:01 +09:00
submitButton
];
} else if (_rankStep == _RankSubmissionStep.submitting) {
dialogActions = [const CircularProgressIndicator()];
} else {
dialogActions = [closeButton];
}
return AlertDialog(
title: Text(_rankStep == _RankSubmissionStep.showList
? '🏆 상위 10개 랭킹 ($contextId)'
: '🎉 성공! 기록을 남겨주세요.'),
content: SizedBox(
width: 400,
height: _rankStep == _RankSubmissionStep.showList ? 400 : null,
child: dialogContent,
),
actions: dialogActions,
2025-11-07 17:07:22 +09:00
);
},
);
},
);
}
2025-11-10 18:02:01 +09:00
2025-11-07 17:07:22 +09:00
int _difficultyLevel(String question) {
int holes = question.split('').where((c) => c == '0').length;
double holeRatio = holes / (gridSize * gridSize);
if (holeRatio <= 0.51) return 1;
if (holeRatio <= 0.58) return 2;
if (holeRatio <= 0.63) return 3;
if (holeRatio <= 0.68) return 4;
return 5;
}
@override
Widget build(BuildContext context) {
String formattedTime =
'${(secondsElapsed ~/ 60).toString().padLeft(2, '0')}:${(secondsElapsed % 60).toString().padLeft(2, '0')}';
final Map<int, int> numberCounts = {};
for (int i = 1; i <= gridSize; i++) { numberCounts[i] = 0; }
for (int cellValue in puzzleCells) {
if (cellValue > 0) {
numberCounts[cellValue] = (numberCounts[cellValue] ?? 0) + 1;
}
}
return Scaffold(
2025-11-10 18:02:01 +09:00
body: SafeArea(
child: Column(
children: [
Expanded(
child: LayoutBuilder(
builder: (context, constraints) {
bool isLandscape = constraints.maxWidth > constraints.maxHeight;
if (isLandscape) {
return _buildLandscapeLayout(context, numberCounts, constraints, formattedTime);
} else {
return _buildPortraitLayout(context, numberCounts, constraints, formattedTime);
}
},
2025-11-07 17:07:22 +09:00
),
),
2025-11-10 18:02:01 +09:00
const AdBannerWidget(),
],
),
2025-11-07 17:07:22 +09:00
),
);
}
2025-11-11 17:45:02 +09:00
// 세로 모드(일반 폰) 레이아웃
2025-11-10 18:02:01 +09:00
Widget _buildPortraitLayout(BuildContext context, Map<int, int> numberCounts, BoxConstraints constraints, String formattedTime) {
final double boardWidth = (constraints.maxWidth > 600) ? 600 : constraints.maxWidth;
2025-11-07 17:07:22 +09:00
return Center(
child: ConstrainedBox(
2025-11-10 18:02:01 +09:00
constraints: BoxConstraints(maxWidth: boardWidth),
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0),
2025-11-11 17:45:02 +09:00
child: _buildGameInfoWidget(formattedTime), // 👈 상단 컨트롤 바
2025-11-07 17:07:22 +09:00
),
2025-11-10 18:02:01 +09:00
Expanded(
child: Center(
child: SingleChildScrollView(
child: Padding(
2025-11-11 17:45:02 +09:00
padding: const EdgeInsets.symmetric(vertical: 16.0), // 👈 보드/패드 상하 패딩
2025-11-10 18:02:01 +09:00
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2025-11-11 17:45:02 +09:00
_buildSudokuBoardWidget(), // 👈 줌 기능
2025-11-10 18:02:01 +09:00
const SizedBox(height: 15),
2025-11-11 17:45:02 +09:00
Padding( // 👈 하단 컨트롤 패널에만 좌우 패딩
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: _buildControlPanelWidget(context, numberCounts, isLandscape: false, boardWidth: boardWidth),
),
2025-11-10 18:02:01 +09:00
],
),
),
),
),
),
],
2025-11-07 17:07:22 +09:00
),
),
);
}
2025-11-11 17:45:02 +09:00
// 가로 모드(폴더블/태블릿) 레이아웃
2025-11-10 18:02:01 +09:00
Widget _buildLandscapeLayout(BuildContext context, Map<int, int> numberCounts, BoxConstraints constraints, String formattedTime) {
const double infoBarHeight = 60.0;
2025-11-11 17:45:02 +09:00
// 🔽 [수정] 보드 너비는 (사용 가능 높이 - 상단 컨트롤바 높이 - 상하 패딩)
2025-11-10 18:02:01 +09:00
double boardWidth = constraints.maxHeight - infoBarHeight - 32.0;
2025-11-11 17:45:02 +09:00
// 🔽 [수정] 컨트롤 패널 너비 계산
double controlPanelWidth;
2025-11-10 18:02:01 +09:00
const double numberPadScaleRatio = 0.6;
double padWidth = boardWidth * numberPadScaleRatio;
if (padWidth < 200) padWidth = 200;
if (padWidth > 350) padWidth = 350;
2025-11-11 17:45:02 +09:00
controlPanelWidth = padWidth + 100; // 100 for buttons
2025-11-10 18:02:01 +09:00
2025-11-11 17:45:02 +09:00
// 🔽 [수정] 화면 너비에 맞게 전체 크기 동적 조절
double totalWidth = boardWidth + controlPanelWidth + 16.0; // 16 for spacing
if (totalWidth > (constraints.maxWidth - 32.0)) { // 32 for screen padding
2025-11-10 18:02:01 +09:00
double scale = (constraints.maxWidth - 32.0) / totalWidth;
boardWidth *= scale;
2025-11-11 17:45:02 +09:00
controlPanelWidth *= scale;
2025-11-10 18:02:01 +09:00
}
2025-11-07 17:07:22 +09:00
return Padding(
padding: const EdgeInsets.all(16.0),
2025-11-10 18:02:01 +09:00
child: Column(
2025-11-07 17:07:22 +09:00
children: [
2025-11-11 17:45:02 +09:00
_buildGameInfoWidget(formattedTime), // 👈 상단 컨트롤 바
2025-11-07 17:07:22 +09:00
Expanded(
2025-11-10 18:02:01 +09:00
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: boardWidth,
2025-11-11 17:45:02 +09:00
child: _buildSudokuBoardWidget(), // 👈 줌 기능
2025-11-10 18:02:01 +09:00
),
const SizedBox(width: 16),
SizedBox(
2025-11-11 17:45:02 +09:00
width: controlPanelWidth, // 👈 계산된 너비
2025-11-10 18:02:01 +09:00
child: SingleChildScrollView(
child: Column(
children: [
2025-11-11 17:45:02 +09:00
_buildControlPanelWidget(context, numberCounts, isLandscape: true, boardWidth: boardWidth),
2025-11-10 18:02:01 +09:00
],
),
),
),
],
2025-11-07 17:07:22 +09:00
),
),
],
),
);
}
2025-11-11 17:45:02 +09:00
// 🔽 [수정] 상단 정보 (점수, 시간만)
2025-11-10 18:02:01 +09:00
Widget _buildGameInfoWidget(String formattedTime) {
2025-11-07 17:07:22 +09:00
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('SCORE: $score', style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
2025-11-10 18:02:01 +09:00
Text(formattedTime, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
2025-11-07 17:07:22 +09:00
],
);
}
2025-11-11 17:45:02 +09:00
// 게임 보드 (줌 기능 + 롱프레스 리셋)
2025-11-07 17:07:22 +09:00
Widget _buildSudokuBoardWidget() {
2025-11-11 17:45:02 +09:00
return GestureDetector( // 👈 롱프레스 감지
onLongPress: _resetBoardZoom,
child: InteractiveViewer(
transformationController: _transformationController, // 👈 컨트롤러 연결
boundaryMargin: const EdgeInsets.all(20.0),
minScale: 1.0,
maxScale: 2.5,
child: SudokuBoard(
blockSize: blockSize,
theme: activeTheme,
cells: puzzleCells,
originalCells: originalCells,
selectedIndex: selectedIndex,
selectedNumberPad: selectedNumberPad,
incorrectCells: incorrectCells,
onCellTapped: onCellTapped,
),
),
2025-11-07 17:07:22 +09:00
);
}
2025-11-11 17:45:02 +09:00
// 🔽 [수정] 하단 컨트롤 패널 (넘버패드 + 모든 버튼)
Widget _buildControlPanelWidget(BuildContext context, Map<int, int> numberCounts, {required bool isLandscape, required double boardWidth}) {
2025-11-10 18:02:01 +09:00
const double numberPadScaleRatio = 0.6;
double? padMaxWidth;
if (!isLandscape) {
2025-11-11 17:45:02 +09:00
// 세로 모드: (보드 너비 * 0.6)
2025-11-10 18:02:01 +09:00
padMaxWidth = boardWidth * numberPadScaleRatio;
} else {
2025-11-11 17:45:02 +09:00
// 가로 모드: (보드 너비 * 0.6) -> 가로모드에서도 비율 유지
padMaxWidth = boardWidth * numberPadScaleRatio;
if (padMaxWidth < 200) padMaxWidth = 200; // 최소 너비
2025-11-10 18:02:01 +09:00
}
2025-11-07 17:07:22 +09:00
2025-11-11 17:45:02 +09:00
// 1. 숫자 그리드
2025-11-10 18:02:01 +09:00
Widget numberPadGrid = ConstrainedBox(
constraints: BoxConstraints(maxWidth: padMaxWidth ?? double.infinity),
child: NumberPad(
blockSize: blockSize,
theme: activeTheme,
numberCounts: numberCounts,
selectedNumber: selectedNumberPad,
onNumberTapped: onNumberTapped,
isLandscape: isLandscape,
2025-11-07 17:07:22 +09:00
),
);
2025-11-10 18:02:01 +09:00
2025-11-11 17:45:02 +09:00
// 2. 왼쪽 버튼 (종료, 다시하기)
Widget leftButtons = Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.close, color: Colors.red.shade700, size: 30),
onPressed: _onQuitGameTapped,
tooltip: "게임 종료",
),
IconButton(
icon: Icon(Icons.refresh, color: Colors.blue.shade700, size: 30),
onPressed: _onRestartGameTapped,
tooltip: "다시하기",
),
],
2025-11-10 18:02:01 +09:00
);
2025-11-11 17:45:02 +09:00
// 3. 오른쪽 버튼 (힌트, 되돌리기)
Widget rightButtons = Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: onHintTapped,
icon: const Icon(Icons.lightbulb_outline, color: Colors.orange),
iconSize: 30,
tooltip: "힌트",
),
IconButton(
onPressed: onUndoTapped,
icon: const Icon(Icons.undo, color: Colors.red),
iconSize: 30,
tooltip: "되돌리기",
),
],
2025-11-10 18:02:01 +09:00
);
2025-11-11 17:45:02 +09:00
// 4. 레이아웃 조립
2025-11-10 18:02:01 +09:00
if (isLandscape) {
2025-11-11 17:45:02 +09:00
// 가로 모드: (세로) Column -> [ NumberPad, (가로) Row [버튼 4개] ]
2025-11-10 18:02:01 +09:00
return Column(
mainAxisSize: MainAxisSize.min,
children: [
numberPadGrid,
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
2025-11-11 17:45:02 +09:00
children: [
leftButtons, // 👈 Column
rightButtons // 👈 Column
],
2025-11-10 18:02:01 +09:00
)
],
);
} else {
2025-11-11 17:45:02 +09:00
// 세로 모드: (가로) Row -> [ 왼쪽 버튼, Expanded(NumberPad), 오른쪽 버튼 ]
2025-11-10 18:02:01 +09:00
return Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
2025-11-11 17:45:02 +09:00
leftButtons,
2025-11-10 18:02:01 +09:00
Expanded(child: numberPadGrid),
2025-11-11 17:45:02 +09:00
rightButtons,
2025-11-10 18:02:01 +09:00
],
);
}
2025-11-07 17:07:22 +09:00
}
}