123 lines
3.6 KiB
Dart
123 lines
3.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:service_api/service_api.dart';
|
|
import 'base_game_screen.dart'; // GameResultArgs import
|
|
|
|
class GameCompletionScreen extends StatefulWidget {
|
|
final GameResultArgs args;
|
|
final bool isDailyCourse;
|
|
|
|
const GameCompletionScreen({
|
|
super.key,
|
|
required this.args,
|
|
this.isDailyCourse = false,
|
|
});
|
|
|
|
@override
|
|
State<GameCompletionScreen> createState() => _GameCompletionScreenState();
|
|
}
|
|
|
|
class _GameCompletionScreenState extends State<GameCompletionScreen> {
|
|
late TextEditingController _nameController;
|
|
late IdentityService _identityService;
|
|
bool _isSaving = false;
|
|
String? _userName;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_identityService = context.read<IdentityService>();
|
|
// [Fix] args.userName 사용 가능
|
|
String? initialName = widget.args.userName;
|
|
_nameController = TextEditingController(text: initialName ?? '');
|
|
_loadUserInfo();
|
|
}
|
|
|
|
Future<void> _loadUserInfo() async {
|
|
final session = await _identityService.getUserSession();
|
|
String? name = session?.userName ?? await _identityService.getUserName();
|
|
|
|
// 만약 args에 이름이 없고 저장된 이름이 있다면 불러옴
|
|
if (widget.args.userName == null && name != null) {
|
|
if (mounted) {
|
|
setState(() {
|
|
_nameController.text = name;
|
|
_userName = name;
|
|
});
|
|
}
|
|
}
|
|
|
|
if (session != null && !session.isGuest) {
|
|
_saveProgress(name ?? "Unknown");
|
|
}
|
|
}
|
|
|
|
Future<void> _saveProgress(String playerName) async {
|
|
if (_isSaving) return;
|
|
setState(() => _isSaving = true);
|
|
|
|
try {
|
|
await _identityService.saveUserName(playerName);
|
|
|
|
// [Fix] 타입 호환성 수정: String 인자 전달
|
|
if (widget.args.onProgressSave != null) {
|
|
await widget.args.onProgressSave!(playerName);
|
|
}
|
|
|
|
if (widget.args.stars != null && widget.args.levelIndex != null) {
|
|
await _identityService.submitGameResult(
|
|
gameType: widget.args.gameType,
|
|
level: widget.args.levelIndex!,
|
|
stars: widget.args.stars!
|
|
);
|
|
}
|
|
|
|
} catch (e) {
|
|
debugPrint("Error saving progress: $e");
|
|
} finally {
|
|
if (mounted) setState(() => _isSaving = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('결과')),
|
|
body: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
children: [
|
|
const Icon(Icons.emoji_events, size: 80, color: Colors.orange),
|
|
const SizedBox(height: 24),
|
|
Text(
|
|
"점수: ${widget.args.primaryScore}",
|
|
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
|
|
),
|
|
const SizedBox(height: 32),
|
|
TextField(
|
|
controller: _nameController,
|
|
decoration: const InputDecoration(
|
|
labelText: '이름을 입력하세요',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
await _saveProgress(_nameController.text);
|
|
if (!mounted) return;
|
|
|
|
if (widget.args.onNextGame != null) {
|
|
widget.args.onNextGame!();
|
|
} else {
|
|
Navigator.pop(context);
|
|
}
|
|
},
|
|
child: const Text('확인'),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |