Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Flutter exercise sohaib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sohaib A. Y. Nassar
Flutter exercise sohaib
Commits
9bd2a653
Commit
9bd2a653
authored
3 years ago
by
sohanassa
Browse files
Options
Downloads
Patches
Plain Diff
exercise finished
parent
5e6e7f19
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ex2/lib/main.dart
+68
-7
68 additions, 7 deletions
ex2/lib/main.dart
with
68 additions
and
7 deletions
ex2/lib/main.dart
+
68
−
7
View file @
9bd2a653
import
'dart:async'
;
import
'dart:convert'
;
import
'dart:html'
;
import
'package:flutter/material.dart'
;
import
'package:http/http.dart'
as
http
;
Future
<
Album
>
fetchAlbum
()
async
{
final
response
=
await
http
.
get
(
Uri
.
parse
(
'https://jsonplaceholder.typicode.com/albums/1'
));
List
<
Album
>
parseAlbum
(
String
responseBody
)
{
final
parsed
=
jsonDecode
(
responseBody
)
.
cast
<
Map
<
String
,
dynamic
>>();
return
parsed
.
map
<
Album
>((
data
)
=
>
Album
.
fromJson
(
data
))
.
toList
();
}
Future
<
List
<
Album
>>
fetchAlbum
()
async
{
final
response
=
await
http
.
get
(
Uri
.
parse
(
'https://jsonplaceholder.typicode.com/albums/'
));
if
(
response
.
statusCode
==
200
)
{
// If the server did return a 200 OK response,
// then parse the JSON.
return
Album
.
fromJson
(
jsonDecode
(
response
.
body
)
)
;
return
parse
Album
(
response
.
body
);
}
else
{
// If the server did not return a 200 OK response,
// then throw an exception.
...
...
@@ -49,7 +56,7 @@ class MyApp extends StatefulWidget {
}
class
_MyAppState
extends
State
<
MyApp
>
{
late
Future
<
Album
>
futureAlbum
;
late
Future
<
List
<
Album
>
>
futureAlbum
;
@override
void
initState
()
{
...
...
@@ -69,11 +76,11 @@ class _MyAppState extends State<MyApp> {
title:
const
Text
(
'Fetch Data Example'
),
),
body:
Center
(
child:
FutureBuilder
<
Album
>(
child:
FutureBuilder
<
List
<
Album
>
>
(
future:
futureAlbum
,
builder:
(
context
,
snapshot
)
{
if
(
snapshot
.
hasData
)
{
return
Text
(
snapshot
.
data
!
.
title
);
return
AlbumsList
(
albums:
snapshot
.
data
!
);
}
else
if
(
snapshot
.
hasError
)
{
return
Text
(
'
${snapshot.error}
'
);
}
...
...
@@ -87,3 +94,57 @@ class _MyAppState extends State<MyApp> {
);
}
}
class
AlbumsList
extends
StatelessWidget
{
const
AlbumsList
({
Key
?
key
,
required
this
.
albums
})
:
super
(
key:
key
);
final
List
<
Album
>
albums
;
@override
Widget
build
(
BuildContext
context
)
{
return
GridView
.
builder
(
gridDelegate:
const
SliverGridDelegateWithFixedCrossAxisCount
(
crossAxisCount:
2
,
),
itemCount:
albums
.
length
,
itemBuilder:
(
context
,
index
)
{
return
detailCard
(
albums
[
index
]);
},
);
}
}
Widget
detailCard
(
Album
)
{
return
Padding
(
padding:
const
EdgeInsets
.
all
(
10.0
),
child:
Card
(
color:
Colors
.
grey
[
800
],
child:
Padding
(
padding:
const
EdgeInsets
.
all
(
8.0
),
child:
Column
(
children:
<
Widget
>[
Padding
(
padding:
const
EdgeInsets
.
all
(
8.0
),
child:
Container
(
width:
50.0
,
height:
20.0
,
),
),
Column
(
crossAxisAlignment:
CrossAxisAlignment
.
start
,
children:
<
Widget
>[
Text
(
Album
.
title
,
style:
TextStyle
(
fontWeight:
FontWeight
.
bold
,
color:
Colors
.
white
,
fontSize:
30
),
)
],
)
],
),
),
),
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment