개발 중인 앱 PEEP은 사용자의 감정에 따라 음악을 추천하여 재생해준다. 감정은 총 5가지로, HAPPY, SAD, ANGRY, CALM, FEAR이다.
어제의 개발은...
music_player_page.dart
는 음악 플레이어 화면이고 노래의 느낌에 맞게 분류된 감정에 따라 배경 색이 달라진다.
music_player_page.dart
에서 아래와 같이 감정에 대한 색이 하드코딩 되어 있다.
var pageColor = Colors.white;
if (randomEmotion == 'happy') {
pageColor = const Color(0xFFd5b15c);
} else if (randomEmotion == 'sad') {
pageColor = const Color(0xFF7fbad0);
} else if (randomEmotion == 'angry') {
pageColor = const Color(0xFFd77881);
} else if (randomEmotion == 'calm') {
pageColor = const Color(0xFF669f82);
} else if (randomEmotion == 'fear') {
pageColor = const Color(0xFF8481ac);
}
또한, 같은 화면에서 파도 물결이 넘실거리는 애니메이션이 있는데 이 또한 감정색과 어울리는 비슷한 색이며 위의 코드와 Color()
의 HEX 값의 차이만 있다.
if (randomEmotion == 'happy') {
pickColor = const Color(0xFFe8c075);
pickColor1 = const Color(0xFFd57a47);
} else if (randomEmotion == 'sad') {
pickColor = const Color(0xFFa5c3d7);
pickColor1 = const Color(0xFF446199);
} else if (randomEmotion == 'angry') {
pickColor = const Color(0xFFd77d92);
pickColor1 = const Color(0xFFcc4e60);
} else if (randomEmotion == 'calm') {
pickColor = const Color(0xFF57a481);
pickColor1 = const Color(0xFF426641);
} else if (randomEmotion == 'fear') {
pickColor = const Color(0xFF8776a4);
pickColor1 = const Color(0xFF564986);
}
내가 느낀 이 코드의 문제점과 방향성은 아래와 같다.
music_player_page.dart
의 주요 목적은 플레이어 화면을 보여주는 것인데, 특정 위젯의 디테일한 설정이 이 파일 안에 드러나 있어 파일의 목적과는 다소 거리감 있어보임해결 방법은 아래와 같다.
감정의 색을 분리한 소스 코드 파일이다.
class EmotionColor {
static const HAPPY = "HAPPY";
static const SAD = "SAD";
static const ANGRY = "ANGRY";
static const CALM = "CALM";
static const FEAR = "FEAR";
static const DEFAULT = "DEFAULT";
static const _normalColorMap = {
HAPPY: const Color(0xFFd5b15c),
SAD: const Color(0xFF7fbad0),
ANGRY: const Color(0xFFd77881),
CALM: const Color(0xFF669f82),
FEAR: const Color(0xFF8481ac),
DEFAULT: Colors.white,
};
static const _lightColorMap = {
HAPPY: const Color(0xFFe8c075),
SAD: const Color(0xFFa5c3d7),
ANGRY: const Color(0xFFd77d92),
CALM: const Color(0xFF57a481),
FEAR: const Color(0xFF8776a4),
DEFAULT: Colors.white,
};
static const _darkColorMap = {
HAPPY: const Color(0xFFd57a47),
SAD: const Color(0xFF446199),
ANGRY: const Color(0xFFcc4e60),
CALM: const Color(0xFF426641),
FEAR: const Color(0xFF564986),
DEFAULT: Colors.white,
};
const EmotionColor._();
static getNormalColorFor(String emotion) => _normalColorMap[emotion.toUpperCase()] ?? _normalColorMap[DEFAULT];
static getLightColorFor(String emotion) => _lightColorMap[emotion.toUpperCase()] ?? _lightColorMap[DEFAULT];
static getDarkColorFor(String emotion) => _darkColorMap[emotion.toUpperCase()] ?? _darkColorMap[DEFAULT];
}
if (randomEmotion == null || randomEmotion == "") {
randomEmotion = "default";
}
EmotionColor.getNormalColorFor(randomEmotion),
randomEmotion
이라는 변수에 감정 종류를 문자열로 입력받고 함수를 호출하여 매개변수로 넘긴다.
InheritedWidget
를 이용해 변수가 변경되었을 때 UI에도 바로 적용될 수 있도록 하는 것이 필요해보여 클래스를 생성하였으나, 기본값 설정이 안돼서 에러가 남. 기본값 설정 방법을 찾는 중이다.class CurrentEmotion extends InheritedWidget {
const CurrentEmotion({
Key key,
this.emotion,
Widget child,
}) : super(key: key, child: child);
final String emotion;
static CurrentEmotion of(BuildContext context) {
final CurrentEmotion result = context.dependOnInheritedWidgetOfExactType<CurrentEmotion>();
assert(result != null, 'No CurrentEmotion found in context');
return result;
}
@override
bool updateShouldNotify(CurrentEmotion old) => emotion != old.emotion;
}