-
Notifications
You must be signed in to change notification settings - Fork 10
[Step1] 1단계 UI-로직 분리하기 #5
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: malibinyun
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'dart:async'; | ||
|
||
import 'asset_name.dart'; | ||
|
||
class FlipCardCore { | ||
final _imageNames = [ | ||
AssetImageName.orange, | ||
AssetImageName.banana, | ||
AssetImageName.apple, | ||
AssetImageName.strawberry, | ||
]; | ||
|
||
final List<String> _randomImageNames = []; | ||
int _frontCardCount = 0; | ||
final List<int> _frontCardIndexes = []; | ||
|
||
final StreamController toggleCardToFrontStream = StreamController(); | ||
final StreamController cardsMatchedStream = StreamController(); | ||
|
||
List<String> getRandomCardImages() { | ||
return _randomImageNames.map((e) => e).toList(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서는 |
||
} | ||
|
||
void reset() { | ||
// add 2 times | ||
_randomImageNames.clear(); | ||
_randomImageNames.addAll(_imageNames); | ||
_randomImageNames.addAll(_imageNames); | ||
|
||
// shuffle | ||
_randomImageNames.shuffle(); | ||
} | ||
|
||
void flipCard(int cardIndex) { | ||
_frontCardCount++; | ||
_frontCardIndexes.add(cardIndex); | ||
} | ||
|
||
void checkCards() { | ||
print('_frontCardCount : $_frontCardCount'); | ||
print('_frontCardIndexes : $_frontCardIndexes'); | ||
|
||
if (_frontCardCount == 2) { | ||
toggleCardToFrontStream.add(true); | ||
if (_frontCardIndexes.length >= 2) { | ||
String firstCardName = _randomImageNames[_frontCardIndexes[0]]; | ||
String secondCardName = _randomImageNames[_frontCardIndexes[1]]; | ||
if (firstCardName == secondCardName) { | ||
_randomImageNames[_frontCardIndexes[0]] = ''; | ||
_randomImageNames[_frontCardIndexes[1]] = ''; | ||
|
||
cardsMatchedStream.add(true); | ||
} | ||
} | ||
|
||
_frontCardIndexes.clear(); | ||
_frontCardCount = 0; | ||
Comment on lines
+52
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
처음 한 벌의 카드를 맞춘 뒤에 카드를 누르면, _frontCardCount는 3으로 초기화가 되지 않음을 확인했어요. |
||
} | ||
} | ||
|
||
void dispose() { | ||
toggleCardToFrontStream.close(); | ||
cardsMatchedStream.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import 'package:flip_card/flip_card.dart'; | ||
import 'package:flip_card_game/asset_name.dart'; | ||
import 'package:flip_card_game/flip_card_core.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
void main() { | ||
|
@@ -31,37 +31,29 @@ class MyHomePage extends StatefulWidget { | |
} | ||
|
||
class _MyHomePageState extends State<MyHomePage> { | ||
final _imageNames = [ | ||
AssetImageName.orange, | ||
AssetImageName.banana, | ||
AssetImageName.apple, | ||
AssetImageName.strawberry, | ||
]; | ||
|
||
final List<String> _randomImageNames = []; | ||
final FlipCardCore _flipCardCore = FlipCardCore(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
다만 1주차 때 배운 (시간 되실 때 보너스로 진행해보셔도 좋을 듯 해요! 😀) |
||
final List<GlobalKey<FlipCardState>> _cardKeys = []; | ||
int _frontCardCount = 0; | ||
final List<int> _frontCardIndexes = []; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
|
||
print('initState'); | ||
_flipCardCore.toggleCardToFrontStream.stream.listen((_) { | ||
_toggleCardToFront(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 카드가 2장이 뒤집혔을 때, 첫번째 매칭 후 카드가 더 뒤집히지 않는 이슈와도 연관이 있을 듯 해요! |
||
}); | ||
|
||
reset(); | ||
} | ||
|
||
void reset() { | ||
// add 2 times | ||
_randomImageNames.clear(); | ||
_randomImageNames.addAll(_imageNames); | ||
_randomImageNames.addAll(_imageNames); | ||
|
||
// shuffle | ||
_randomImageNames.shuffle(); | ||
_flipCardCore.reset(); | ||
|
||
// create global key | ||
_cardKeys.clear(); | ||
_cardKeys.addAll(_randomImageNames.map((_) => GlobalKey<FlipCardState>())); | ||
_cardKeys.addAll(_flipCardCore | ||
.getRandomCardImages() | ||
.map((_) => GlobalKey<FlipCardState>())); | ||
} | ||
|
||
@override | ||
|
@@ -70,66 +62,52 @@ class _MyHomePageState extends State<MyHomePage> { | |
appBar: AppBar( | ||
title: Text(widget.title), | ||
), | ||
body: Center( | ||
child: Wrap( | ||
spacing: 4, | ||
runSpacing: 4, | ||
children: List.generate( | ||
_randomImageNames.length, | ||
(index) { | ||
if (_randomImageNames[index].isEmpty) { | ||
return Container( | ||
width: 100, | ||
height: 150, | ||
color: Colors.transparent, | ||
); | ||
} | ||
return FlipCard( | ||
key: _cardKeys[index], | ||
onFlip: () { | ||
_frontCardCount++; | ||
_frontCardIndexes.add(index); | ||
}, | ||
onFlipDone: (bool) { | ||
if (_frontCardCount == 2) { | ||
_toggleCardToFront(); | ||
|
||
_checkCardIsEqual(); | ||
|
||
_frontCardCount = 0; | ||
|
||
if (_frontCardIndexes.length >= 2) { | ||
String firstCardName = _randomImageNames[_frontCardIndexes[0]]; | ||
String secondCardName = _randomImageNames[_frontCardIndexes[1]]; | ||
if (firstCardName == secondCardName) { | ||
_randomImageNames[_frontCardIndexes[0]] = ''; | ||
_randomImageNames[_frontCardIndexes[1]] = ''; | ||
|
||
setState(() {}); | ||
} | ||
} | ||
|
||
_frontCardIndexes.clear(); | ||
_frontCardCount = 0; | ||
body: StreamBuilder( | ||
stream: _flipCardCore.cardsMatchedStream.stream, | ||
builder: (context, snapshot) { | ||
List<String> _randomImageNames = _flipCardCore.getRandomCardImages(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
StreamBuilder<bool>(
stream: _flipCardCore.cardsMatchedStream.stream,
builder: (context, snapshot) {
// StreamBuilder의 제네릭 타입을 명시해주면, `snapshot.data`의 타입도 명확해집니다.
},
) |
||
|
||
return Center( | ||
child: Wrap( | ||
spacing: 4, | ||
runSpacing: 4, | ||
children: List.generate( | ||
_randomImageNames.length, | ||
(index) { | ||
if (_randomImageNames[index].isEmpty) { | ||
return Container( | ||
width: 100, | ||
height: 150, | ||
color: Colors.transparent, | ||
); | ||
} | ||
return FlipCard( | ||
key: _cardKeys[index], | ||
onFlip: () { | ||
_flipCardCore.flipCard(index); | ||
}, | ||
onFlipDone: (_) { | ||
_flipCardCore.checkCards(); | ||
}, | ||
front: Container( | ||
width: 100, | ||
height: 150, | ||
color: Colors.orange, | ||
), | ||
back: Container( | ||
width: 100, | ||
height: 150, | ||
child: Image.asset( | ||
_randomImageNames[index], | ||
fit: BoxFit.cover, | ||
), | ||
), | ||
); | ||
}, | ||
front: Container( | ||
width: 100, | ||
height: 150, | ||
color: Colors.orange, | ||
), | ||
back: Container( | ||
width: 100, | ||
height: 150, | ||
child: Image.asset( | ||
_randomImageNames[index], | ||
fit: BoxFit.cover, | ||
), | ||
), | ||
); | ||
}, | ||
), | ||
), | ||
), | ||
), | ||
); | ||
}, | ||
), | ||
floatingActionButton: FloatingActionButton( | ||
onPressed: () { | ||
|
@@ -152,20 +130,9 @@ class _MyHomePageState extends State<MyHomePage> { | |
} | ||
} | ||
|
||
void _checkCardIsEqual() { | ||
print('_checkCardIsEqual'); | ||
print(_frontCardIndexes); | ||
if (_frontCardIndexes.length >= 2) { | ||
String firstCardName = _randomImageNames[_frontCardIndexes[0]]; | ||
String secondCardName = _randomImageNames[_frontCardIndexes[1]]; | ||
if (firstCardName == secondCardName) { | ||
_randomImageNames[_frontCardIndexes[0]] = ''; | ||
_randomImageNames[_frontCardIndexes[1]] = ''; | ||
|
||
setState(() {}); | ||
} | ||
} | ||
|
||
_frontCardIndexes.clear(); | ||
@override | ||
void dispose() { | ||
super.dispose(); | ||
_flipCardCore.dispose(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StreamController
를 사용할 때는 제네릭 타입을 명시해주는 것을 권장드려요!