Skip to content

Commit

Permalink
♻️ Refactor(#4) :: 각 컴포넌트 모듈화
Browse files Browse the repository at this point in the history
  • Loading branch information
mic050r committed Jan 29, 2025
1 parent 02f2af8 commit 47611b9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 76 deletions.
90 changes: 14 additions & 76 deletions lib/pages/today_song_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:tj_musical_number_book/models/song.dart';
import 'package:tj_musical_number_book/services/song_service.dart';
import 'package:tj_musical_number_book/theme/colors.dart';
import 'package:tj_musical_number_book/widgets/song_card.dart';

class SavedSongsPage extends StatefulWidget {
const SavedSongsPage({super.key});
Expand All @@ -16,17 +17,14 @@ class _SavedSongsPageState extends State<SavedSongsPage> {
@override
void initState() {
super.initState();
// 초기화: 저장된 노래 목록을 불러오기
loadSavedSongs();
}

// 저장된 노래 목록을 불러오기
Future<void> loadSavedSongs() async {
savedSongs = await getSavedSongs();
setState(() {});
}

// 드래그로 순서 변경 후 저장하기
void _onReorder(int oldIndex, int newIndex) {
setState(() {
if (newIndex > oldIndex) {
Expand Down Expand Up @@ -61,87 +59,27 @@ class _SavedSongsPageState extends State<SavedSongsPage> {
return const Center(child: Text('저장된 노래가 없습니다.'));
}

savedSongs = snapshot.data!;
savedSongs = snapshot.data!;

return ReorderableListView(
onReorder: _onReorder,
// 수정된 부분
children: savedSongs
.asMap()
.map((index, song) {
return MapEntry(
index, // 인덱스를 key로 사용
Card(
key: ValueKey(index.toString()), // 고유한 Key 부여
margin: const EdgeInsets.symmetric(
vertical: 4.0, horizontal: 8.0),
child: ListTile(
leading: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'${index + 1}', // 1부터 시작하는 번호
style: const TextStyle(
color: AppColors.primary,
fontWeight: FontWeight.bold,
fontSize: 16.0, // 번호 텍스트 크기
),
),
SizedBox(width: 8.0), // 번호와 이미지 사이의 간격
Container(
width: 50.0,
height: 50.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
),
child: Stack(
alignment: Alignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.asset(
'./assets/imgs/logo/${song.img.isNotEmpty ? song.img : 'default.png'}',
fit: BoxFit.cover,
),
),
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Container(
color: Colors.black.withOpacity(0.43),
),
),
Text(
song.tjNumber,
style: const TextStyle(
color: AppColors.white,
fontWeight: FontWeight.bold,
fontSize: 11.0,
),
),
],
),
),
],
),
title: Text(song.title,
style:
const TextStyle(fontWeight: FontWeight.bold)),
subtitle: Text(song.singer),
trailing: IconButton(
icon: const Icon(Icons.remove_circle_outline,
color: AppColors.circleBorder),
onPressed: () async {
await deleteSong(song);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('"${song.title}" 번호가 삭제되었습니다.'),
),
);
loadSavedSongs();
},
),
),
index,
SongCard(
key: ValueKey(index), // 여기에 key를 설정
song: song,
index: index,
onDelete: () async {
await deleteSong(song);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('"${song.title}" 번호가 삭제되었습니다.')),
);
loadSavedSongs();
},
),
);
})
Expand Down
79 changes: 79 additions & 0 deletions lib/widgets/song_card.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'package:flutter/material.dart';
import 'package:tj_musical_number_book/models/song.dart';
import 'package:tj_musical_number_book/theme/colors.dart';

class SongCard extends StatelessWidget {
final Song song;
final int index;
final VoidCallback onDelete;

const SongCard({
Key? key,
required this.song,
required this.index,
required this.onDelete,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Card(
key: ValueKey(index.toString()),
margin: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 8.0),
child: ListTile(
leading: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'${index + 1}',
style: const TextStyle(
color: AppColors.primary,
fontWeight: FontWeight.bold,
fontSize: 16.0,
),
),
const SizedBox(width: 8.0),
Container(
width: 50.0,
height: 50.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
),
child: Stack(
alignment: Alignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.asset(
'./assets/imgs/logo/${song.img.isNotEmpty ? song.img : 'default.png'}',
fit: BoxFit.cover,
),
),
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Container(
color: Colors.black.withOpacity(0.43),
),
),
Text(
song.tjNumber,
style: const TextStyle(
color: AppColors.white,
fontWeight: FontWeight.bold,
fontSize: 11.0,
),
),
],
),
),
],
),
title: Text(song.title, style: const TextStyle(fontWeight: FontWeight.bold)),
subtitle: Text(song.singer),
trailing: IconButton(
icon: const Icon(Icons.remove_circle_outline, color: AppColors.circleBorder),
onPressed: onDelete,
),
),
);
}
}

0 comments on commit 47611b9

Please sign in to comment.