Skip to content

Commit d43a3f3

Browse files
committed
lesson-28
1 parent 11cf63a commit d43a3f3

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

world_time_app/lib/pages/loading.dart

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/material.dart';
2-
import 'package:http/http.dart';
3-
import 'dart:convert';
2+
import 'package:world_time_app/services/world_time.dart';
43

54
class Loading extends StatefulWidget {
65
@override
@@ -9,34 +8,30 @@ class Loading extends StatefulWidget {
98

109
class _LoadingState extends State<Loading> {
1110

12-
void getTime() async {
13-
// make the request
14-
Response response = await get('http://worldtimeapi.org/api/timezone/Europe/London');
15-
Map data = jsonDecode(response.body);
16-
//print(data);
11+
String time = 'loading';
1712

18-
// get properties from json
19-
String datetime = data['datetime'];
20-
String offset = data['utc_offset'].substring(1,3);
21-
//print(datetime);
22-
//print(offset);
23-
24-
// create DateTime object
25-
DateTime now = DateTime.parse(datetime);
26-
now = now.add(Duration(hours: int.parse(offset)));
27-
print(now);
13+
void setupWorldTime() async {
14+
WorldTime instance = WorldTime(location: 'Berlin', flag: 'germany.png', url: 'Europe/Berlin');
15+
await instance.getTime();
16+
print(instance.time);
17+
setState(() {
18+
time = instance.time;
19+
});
2820
}
2921

3022
@override
3123
void initState() {
3224
super.initState();
33-
getTime();
25+
setupWorldTime();
3426
}
3527

3628
@override
3729
Widget build(BuildContext context) {
3830
return Scaffold(
39-
body: Text('loading screen'),
31+
body: Padding(
32+
padding: const EdgeInsets.all(50.0),
33+
child: Text(time),
34+
),
4035
);
4136
}
4237
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import 'package:http/http.dart';
2+
import 'dart:convert';
3+
4+
class WorldTime {
5+
6+
String location; // location name for UI
7+
String time; // the time in that location
8+
String flag; // url to an asset flag icon
9+
String url; // location url for api endpoint
10+
11+
WorldTime({ this.location, this.flag, this.url });
12+
13+
Future<void> getTime() async {
14+
// make the request
15+
Response response = await get('http://worldtimeapi.org/api/timezone/$url');
16+
Map data = jsonDecode(response.body);
17+
18+
// get properties from json
19+
String datetime = data['datetime'];
20+
String offset = data['utc_offset'].substring(1,3);
21+
22+
// create DateTime object
23+
DateTime now = DateTime.parse(datetime);
24+
now = now.add(Duration(hours: int.parse(offset)));
25+
26+
print(now);
27+
28+
// set the time property
29+
time = now.toString();
30+
}
31+
32+
}
33+
34+
// WorldTime instance = WorldTime(location: 'Berlin', flag: 'germany.png', url: 'Europe/Berlin');

0 commit comments

Comments
 (0)