-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwheather_screen.dart
More file actions
214 lines (199 loc) · 6.95 KB
/
Copy pathwheather_screen.dart
File metadata and controls
214 lines (199 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import 'dart:convert';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:http/http.dart' as http;
import 'package:wheather_app/secrets.dart';
import 'additional_info_item.dart';
import 'hourly_forecast_item.dart';
class WeatherScreen extends StatefulWidget {
const WeatherScreen({super.key});
@override
State<WeatherScreen> createState() => _WeatherScreenState();
}
class _WeatherScreenState extends State<WeatherScreen> {
late Future<Map<String, dynamic>> weather;
Future<Map<String, dynamic>> getCurrentWeather() async {
try {
String cityName = 'London';
final res = await http.get(
Uri.parse(
'https://api.openweathermap.org/data/2.5/forecast?q=$cityName&APPID=$openWeatherApiKey',
),
);
final data = jsonDecode(res.body);
if (data['cod'] != '200') {
throw 'An unexpected error occurred';
}
return data;
} catch (e) {
throw e.toString();
}
}
@override
void initState() {
super.initState();
weather = getCurrentWeather();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
'Weather App',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
centerTitle: true,
actions: [
IconButton(
onPressed: () {
setState(() {
weather = getCurrentWeather();
});
},
icon: const Icon(Icons.refresh),
),
],
),
body: FutureBuilder(
future: weather,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator.adaptive(),
);
}
if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
);
}
final data = snapshot.data!;
final currentWeatherData = data['list'][0];
final currentTemp = currentWeatherData['main']['temp'];
final currentSky = currentWeatherData['weather'][0]['main'];
final currentPressure = currentWeatherData['main']['pressure'];
final currentWindSpeed = currentWeatherData['wind']['speed'];
final currentHumidity = currentWeatherData['main']['humidity'];
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// main card
SizedBox(
width: double.infinity,
child: Card(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: 10,
sigmaY: 10,
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text(
'$currentTemp K',
style: const TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
Icon(
currentSky == 'Clouds' || currentSky == 'Rain'
? Icons.cloud
: Icons.sunny,
size: 64,
),
const SizedBox(height: 16),
Text(
currentSky,
style: const TextStyle(
fontSize: 20,
),
),
],
),
),
),
),
),
),
const SizedBox(height: 20),
const Text(
'Hourly Forecast',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
SizedBox(
height: 120,
child: ListView.builder(
itemCount: 5,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
final hourlyForecast = data['list'][index + 1];
final hourlySky =
data['list'][index + 1]['weather'][0]['main'];
final hourlyTemp =
hourlyForecast['main']['temp'].toString();
final time = DateTime.parse(hourlyForecast['dt_txt']);
return HourlyForecastItem(
time: DateFormat.j().format(time),
temp: hourlyTemp,
icon: hourlySky == 'Clouds' || hourlySky == 'Rain'
? Icons.cloud
: Icons.sunny,
);
},
),
),
const SizedBox(height: 20),
const Text(
'Additional Information',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
AdditionalInfoItem(
icon: Icons.water_drop,
label: 'Humidity',
value: currentHumidity.toString(),
),
AdditionalInfoItem(
icon: Icons.air,
label: 'Wind Speed',
value: currentWindSpeed.toString(),
),
AdditionalInfoItem(
icon: Icons.beach_access,
label: 'Pressure',
value: currentPressure.toString(),
),
],
),
],
),
);
},
),
);
}
}