Skip to content

Commit 11cf63a

Browse files
committed
lesson-27
1 parent 0493d74 commit 11cf63a

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

world_time_app/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:world_time_app/pages/loading.dart';
44
import 'package:world_time_app/pages/choose_location.dart';
55

66
void main() => runApp(MaterialApp(
7-
initialRoute: '/home',
7+
initialRoute: '/',
88
routes: {
99
'/': (context) => Loading(),
1010
'/home': (context) => Home(),

world_time_app/lib/pages/home.dart

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,9 @@ class Home extends StatefulWidget {
99

1010
class _HomeState extends State<Home> {
1111

12-
void getData() async {
13-
14-
Response response = await get('https://jsonplaceholder.typicode.com/todos/1');
15-
// print(response.body);
16-
Map data = jsonDecode(response.body);
17-
print(data);
18-
print(data['title']);
19-
20-
}
21-
2212
@override
2313
void initState() {
2414
super.initState();
25-
getData();
2615
}
2716

2817
@override

world_time_app/lib/pages/loading.dart

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
import 'package:flutter/material.dart';
2+
import 'package:http/http.dart';
3+
import 'dart:convert';
4+
5+
class Loading extends StatefulWidget {
6+
@override
7+
_LoadingState createState() => _LoadingState();
8+
}
9+
10+
class _LoadingState extends State<Loading> {
11+
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);
17+
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);
28+
}
29+
30+
@override
31+
void initState() {
32+
super.initState();
33+
getTime();
34+
}
235

3-
class Loading extends StatelessWidget {
436
@override
537
Widget build(BuildContext context) {
638
return Scaffold(

0 commit comments

Comments
 (0)