08. 실습 - Shazam 클론 코딩
import 'dart:convert';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Shazam',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
initialIndex: 1,
length: 3,
child: Builder(builder: (context) {
DefaultTabController.of(context)?.addListener(() {
setState(() {});
});
return Scaffold(
body: Stack(
children: [
TabBarView(
children: [
FirstTab(),
SecondTab(),
ThirdTab(),
],
),
SafeArea(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 20, horizontal: 16),
child: Column(
children: [
Container(
alignment: Alignment.topCenter,
child: TabPageSelector(
color: DefaultTabController.of(context)?.index == 1
? Colors.blue[300]
: Colors.grey[400],
selectedColor:
DefaultTabController.of(context)?.index == 1
? Colors.white
: Colors.blue,
indicatorSize: 8,
),
),
],
),
),
),
],
),
);
}),
);
}
}
class FirstTab extends StatelessWidget {
const FirstTab({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
const songs = [
{
'imageUrl': 'https://i.ytimg.com/vi/jAO0KXRdz_4/hqdefault.jpg',
'title': '가을밤에 든 생각1',
'artist': '잔나비',
},
{
'imageUrl': 'https://i.ytimg.com/vi/jAO0KXRdz_4/hqdefault.jpg',
'title': '가을밤에 든 생각2',
'artist': '잔나비',
},
{
'imageUrl': 'https://i.ytimg.com/vi/jAO0KXRdz_4/hqdefault.jpg',
'title': '가을밤에 든 생각3',
'artist': '잔나비',
},
{
'imageUrl': 'https://i.ytimg.com/vi/jAO0KXRdz_4/hqdefault.jpg',
'title': '가을밤에 든 생각4',
'artist': '잔나비',
},
{
'imageUrl': 'https://i.ytimg.com/vi/jAO0KXRdz_4/hqdefault.jpg',
'title': '가을밤에 든 생각5',
'artist': '잔나비',
},
{
'imageUrl': 'https://i.ytimg.com/vi/jAO0KXRdz_4/hqdefault.jpg',
'title': '가을밤에 든 생각6',
'artist': '잔나비',
},
];
return SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Icon(Icons.settings),
),
Text(
"라이브러리",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
Icon(null),
]),
SizedBox(
height: 8,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: [
Image.network(
"https://cdn.iconscout.com/icon/free/png-256/shazam-3-761709.png",
height: 20,
width: 20,
fit: BoxFit.fill,
),
SizedBox(
width: 8,
),
Text(
"Shazam",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
],
),
),
Divider(),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: [
Icon(
Icons.person_rounded,
color: Colors.black,
),
SizedBox(
width: 8,
),
Text(
"아티스트",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
],
),
),
Divider(),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: [
Icon(Icons.music_note),
SizedBox(
width: 8,
),
Text(
"회원님을 위한 재생 목록",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
],
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Container(
alignment: Alignment.topLeft,
child: Text(
"최근 Shazam",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
)),
),
Expanded(
child: GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 3 / 5,
),
itemCount: songs.length,
itemBuilder: (BuildContext context, int index) {
String imageUrl = songs[index]['imageUrl']!;
String title = songs[index]['title']!;
String artist = songs[index]['artist']!;
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.black26)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10)),
child: Image.network(
imageUrl,
fit: BoxFit.fitHeight,
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Stack(children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
Text(
artist,
style: TextStyle(
color: Colors.black26,
fontSize: 14,
),
),
],
),
Positioned(
bottom: 5,
child: Image.network(
'https://images.squarespace-cdn.com/content/v1/5b31aa3a2487fddecb16c83d/1571241639561-RZS5N4CB4VQT3BEH3L1B/applemusic.png',
width: 60,
)),
]),
),
),
],
),
),
);
},
),
),
]),
),
);
}
}
class SecondTab extends StatelessWidget {
const SecondTab({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.blue[300]!, Colors.blue[900]!]),
),
child: SafeArea(
child: Column(children: [
Row(children: [
Padding(
padding: EdgeInsets.all(8),
child: Column(children: [
Icon(
Icons.person,
color: Colors.white,
),
Text(
'라이브러리',
style: TextStyle(color: Colors.white),
)
]),
),
Spacer(),
Padding(
padding: EdgeInsets.all(8),
child: Column(children: [
Icon(
Icons.show_chart,
color: Colors.white,
),
Text(
'차트',
style: TextStyle(color: Colors.white),
)
]),
),
]),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
"Shazam하려면 탭하세요.",
style: TextStyle(
color: Colors.white,
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
Container(
alignment: Alignment.center,
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.lightBlue,
borderRadius: BorderRadius.circular(100),
),
child: Image.network(
"https://cdn.iconscout.com/icon/free/png-256/shazam-3-761709.png",
height: 130,
width: 130,
fit: BoxFit.fill,
),
),
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: Colors.lightBlue,
borderRadius: BorderRadius.circular(25)),
child: Icon(
Icons.search_rounded,
color: Colors.white,
size: 25,
),
),
],
),
),
]),
),
);
}
}
class ThirdTab extends StatelessWidget {
const ThirdTab({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
const chartData = {
'korea': [
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite1',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite2',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite3',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite4',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite5',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite6',
'artist': 'BTS',
},
],
'global': [
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite g1',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite g2',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite g3',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite g4',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite g5',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite g6',
'artist': 'BTS',
},
],
'newyork': [
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite n1',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite n2',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite n3',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite n4',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite n5',
'artist': 'BTS',
},
{
'imageUrl': 'https://i.ibb.co/xf2HpfG/dynamite.jpg',
'name': 'Dynamite n6',
'artist': 'BTS',
},
],
};
return SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'차트',
style: TextStyle(
color: Color.fromARGB(255, 83, 83, 83),
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
SizedBox(
height: 16,
),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
Stack(alignment: Alignment.center, children: [
Container(
width: double.infinity,
height: 150,
decoration: BoxDecoration(
color: Colors.purple,
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
alignment: Alignment.center,
width: 200,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(4)),
child: Text(
'국가 및 도시별 차트',
style: TextStyle(color: Colors.purple),
),
),
),
Text(
'전 세계',
style: TextStyle(color: Colors.white),
),
],
),
]),
AlbumCard(
tmpObj: chartData['korea'],
chartName: "대한민국 차트",
),
AlbumCard(
tmpObj: chartData['global'],
chartName: "글로벌 차트",
),
AlbumCard(
tmpObj: chartData['newyork'],
chartName: "뉴욕 차트",
),
],
),
),
),
],
));
}
}
class AlbumCard extends StatelessWidget {
const AlbumCard({
Key? key,
List<Map<String, String>>? this.tmpObj,
required String this.chartName,
}) : super(key: key);
final List<Map<String, String>>? tmpObj;
final String chartName;
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(
width: double.infinity,
height: 8,
color: Colors.grey,
),
Row(children: [
Text(chartName),
Spacer(),
TextButton(
onPressed: () {},
child: Text(
'모두 보기',
),
),
]),
SizedBox(
height: 180,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: tmpObj!.length,
itemBuilder: (context, index) {
final tmpUrl = tmpObj![index]['imageUrl'];
final tmpName = tmpObj![index]['name'];
final tmpArtist = tmpObj![index]['artist'];
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.network(
tmpUrl!,
width: MediaQuery.of(context).size.width * 0.29,
),
Text(
tmpName!,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
Text(tmpArtist!),
],
),
);
}),
),
],
);
}
}