main.dart:
import 'package:flutter/material.dart';
import 'package:icardi/pages/choose_location.dart';
import 'package:icardi/pages/home.dart';
import 'package:icardi/pages/loading.dart';
void main() =>runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => Loading(),
'/home': (context) => Home(),
'/location': (context) => ChooseLocation()
},
);
}
}
home.dart:
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
Map data = {};
@override
Widget build(BuildContext context) {
data = data.isEmpty ? data :ModalRoute.of(context)?.settings.arguments as Map;
print(data);
String bgImage = data['dayTime'] ? 'day.jpg' : 'night.jpg';
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage('assets/$bgImage') ,fit: BoxFit.cover),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(40, 40, 40, 0),
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
FlatButton.icon(
onPressed: () async {
dynamic result= await Navigator.pushNamed(context, '/location');
setState(() {
data = {
'time': result['time'],
'location': result['location'],
'dayTime': result['dayTime'],
'flag': result['flag'],
};
});
},
icon: Icon(
Icons.edit_location,
color: Colors.grey[300],),
label: Text('edit location',
style: TextStyle( color: Colors.grey[300]),),
),
SizedBox(height: 40),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(
data['location'],
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
letterSpacing: 2, color: Colors.white),
),
],
),
SizedBox(height: 40),
Text(
data ['time'], style: TextStyle(
fontSize: 66, color: Colors.white
),
),
],
)),
),
),
);
}
}
loading.dart:
import 'package:flutter/material.dart';
import 'package:icardi/services/world_time.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
class Loading extends StatefulWidget {
const Loading({Key? key}) : super(key: key);
@override
State<Loading> createState() => _LoadingState();
}
class _LoadingState extends State<Loading> {
setupWorldTime() async {
WordTime instance = WordTime(flag: 'uganda.png', location: 'Kampala', url: 'Africa/Kampala');
await instance.getTime();
Navigator.pushReplacementNamed(context, '/home', arguments: {
'location': instance.location, 'flag': instance.flag, 'time': instance.time,
'dayTime': instance.dayTime
});
setState(() {
var time = instance.time;
});
}
@override
void initState() {
super.initState();
setupWorldTime();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[900],
body: Center(
child: SpinKitFadingCircle(
color: Colors.white,
size: 50,
),
),
);
}
}
worldtime.dart:
import 'package:http/http.dart'as http;
import 'package:http/http.dart';
import 'dart:convert';
import 'package:intl/intl.dart';
class WordTime{
String location;
late String time;
String flag;
String url;
late bool dayTime;
WordTime({ required this.flag , required this.location, required this.url});
Future<void> getTime () async{
try {
Response response= await http.get(Uri.parse('http://worldtimeapi.org/api/timezone/$url'));
Map data = jsonDecode(response.body);
print (data);
String datetime = data['datetime'];
String offset = data['utc_offset'].substring(1,3);
DateTime now = DateTime.parse(datetime);
now= now.add(Duration(hours: int.parse(offset)));
time = DateFormat.jm().format(now);
dayTime = now.hour >6 && now.hour <20 ? true : false;
}
catch (e){
print(' the error is $e');
time ='could not get time data';
}
}
}
choose_location.dart:
import 'package:flutter/material.dart';
import 'package:icardi/services/world_time.dart';
class ChooseLocation extends StatefulWidget {
@override
State<ChooseLocation> createState() => _ChooseLocationState();
}
class _ChooseLocationState extends State<ChooseLocation> {
List<WordTime> locations =[
WordTime(url:'Europe/London', location: 'London', flag: 'uk.png'),
WordTime(url:'Europe/Berlin', location: 'Berlin', flag: 'germany.png'),
WordTime(url:'Africa/Cairo', location: 'Cairo', flag: 'egypt.png'),
WordTime(url:'Africa/Nairobi', location: 'Nairobi', flag: 'kenya.png'),
WordTime(url:'Africa/Kampala', location: 'Kampala', flag: 'ug.png'),
WordTime(url:'America/Chicago', location: 'Chicago', flag: 'usa.png'),
WordTime(url:'Asia/Jakarta', location: 'Jakarta', flag: 'indoo.png'),
WordTime(url:'Asia/Seoul', location: 'Seoul', flag: 'korea.jpg'),
WordTime(url:'Europe/Athens', location: 'Athens', flag: 'greece.png'),
];
void updateTime(int index) async{
WordTime instance = locations[index];
await instance.getTime();
// ignore: use_build_context_synchronously
Navigator.pop(context, {'location': instance.location, 'flag': instance.flag, 'time': instance.time,
'dayTime': instance.dayTime});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text('Locations'),
backgroundColor: Colors.blue[900],
elevation: 0,
centerTitle: true,
),
body: ListView.builder(
itemCount: locations.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 1, horizontal: 6),
child: Card(
child: ListTile(
onTap: (){
updateTime(index);
},
title: Text(locations[index].location),
leading: CircleAvatar(
backgroundImage: AssetImage('assets/${locations[index].flag}'),
),
),
),
);
},
),
);
}
}
the error i get is: Performing hot restart... Syncing files to device TECNO K7... Restarted application in 4,790ms. D/libc-netbsd( 7099): getaddrinfo: worldtimeapi.org get result from proxy gai_error = 0 I/flutter ( 7099): {abbreviation: EAT, client_ip: 197.239.7.195, datetime: 2022-09-14T12:27:41.758821+03:00, day_of_week: 3, day_of_year: 257, dst: false, dst_from: null, dst_offset: 0, dst_until: null, raw_offset: 10800, timezone: Africa/Kampala, unixtime: 1663147661, utc_datetime: 2022-09-14T09:27:41.758821+00:00, utc_offset: +03:00, week_number: 37} I/flutter ( 7099): {}
======== Exception caught by widgets library ======================================================= The following _TypeError was thrown building Home(dirty, state: _HomeState#f907f): type 'Null' is not a subtype of type 'bool'