-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_page.dart
More file actions
98 lines (85 loc) · 2.38 KB
/
Copy pathweather_page.dart
File metadata and controls
98 lines (85 loc) · 2.38 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
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
import 'package:whether_app/models/weather_model.dart';
import 'package:whether_app/services/weather_service.dart';
class Weatherpage extends StatefulWidget {
const Weatherpage({super.key});
@override
State<Weatherpage> createState() => _WeatherpageState();
}
class _WeatherpageState extends State<Weatherpage> {
//api key
final _weatherService = WeatherService('537220d4294fc53d0e80ce343710edad');
Weather? _weather;
//fetch weather
_fetchWeather() async {
//get the current city\
String cityName = await _weatherService.getCurrentCity();
//get weather for city
try {
final weather = await _weatherService.getWeather(cityName);
setState(() {
_weather = weather;
});
}
//any errors
catch (e) {
print(e);
}
}
//weather animation
String getWeatherAnimation(String? mainCondition) {
switch (mainCondition?.toLowerCase()) {
case 'clouds':
case 'mist':
case 'smoke':
case 'haze':
case 'dust':
case 'fog':
return 'assets/cloudy.json';
case 'rain':
case 'drizzle':
case 'shower rain':
return 'assets/rainy.json';
case 'thunderstorm':
return 'assets/thundering.json';
case 'clear':
return 'assets/sunny.json';
default:
return 'assets/sunny.json';
}
}
//init state
@override
void initState() {
super.initState();
//fetch weather on startup
_fetchWeather();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[900],
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//city name
Text(
_weather?.cityName ?? "loading city..",
style: const TextStyle(color: Colors.white, fontSize: 20.0),
),
//animation
Lottie.asset(getWeatherAnimation(_weather?.mainCondition)),
//temperature
Text('${_weather?.temperature.round()}°C',
style: const TextStyle(color: Colors.white, fontSize: 16.0)),
//temperature
Text(_weather?.mainCondition ?? "",
style: const TextStyle(color: Colors.white, fontSize: 18.0))
],
),
),
);
}
}