[무작정 플러터 강의노트] 08 플러터와 파이어베이스 연동하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Movie Data
[{
title: 보헤미안 랩소디
keyword: 음악/드라마/인물
poster: https://movie-phinf.pstatic.net/20181213_264/1544692854634ss65r_JPEG/movie_image.jpg
like: false
},
{
title: 사랑의 불시착
keyword: 가슴 뭉클/로맨스/코미디/금지된 사랑/정반대 캐릭터
poster: https://file.mk.co.kr/meet/neds/2019/12/image_readtop_2019_1046822_15762744634013454.jpg
like: false
},
{
title: 포레스트 검프
keyword: 드라마/외국
poster: https://movie-phinf.pstatic.net/20160901_187/1472711688297YP2jH_JPEG/movie_image.jpg
like: false
},
{
title: 쇼생크 탈출
keyword: 추리/반전/서스펜스
poster: https://movie-phinf.pstatic.net/20160119_278/14531650465287bcuk_JPEG/movie_image.jpg
like: false
},
{
title: 라이언 일병 구하기
keyword: 드라마/전쟁/역사
poster: https://movie-phinf.pstatic.net/20111222_4/1324498435937yqCYm_JPEG/movie_image.jpg
like: false
}]
1
2
3
4
5
6
7
8
9
# pubspec.yaml
dependencies:
flutter:
sdk: flutter
carousel_slider:
flutter_linkify:
url_launcher:
cloud_firestore:
firebase_core:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// model_movie.dart
import 'package:cloud_firestore/cloud_firestore.dart';

class Movie {
final String title;
final String keyword;
final String poster;
final bool like;
final DocumentReference reference;

Movie.fromMap(Map<String, dynamic> map, {this.reference})
: title = map['title'],
keyword = map['keyword'],
poster = map['poster'],
like = map['like'];

Movie.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);

@override
String toString() => "Movie<$title:$keyword>";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// home_screen.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:netflix_clone_lecture_note/model/model_movie.dart';
import 'package:netflix_clone_lecture_note/widget/box_slider.dart';
import 'package:netflix_clone_lecture_note/widget/carousel_slider.dart';
import 'package:netflix_clone_lecture_note/widget/circle_slider.dart';

class HomeScreen extends StatefulWidget {
_HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
Firestore firestore = Firestore.instance;
Stream<QuerySnapshot> streamData;

@override
void initState() {
super.initState();
streamData = firestore.collection('movie').snapshots();
}

Widget _fetchData(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('movie').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildBody(context, snapshot.data.documents);
},
);
}

Widget _buildBody(BuildContext context, List<DocumentSnapshot> snapshot) {
List<Movie> movies = snapshot.map((d) => Movie.fromSnapshot(d)).toList();
return ListView(
children: <Widget>[
Stack(
children: <Widget>[
CarouselImage(movies: movies),
TopBar(),
],
),
CircleSlider(movies: movies),
BoxSlider(movies: movies),
],
);
}

@override
Widget build(BuildContext context) {
return _fetchData(context);
}
}

class TopBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.fromLTRB(20, 7, 20, 7),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Image.asset(
'images/bbongflix_logo.png',
fit: BoxFit.contain,
height: 25,
),
Container(
padding: EdgeInsets.only(right: 1),
child: Text(
'TV 프로그램',
style: TextStyle(fontSize: 14),
),
),
Container(
padding: EdgeInsets.only(right: 1),
child: Text(
'영화',
style: TextStyle(fontSize: 14),
),
),
Container(
padding: EdgeInsets.only(right: 1),
child: Text(
'내가 찜한 콘텐츠',
style: TextStyle(fontSize: 14),
),
),
],
),
);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// carousel_slider.dart
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:netflix_clone_lecture_note/model/model_movie.dart';
import 'package:netflix_clone_lecture_note/screen/detail_screen.dart';

class CarouselImage extends StatefulWidget {
final List<Movie> movies;
CarouselImage({this.movies});
_CarouselImageState createState() => _CarouselImageState();
}

class _CarouselImageState extends State<CarouselImage> {
List<Movie> movies;
List<Widget> images;
List<String> keywords;
List<bool> likes;
int _currentPage = 0;
String _currentKeyword;

@override
void initState() {
super.initState();
movies = widget.movies;
images = movies.map((m) => Image.network(m.poster)).toList();
keywords = movies.map((m) => m.keyword).toList();
likes = movies.map((m) => m.like).toList();
_currentKeyword = keywords[0];
}

@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(20),
),
CarouselSlider(
items: images,
onPageChanged: (index) {
setState(() {
_currentPage = index;
_currentKeyword = keywords[_currentPage];
});
},
),
Container(
padding: EdgeInsets.fromLTRB(0, 10, 0, 3),
child: Text(
_currentKeyword,
style: TextStyle(fontSize: 11),
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
child: Column(
children: <Widget>[
likes[_currentPage]
? IconButton(
icon: Icon(Icons.check),
onPressed: () {
setState(() {
likes[_currentPage] = !likes[_currentPage];
movies[_currentPage].reference.updateData(
{'like': likes[_currentPage]});
});
},
)
: IconButton(
icon: Icon(Icons.add),
onPressed: () {
setState(() {
likes[_currentPage] = !likes[_currentPage];
movies[_currentPage].reference.updateData(
{'like': likes[_currentPage]});
});
},
),
Text(
'내가 찜한 콘텐츠',
style: TextStyle(fontSize: 11),
),
],
),
),
Container(
padding: EdgeInsets.only(right: 10),
child: FlatButton(
color: Colors.white,
onPressed: () {},
child: Row(
children: <Widget>[
Icon(
Icons.play_arrow,
color: Colors.black,
),
Padding(
padding: EdgeInsets.all(3),
),
Text(
'재생',
style: TextStyle(color: Colors.black),
),
],
),
),
),
Container(
padding: EdgeInsets.only(right: 10),
child: Column(
children: <Widget>[
IconButton(
icon: Icon(Icons.info),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute<Null>(
fullscreenDialog: true,
builder: (BuildContext context) {
return DetailScreen(
movie: movies[_currentPage],
);
}));
},
),
Text(
'정보',
style: TextStyle(fontSize: 11),
)
],
),
)
],
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: makeIndicator(likes, _currentPage),
)),
],
),
);
}
}

List<Widget> makeIndicator(List list, int _currentPage) {
List<Widget> results = [];
for (var i = 0; i < list.length; i++) {
results.add(Container(
width: 8,
height: 8,
margin: EdgeInsets.symmetric(vertical: 10.0, horizontal: 2.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _currentPage == i
? Color.fromRGBO(255, 255, 255, 0.9)
: Color.fromRGBO(255, 255, 255, 0.4),
),
));
}

return results;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// circle_slider.dart
import 'package:flutter/material.dart';
import 'package:netflix_clone_lecture_note/model/model_movie.dart';
import 'package:netflix_clone_lecture_note/screen/detail_screen.dart';

class CircleSlider extends StatelessWidget {
final List<Movie> movies;
CircleSlider({this.movies});
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(7),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('미리보기'),
Container(
height: 120,
child: ListView(
scrollDirection: Axis.horizontal,
children: makeCircleImages(context, movies),
),
),
],
),
);
}
}

List<Widget> makeCircleImages(BuildContext context, List<Movie> movies) {
List<Widget> results = [];
for (var i = 0; i < movies.length; i++) {
results.add(
InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute<Null>(
fullscreenDialog: true,
builder: (BuildContext context) {
return DetailScreen(
movie: movies[i],
);
}));
},
child: Container(
padding: EdgeInsets.only(right: 10),
child: Align(
alignment: Alignment.centerLeft,
child: CircleAvatar(
backgroundImage: NetworkImage(movies[i].poster),
radius: 48,
),
),
),
),
);
}
return results;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// box_slider.dart
import 'package:flutter/material.dart';
import 'package:netflix_clone_lecture_note/model/model_movie.dart';
import 'package:netflix_clone_lecture_note/screen/detail_screen.dart';

class BoxSlider extends StatelessWidget {
final List<Movie> movies;
BoxSlider({this.movies});
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(7),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('지금 뜨는 콘텐츠'),
Container(
height: 120,
child: ListView(
scrollDirection: Axis.horizontal,
children: makeBoxImages(context, movies),
),
)
],
),
);
}
}

List<Widget> makeBoxImages(BuildContext context, List<Movie> movies) {
List<Widget> results = [];
for (var i = 0; i < movies.length; i++) {
results.add(InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute<Null>(
fullscreenDialog: true,
builder: (BuildContext context) {
return DetailScreen(
movie: movies[i],
);
}));
},
child: Container(
padding: EdgeInsets.only(right: 10),
child: Align(
alignment: Alignment.centerLeft,
child: Image.network(movies[i].poster),
),
),
));
}
return results;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// detail_screen.dart
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:netflix_clone_lecture_note/model/model_movie.dart';

class DetailScreen extends StatefulWidget {
final Movie movie;
DetailScreen({this.movie});
_DetailScreenState createState() => _DetailScreenState();
}

class _DetailScreenState extends State<DetailScreen> {
bool like = false;
@override
void initState() {
super.initState();
like = widget.movie.like;
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: SafeArea(
child: ListView(
children: <Widget>[
Stack(
children: <Widget>[
Container(
width: double.maxFinite,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(widget.movie.poster),
fit: BoxFit.cover,
),
),
child: ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: Container(
alignment: Alignment.center,
color: Colors.black.withOpacity(0.1),
child: Container(
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(0, 45, 0, 10),
child: Image.network(widget.movie.poster),
height: 300,
),
Container(
padding: EdgeInsets.all(7),
child: Text(
'99% 일치 2019 15+ 시즌 1개',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 13),
),
),
Container(
padding: EdgeInsets.all(7),
child: Text(
widget.movie.title,
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
Container(
padding: EdgeInsets.all(3),
child: FlatButton(
onPressed: () {},
color: Colors.red,
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.play_arrow),
Text('재생'),
],
),
),
),
Container(
padding: EdgeInsets.all(5),
child: Text(widget.movie.toString()),
),
Container(
padding: EdgeInsets.all(5),
alignment: Alignment.centerLeft,
child: Text(
'출연: 현빈, 손예진, 서지혜\n제작자: 이정효, 박지은',
style: TextStyle(
color: Colors.white60,
fontSize: 12,
),
),
)
],
),
),
),
),
),
),
Positioned(
child: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
),
),
],
),
Container(
color: Colors.black26,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
child: InkWell(
onTap: () {
setState(() {
like = !like;
widget.movie.reference.updateData({'like': like});
});
},
child: Column(
children: <Widget>[
like ? Icon(Icons.check) : Icon(Icons.add),
Padding(
padding: EdgeInsets.all(5),
),
Text(
'내가 찜한 콘텐츠',
style: TextStyle(
fontSize: 11,
color: Colors.white60,
),
)
],
),
),
),
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
child: Container(
child: Column(
children: <Widget>[
Icon(Icons.thumb_up),
Padding(
padding: EdgeInsets.all(5),
),
Text(
'평가',
style: TextStyle(
fontSize: 11, color: Colors.white60),
)
],
),
),
),
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
child: Container(
child: Column(
children: <Widget>[
Icon(Icons.send),
Padding(padding: EdgeInsets.all(5)),
Text(
'공유',
style: TextStyle(
fontSize: 11, color: Colors.white60),
),
],
),
),
)
],
),
)
],
),
),
),
);
}
}

[무작정 플러터 강의노트] 08 플러터와 파이어베이스 연동하기

https://taebbong.github.io/2020/03/23/2020-03-23-bbongflix-lec08-post/

Author

TaeBbong Kwon

Posted on

2020-03-23

Updated on

2022-08-06

Licensed under

Comments