[무작정 플러터 강의노트] 04 홈 화면에 이미지 캐로셀 슬라이더 위젯 추가하기

1
2
3
4
5
# pubspec.yaml
dependencies:
flutter:
sdk: flutter
carousel_slider:
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
// lib/screen/home_screen.dart
import 'package:flutter/material.dart';
import 'package:netflix_clone_lecture_note/model/model_movie.dart';
import 'package:netflix_clone_lecture_note/widget/carousel_slider.dart';

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

class _HomeScreenState extends State<HomeScreen> {
List<Movie> movies = [
Movie.fromMap({
'title': '사랑의 불시착',
'keyword': '사랑/로맨스/판타지',
'poster': 'test_movie_1.png',
'like': false
}),
Movie.fromMap({
'title': '사랑의 불시착',
'keyword': '사랑/로맨스/판타지',
'poster': 'test_movie_1.png',
'like': false
}),
Movie.fromMap({
'title': '사랑의 불시착',
'keyword': '사랑/로맨스/판타지',
'poster': 'test_movie_1.png',
'like': false
}),
Movie.fromMap({
'title': '사랑의 불시착',
'keyword': '사랑/로맨스/판타지',
'poster': 'test_movie_1.png',
'like': false
}),
];
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return ListView(
children: <Widget>[
Stack(
children: <Widget>[
CarouselImage(movies: movies),
TopBar(),
],
)
],
);
}
}

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
// lib/widget/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';

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.asset('./img/images/' + 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: () {},
)
: IconButton(
icon: Icon(Icons.add),
onPressed: () {},
),
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: () {},
),
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;
}

[무작정 플러터 강의노트] 04 홈 화면에 이미지 캐로셀 슬라이더 위젯 추가하기

https://taebbong.github.io/2020/03/21/2020-03-21-bbongflix-lec04-post/

Author

TaeBbong Kwon

Posted on

2020-03-21

Updated on

2023-01-02

Licensed under

Comments