
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
숙제중 스코어 바뀌는것도 했고 새로고침하면 카드 trycount 와 score이 바뀌는것까진 했는데 카드가 새로고침되는건 정말 모르겠어요ㅜㅠㅠㅠ
card_bords 코드
import 'package:flutter/material.dart';
import 'package:memory_matching_game/src/card.dart';
import 'package:memory_matching_game/src/card_model.dart';
class CardBoards extends StatefulWidget {
final Function() updateTryCount;
final Function() updateScore;
final Function() resetGame;
CardBoards(
{super.key,
required this.updateTryCount,
required this.updateScore,
required this.resetGame});
@override
State<CardBoards> createState() => _CardBoardsState();
}
class _CardBoardsState extends State<CardBoards> {
late List<CardModel> cards;
@override
void initState() {
super.initState();
List<int> cardsValue = [1, 5, 2, 6, 3, 4, 3, 2, 6, 1, 4, 5];
cardsValue.shuffle();
cards = List.generate(cardsValue.length, (index) {
return CardModel(index: index, cardValue: cardsValue[index]);
});
}
CardModel? instantFirstCard;
void onTapCard(int cardIndex) {
print('$cardIndex 번째 카드를 선택하셨습니다.');
if (instantFirstCard == null) {
instantFirstCard = cards[cardIndex];
} else {
// 두번째 카드가 선택되었을때 로직 추가
widget.updateTryCount();
var firstCard = instantFirstCard;
var secondCard = cards[cardIndex];
if (firstCard!.cardValue == secondCard.cardValue) {
print('짝이 맞았습니다.');
widget.updateScore();
instantFirstCard = null;
} else {
resetInstantCards(instantFirstCard!, secondCard);
}
}
setState(() {
cards[cardIndex].setFlipped(true);
});
}
void resetInstantCards(CardModel firstCard, CardModel secondCard) async {
await Future.delayed(Duration(seconds: 2));
setState(() {
firstCard.setFlipped(false);
secondCard.setFlipped(false);
});
instantFirstCard = null;
return;
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Wrap(spacing: 4, runSpacing: 4, children: [
for (var i = 0; i < cards.length; i++)
CardWidget(
card: cards[i],
onTap: () {
onTapCard(i);
},
),
]),
);
}
}
header
import 'package:flutter/material.dart';
class Header extends StatelessWidget {
final int tryCount;
final int score;
final VoidCallback resetGame;
Header(
{super.key,
this.tryCount = 0,
required this.score,
required this.resetGame});
@override
Widget build(BuildContext context) {
return SizedBox(
height: 60,
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'score',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w200,
color: Colors.black,
letterSpacing: 0,
height: 0,
),
),
Text(score.toString(),
style: TextStyle(
height: 0,
fontSize: 30,
letterSpacing: -2,
fontWeight: FontWeight.bold,
color: Colors.black)),
],
)),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'try count',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w200,
color: Colors.black,
letterSpacing: 0,
height: 0,
),
),
Text(tryCount.toString(),
style: TextStyle(
height: 0,
fontSize: 30,
letterSpacing: -2,
fontWeight: FontWeight.bold,
color: Colors.black)),
],
)),
Expanded(
child: GestureDetector(
onTap: resetGame,
child: Container(
margin: EdgeInsets.only(top: 10, bottom: 10, left: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
color: Color(0xff94BEE5),
),
child: Center(child: Text('새 게임')),
),
),
),
],
),
);
}
}
home
import 'package:flutter/material.dart';
import 'package:memory_matching_game/src/card_boards.dart';
import 'package:memory_matching_game/src/header.dart';
class Home extends StatefulWidget {
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int tryCount = 0;
int score = 0;
// 추가
void updateTryCount() {
setState(() {
tryCount++;
});
}
void updateScore() {
setState(() {
score += 100;
});
}
void resetGame() {
setState(() {
tryCount = 0;
score = 0;
});
}
// 새 게임 버튼 클릭 시 카드 섞기 및 리셋
void _resetGameAndShuffleCards() {
resetGame(); // 점수, 시도 횟수 리셋
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xffECE7E4),
appBar: AppBar(
title: const Text('짝맞추기 게임'),
backgroundColor: const Color(0xff92CBFF),
),
body: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Header(
tryCount: tryCount,
score: score,
resetGame: resetGame,
),
SizedBox(height: 20),
Expanded(
child: CardBoards(
updateTryCount: updateTryCount,
updateScore: updateScore,
resetGame: resetGame, // 여기를 수정
),
),
],
),
),
);
}
}
