Skip to content

Commit

Permalink
lesson-27
Browse files Browse the repository at this point in the history
  • Loading branch information
iamshaunjp committed Aug 15, 2019
1 parent 0493d74 commit 11cf63a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion world_time_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:world_time_app/pages/loading.dart';
import 'package:world_time_app/pages/choose_location.dart';

void main() => runApp(MaterialApp(
initialRoute: '/home',
initialRoute: '/',
routes: {
'/': (context) => Loading(),
'/home': (context) => Home(),
Expand Down
11 changes: 0 additions & 11 deletions world_time_app/lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,9 @@ class Home extends StatefulWidget {

class _HomeState extends State<Home> {

void getData() async {

Response response = await get('https://jsonplaceholder.typicode.com/todos/1');
// print(response.body);
Map data = jsonDecode(response.body);
print(data);
print(data['title']);

}

@override
void initState() {
super.initState();
getData();
}

@override
Expand Down
34 changes: 33 additions & 1 deletion world_time_app/lib/pages/loading.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'dart:convert';

class Loading extends StatefulWidget {
@override
_LoadingState createState() => _LoadingState();
}

class _LoadingState extends State<Loading> {

void getTime() async {
// make the request
Response response = await get('http://worldtimeapi.org/api/timezone/Europe/London');
Map data = jsonDecode(response.body);
//print(data);

// get properties from json
String datetime = data['datetime'];
String offset = data['utc_offset'].substring(1,3);

This comment has been minimized.

Copy link
@adammartiniri

adammartiniri Mar 16, 2021

some countery have more or less diffrence with utc like 03:30 or other number do this

String offsetHour = date['utc_offset'].substring(1, 3);
    String offsetMin = date['utc_offset'].substring(4, 6);

then to now var

 now = now.add(Duration(hours: int.parse(offsetHour), minutes: int.parse(offsetMin)));
//print(datetime);
//print(offset);

// create DateTime object
DateTime now = DateTime.parse(datetime);
now = now.add(Duration(hours: int.parse(offset)));
print(now);
}

@override
void initState() {
super.initState();
getTime();
}

class Loading extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down

0 comments on commit 11cf63a

Please sign in to comment.