Flutter Challenge The Medium App

Flutter Challenge The Medium App

三月 01, 2019



Flutter Challenges will attempt to recreate a particular app UI or design in
Flutter.

Today we’ll attempt to create the home screen of the Medium app.Note that
the focus will be on the UI rather than actually fetching articles.

Getting Started

Before developing a design,identify the components required to make the
screen in any framework.

The app screen has a drawer,a notification and search option,and a list of
articles.

Let’s take a look at the article



At the very top,the article has a text which gives you relevance as to why it’s
there.For example,if it’s based on your reading history or if it’s from a category
you follow.

Below that is the title of the article iteself,which is bold and black as to stand
out.

Below the title is the author of the article,date of publishing,and how long the
article is estimated to take to read.

On the right hand side is the article image and a bookmark button for saving it
for later.

Setting up the app

Create a new Flutter,example:”medium_app_ui”.

Remove the code for the counter app until you only remain with the a Scaffold
with an AppBar.

Before we move on to the list,let’s contruct the AppBar according to the
Medium design.

As of now,the appBar is blue.We need to change the app bar to black.The actions
on the appBar are notifications and search.Doing these changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
appBar: AppBar(
title: Text('Home'),
backgroundColor: Colors.black,
actions: <Widget>[
Icon(
Icons.notifications_none,
color: Colors.grey,
),
Icon(
Icons.search,
color: Colors.grey,
)
],
),



After this,we need to adjust a few icons and add a drawer.To add a drawer without
and content,simply add.

1
drawer: Drawer(),

to your Scaffold.

Here is the code and resulting AppBar.

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
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Challenge Medium',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Home',
style: TextStyle(
fontWeight: FontWeight.w400,
),
),
backgroundColor: Colors.black,
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: Icon(
Icons.notifications_none,
color: Colors.grey,
),
),
Padding(
padding: const EdgeInsets.only(right: 12.0),
child: Icon(
Icons.search,
color: Colors.grey,
),
)
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[],
),
),
);
}
}



So far,so good.

Let’s create a class to store articles named NewsArticle.dart

1
2
3
4
5
6
7
8
9
10
class NewsArticale {
String categoryTitle;
String title;
String author;
String date;
String readTime;
String imageAssetName;
NewsArticale(this.categoryTitle, this.title, this.author, this.date,
this.readTime, this.imageAssetName);
}

For now,they’re just hardcoded articles.

Creating the List

To create a repeating list in Flutter,we use ListView.builder().

In the image,the cards are over a grey background.

Let’s analyse the card components themselves.



There’s a class named NewsHelper which provides the hard-coded articles.

The final item is build like this:

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
Padding(
padding: const EdgeInsets.fromLTRB(0.0, 0.5, 0.0, 0.5),
child: Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
article.categoryTitle,
style: TextStyle(
color: Colors.black38,
fontWeight: FontWeight.w500,
fontSize: 16.0),
),
Padding(
padding: const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 12.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: Text(
article.title,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22.0,
),
),
flex: 3,
),
Flexible(
flex: 1,
child: Container(
height: 80.0,
width: 80.0,
child: Image.asset(
'assets/' + article.imageAssetName,
fit: BoxFit.cover),
),
)
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
article.author,
style: TextStyle(
fontSize: 18.0,
),
),
Text(
article.date + '.' + article.readTime,
style: TextStyle(
color: Colors.black45,
fontWeight: FontWeight.w500,
),
),
],
),
Icon(Icons.bookmark_border),
],
)
],
),
),
),
)

Designing the Drawer

This is how the drawer looks on the Medium app:


The Drawer is simply a Column with a list of elements and an Image at the
top.The top white portion can be represented by one container and the
bottom half by another.

Here is my drawer code:

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
Drawer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Padding(
padding: const EdgeInsets.fromLTRB(32.0, 64.0, 32.0, 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(Icons.account_circle, size: 90.0),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('John Doe',
style: TextStyle(
fontSize: 20.0,
)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('See profile',
style: TextStyle(
color: Colors.black45,
)),
)
],
),
),
),
Expanded(
child: Container(
color: Colors.black12,
child: Padding(
padding: const EdgeInsets.fromLTRB(40.0, 16.0, 40.0, 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Home', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Audio', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child:
Text('Bookmarks', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child:
Text('Interests', style: TextStyle(fontSize: 18.0)),
),
Divider(),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Become a member',
style: TextStyle(fontSize: 18.0, color: Colors.teal)),
),
Divider(),
Padding(
padding: const EdgeInsets.all(8.0),
child:
Text('New Story', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Stats', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Drafts', style: TextStyle(fontSize: 18.0)),
),
Expanded(
child: Container(
alignment: Alignment.bottomLeft,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(
Icons.account_box,
size: 64.0,
),
Text(
'Setting',
style: TextStyle(fontSize: 18.0),
),
Text('Help', style: TextStyle(fontSize: 18.0))
],
),
),
)
],
),
),
),
)
],
),
)



Here is the final code:

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Challenge Medium',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: buildAppBar(),
body: buildBody(),
drawer: buildDrawer(),
);
}

Drawer buildDrawer() {
return Drawer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Padding(
padding: const EdgeInsets.fromLTRB(32.0, 64.0, 32.0, 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(Icons.account_circle, size: 90.0),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('John Doe',
style: TextStyle(
fontSize: 20.0,
)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('See profile',
style: TextStyle(
color: Colors.black45,
)),
)
],
),
),
),
Expanded(
child: Container(
color: Colors.black12,
child: Padding(
padding: const EdgeInsets.fromLTRB(40.0, 16.0, 40.0, 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Home', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Audio', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child:
Text('Bookmarks', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child:
Text('Interests', style: TextStyle(fontSize: 18.0)),
),
Divider(),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Become a member',
style: TextStyle(fontSize: 18.0, color: Colors.teal)),
),
Divider(),
Padding(
padding: const EdgeInsets.all(8.0),
child:
Text('New Story', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Stats', style: TextStyle(fontSize: 18.0)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Drafts', style: TextStyle(fontSize: 18.0)),
),
Expanded(
child: Container(
alignment: Alignment.bottomLeft,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(
Icons.account_box,
size: 64.0,
),
Text(
'Setting',
style: TextStyle(fontSize: 18.0),
),
Text('Help', style: TextStyle(fontSize: 18.0))
],
),
),
)
],
),
),
),
)
],
),
);
}

Center buildBody() {
return Center(
child: ListView.builder(
itemCount: NewsHelper.articleCount,
itemBuilder: (contex, position) {
NewsArticle article = NewsHelper.getArticle(position);
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 0.5, 0.0, 0.5),
child: Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
article.categoryTitle,
style: TextStyle(
color: Colors.black38,
fontWeight: FontWeight.w500,
fontSize: 16.0),
),
Padding(
padding: const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 12.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: Text(
article.title,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22.0,
),
),
flex: 3,
),
Flexible(
flex: 1,
child: Container(
height: 80.0,
width: 80.0,
child: Image.asset(
'assets/' + article.imageAssetName,
fit: BoxFit.cover),
),
)
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
article.author,
style: TextStyle(
fontSize: 18.0,
),
),
Text(
article.date + '.' + article.readTime,
style: TextStyle(
color: Colors.black45,
fontWeight: FontWeight.w500,
),
),
],
),
Icon(Icons.bookmark_border),
],
)
],
),
),
),
);
},
));
}

AppBar buildAppBar() {
return AppBar(
title: Text(
'Home',
style: TextStyle(
fontWeight: FontWeight.w400,
),
),
backgroundColor: Colors.black,
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: Icon(
Icons.notifications_none,
color: Colors.grey,
),
),
Padding(
padding: const EdgeInsets.only(right: 12.0),
child: Icon(
Icons.search,
color: Colors.grey,
),
)
],
);
}
}

class NewsArticle {
String categoryTitle;
String title;
String author;
String date;
String readTime;
String imageAssetName;
NewsArticle(this.categoryTitle, this.title, this.author, this.date,
this.readTime, this.imageAssetName);
}

class NewsHelper {
static var articleCount = 4;

static var categoryTitles = [
"SPACE",
"FROM YOUR NETWORK",
"BASED ON YOUR READING HISTORY",
"DATA SCIENCE"
];
static var titles = [
"Sorry, Methane and 'Organics' On Mars Are Not Evidence For Life",
"A crash course on Serverless APIs with Express and MongoDB",
"What happened Gmail?",
"A year as a Data Scientist right after college: An honest review"
];
static var authorNames = [
"Ethan Siegal",
"Adnan Rahic",
"Avi Ashkenazi",
"Abhishek Parkbhakar"
];
static var date = ["15 Jun", "15 hours ago", "27 Apr", "14 Jun"];
static var readTimes = [
"7 min read",
"14 min read",
"8 min read",
"8 min read"
];
static var imageAssetName = [
"mars.jpeg",
"cars.jpeg",
"gmail.jpeg",
"umbrella.jpeg"
];

static getArticle(int position) {
return NewsArticle(
categoryTitles[position],
titles[position],
authorNames[position],
date[position],
readTimes[position],
imageAssetName[position]);
}
}