-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome_page.dart
185 lines (174 loc) · 5.74 KB
/
home_page.dart
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
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:weather/weather.dart';
import 'package:weather_app/const.dart';
// HomePage is a StatefulWidget that displays weather information.
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
// WeatherFactory is used to fetch weather data.
final WeatherFactory _wf = WeatherFactory(OPEN_WEATHER_API_KEY);
Weather? _weather; // Holds the current weather data.
@override
void initState() {
super.initState();
// Fetch the current weather for Delhi when the widget is initialized.
_wf.currentWeatherByCityName("Delhi").then((w) {
setState(() {
_weather = w;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(body: _buildUI());
}
// _buildUI constructs the UI for the HomePage.
Widget _buildUI() {
if (_weather == null) {
// Show a loading spinner if the weather data hasn't been loaded yet.
return const Center(
child: CircularProgressIndicator(),
);
}
// Once the weather data is loaded, display it.
return SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_locationHeader(),
SizedBox(height: MediaQuery.of(context).size.height * 0.08),
_dateTimeInfo(),
SizedBox(height: MediaQuery.of(context).size.height * 0.05),
_weatherIcon(),
SizedBox(height: MediaQuery.of(context).size.height * 0.02),
_currentTemp(),
SizedBox(height: MediaQuery.of(context).size.height * 0.02),
_extraInfo(),
],
),
);
}
// _locationHeader displays the name of the location.
Widget _locationHeader() {
return Text(
_weather?.areaName ?? "",
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
);
}
// _dateTimeInfo displays the current date and time.
Widget _dateTimeInfo() {
DateTime now = _weather!.date!;
return Column(
children: [
Text(
DateFormat("h:mm a").format(now),
style: const TextStyle(fontSize: 30),
),
const SizedBox(height: 10),
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(DateFormat("EEEE").format(now),
style: const TextStyle(fontWeight: FontWeight.w900)),
Text(" "),
Text(DateFormat("d.m.y").format(now),
style: const TextStyle(fontWeight: FontWeight.w900)),
],
),
],
);
}
// _weatherIcon displays the current weather icon and description.
Widget _weatherIcon() {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.40,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"http://openweathermap.org/img/wn/${_weather?.weatherIcon}@4x.png"),
),
),
),
Text(_weather?.weatherDescription ?? ""),
],
);
}
// _currentTemp displays the current temperature.
Widget _currentTemp() {
return Text("${_weather?.temperature?.celsius?.toStringAsFixed(0)}° C");
}
// _extraInfo displays additional weather information.
Widget _extraInfo() {
return Container(
height: MediaQuery.of(context).size.height * 0.15,
width: MediaQuery.of(context).size.width * 0.80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
20,
),
),
padding: const EdgeInsets.all(
8.0,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Max: ${_weather?.tempMax?.celsius?.toStringAsFixed(0)}° C",
style: const TextStyle(
fontSize: 15,
),
),
Text(
"Min: ${_weather?.tempMin?.celsius?.toStringAsFixed(0)}° C",
style: const TextStyle(
fontSize: 15,
),
),
],
),
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Wind: ${_weather?.windSpeed?.toStringAsFixed(0)}m/s",
style: const TextStyle(
fontSize: 15,
),
),
Text(
"Humidity: ${_weather?.humidity?.toStringAsFixed(0)}%",
style: const TextStyle(
fontSize: 15,
),
),
],
),
],
),
);
}
}