diff --git a/.gitignore b/.gitignore index ae1f183..b4a256a 100644 --- a/.gitignore +++ b/.gitignore @@ -18,7 +18,7 @@ # The .vscode folder contains launch configuration and tasks you configure in # VS Code which you may wish to be included in version control, so this line # is commented out by default. -#.vscode/ +.vscode/ # Flutter/Dart/Pub related **/doc/api/ diff --git a/lib/main.dart b/lib/main.dart index 746e932..e68aece 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,45 +1,13 @@ import 'package:flutter/material.dart'; +import 'package:my_new_app/pages/choose_location.dart'; +import 'package:my_new_app/pages/home.dart'; +import 'package:my_new_app/pages/loading.dart'; void main() => runApp(MaterialApp( debugShowCheckedModeBanner: false, - home: CardId(), + routes: { + '/home': (context) =>Home(), + '/locate': (context) => LocationChooser(), + '/': (context) =>Loading(), + }, )); - - - -class CardId extends StatelessWidget { - @override - Widget build(BuildContext context) { - return Scaffold( - backgroundColor: Colors.white, - appBar: AppBar( - elevation: 0.0, - backgroundColor: Colors.grey[50], - title: Text( - 'My Profile', - style: TextStyle( - fontFamily: 'Ubuntu', - fontWeight: FontWeight.bold, - letterSpacing: 2.0, - color: Colors.amber[800]), - ), - centerTitle: true, - ), - body: Padding( - padding: EdgeInsets.all(20.0), - child: Text('main'), - ), - floatingActionButton: FloatingActionButton( - onPressed: (){}, - elevation: 10.30, - backgroundColor:Colors.yellow[50], - child: Icon( - Icons.add, - color:Colors.yellow[900], - ), - ), - ); - } -} - - diff --git a/lib/pages/choose_location.dart b/lib/pages/choose_location.dart new file mode 100644 index 0000000..9b50ebb --- /dev/null +++ b/lib/pages/choose_location.dart @@ -0,0 +1,24 @@ +import 'package:flutter/material.dart'; + +class LocationChooser extends StatefulWidget { + @override + _LocationChooserState createState() => _LocationChooserState(); +} + +class _LocationChooserState extends State { + int counter = 0; + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('Choose location'), + ), + body: RaisedButton( + onPressed: () { + setState(() { + counter += 1; + }); + }, + child: Text('count $counter'))); + } +} diff --git a/lib/pages/home.dart b/lib/pages/home.dart new file mode 100644 index 0000000..e7478dc --- /dev/null +++ b/lib/pages/home.dart @@ -0,0 +1,64 @@ +import 'package:flutter/material.dart'; + +class Home extends StatefulWidget { + @override + _HomeState createState() => _HomeState(); +} + +class _HomeState extends State { + Map data = {}; + + @override + Widget build(BuildContext context) { + data = ModalRoute.of(context) + .settings + .arguments; //receiving the data from different route. + print(data); + + return Scaffold( + //appBar: AppBar(title: Text('Home')), + body: SafeArea( + child: Padding( + padding: const EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0), + child: Center( + child: Column( + children: [ + Text( + '${data['time']}', + style: TextStyle( + fontSize: 30.5, + color: Colors.green, + fontFamily: 'ubuntu', + fontWeight: FontWeight.bold, + ), + ), + SizedBox( + height: 20.0, + ), + Text( + data['location'], + style: TextStyle( + fontSize: 25.0, + fontFamily: 'ubuntu', + ), + ), + SizedBox(height: 20.0), + RaisedButton.icon( + elevation: 0.0, + onPressed: () { + Navigator.pushNamed(context, '/locate'); + }, + icon: Icon(Icons.location_city), + label: Text('location'), + color: Colors.greenAccent[100], + splashColor: Colors.greenAccent[200], + hoverColor: Colors.red, + textColor: Colors.green[800], + ), + ], + ), + ), + ), + )); + } +} diff --git a/lib/pages/loading.dart b/lib/pages/loading.dart new file mode 100644 index 0000000..72ec496 --- /dev/null +++ b/lib/pages/loading.dart @@ -0,0 +1,40 @@ +import 'package:flutter/material.dart'; +import 'package:my_new_app/services/worldTime.dart'; +import 'package:flutter_spinkit/flutter_spinkit.dart'; + +class Loading extends StatefulWidget { + @override + _LoadingState createState() => _LoadingState(); +} + +class _LoadingState extends State { + void setTime() async { + WorldTime currentTime = + WorldTime(location: 'kolkata', url: 'Asia/Kolkata', flag: 'get.png'); + await currentTime.getTime(); + Navigator.pushReplacementNamed(context, '/home',arguments: { + 'location':currentTime.location, + 'flag':currentTime.flag, + 'time':currentTime.time + }); //sending data to different route using arguments. + } + + @override + void initState() { + super.initState(); + setTime(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + body: SafeArea( + child: Center( + child: SpinKitChasingDots( + color: Colors.green, + size: 70.0, + ), + ), + )); + } +} diff --git a/lib/services/worldTime.dart b/lib/services/worldTime.dart new file mode 100644 index 0000000..9b528a3 --- /dev/null +++ b/lib/services/worldTime.dart @@ -0,0 +1,50 @@ +import 'package:http/http.dart'; +import 'dart:convert'; +import 'package:intl/intl.dart'; +class WorldTime { + String location; //ui friendly location + String flag; //custom image + String time; //readable time + String url; //url to go the the timezone + + WorldTime({this.location, this.flag, this.url}); + + Future getTime() async { + try { + /*requesting the data*/ + + //make the request to the server end Point + Response response = + await get('http://worldtimeapi.org/api/timezone/$url'); + + /* Storing and parsing data */ + + // Parse the data + Map getdata = jsonDecode(response.body); + //storing the needed data in separate varriable + String datetime = getdata['datetime']; + //add substring to cut the '+0' + String offset = getdata['utc_offset'].substring(2, 6); + + /* manipulating data*/ + +//add substring to cut the all except 5 convert into a integer + int offsetHours = int.parse(offset.substring(0, 1)); +//add substring to cut the all except 30 and convert into a integer + int offsetMinute = int.parse(offset.substring(2, 4)); + + //creating datetime object from the string parsing + DateTime now = DateTime.parse(datetime); + + //adding offset hours and minutes to the datetime + + now = now.add(Duration(hours: offsetHours, minutes: offsetMinute)); + time = DateFormat.jm().format(now); //using 'intl' class to define date + + + } catch (e) { + print('this is a error $e'); + time = 'Could not get the Data..😢'; + } + } +} diff --git a/pubspec.lock b/pubspec.lock index fcf6763..9e345ab 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -69,11 +69,32 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_spinkit: + dependency: "direct dev" + description: + name: flutter_spinkit + url: "https://pub.dartlang.org" + source: hosted + version: "4.1.2+1" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" + http: + dependency: "direct dev" + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.0+4" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.4" image: dependency: transitive description: @@ -81,6 +102,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.4" + intl: + dependency: "direct dev" + description: + name: intl + url: "https://pub.dartlang.org" + source: hosted + version: "0.16.1" matcher: dependency: transitive description: @@ -192,4 +220,5 @@ packages: source: hosted version: "3.5.0" sdks: - dart: ">=2.4.0 <3.0.0" + dart: ">=2.5.0 <3.0.0" + flutter: ">=0.1.4 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 7772676..b2e9f03 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,6 +27,9 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter + http: ^0.12.0+4 + intl: ^0.16.1 + flutter_spinkit: "^4.1.2" # For information on the generic Dart part of this file, see the