191 lines
4.3 KiB
Dart
191 lines
4.3 KiB
Dart
class WeatherModel {
|
|
final Location location;
|
|
final CurrentWeather current;
|
|
final Forecast forecast;
|
|
|
|
WeatherModel({
|
|
required this.location,
|
|
required this.current,
|
|
required this.forecast,
|
|
});
|
|
|
|
factory WeatherModel.fromJson(Map<String, dynamic> json) {
|
|
return WeatherModel(
|
|
location: Location.fromJson(json['location']),
|
|
current: CurrentWeather.fromJson(json['current']),
|
|
forecast: Forecast.fromJson(json['forecast']),
|
|
);
|
|
}
|
|
}
|
|
|
|
class Location {
|
|
final String name;
|
|
final String region;
|
|
final String country;
|
|
final String localtime;
|
|
|
|
Location({
|
|
required this.name,
|
|
required this.region,
|
|
required this.country,
|
|
required this.localtime,
|
|
});
|
|
|
|
factory Location.fromJson(Map<String, dynamic> json) {
|
|
return Location(
|
|
name: json['name'],
|
|
region: json['region'],
|
|
country: json['country'],
|
|
localtime: json['localtime'],
|
|
);
|
|
}
|
|
}
|
|
|
|
class CurrentWeather {
|
|
final double tempC;
|
|
final double feelslikeC;
|
|
final Condition condition;
|
|
final double windKph;
|
|
final int humidity;
|
|
final double uv;
|
|
final int isDay; // 1 = Yes, 0 = No
|
|
|
|
CurrentWeather({
|
|
required this.tempC,
|
|
required this.feelslikeC,
|
|
required this.condition,
|
|
required this.windKph,
|
|
required this.humidity,
|
|
required this.uv,
|
|
required this.isDay,
|
|
});
|
|
|
|
factory CurrentWeather.fromJson(Map<String, dynamic> json) {
|
|
return CurrentWeather(
|
|
tempC: json['temp_c'].toDouble(),
|
|
feelslikeC: json['feelslike_c'].toDouble(),
|
|
condition: Condition.fromJson(json['condition']),
|
|
windKph: json['wind_kph'].toDouble(),
|
|
humidity: json['humidity'],
|
|
uv: json['uv'].toDouble(),
|
|
isDay: json['is_day'],
|
|
);
|
|
}
|
|
}
|
|
|
|
class Condition {
|
|
final String text;
|
|
final String icon;
|
|
final int code; // 天气状况码,用于匹配壁纸
|
|
|
|
Condition({required this.text, required this.icon, required this.code});
|
|
|
|
factory Condition.fromJson(Map<String, dynamic> json) {
|
|
return Condition(
|
|
text: json['text'],
|
|
icon: "https:${json['icon']}",
|
|
code: json['code'],
|
|
);
|
|
}
|
|
}
|
|
|
|
class Forecast {
|
|
final List<ForecastDay> forecastday;
|
|
|
|
Forecast({required this.forecastday});
|
|
|
|
factory Forecast.fromJson(Map<String, dynamic> json) {
|
|
var list = json['forecastday'] as List;
|
|
List<ForecastDay> forecastDayList = list
|
|
.map((i) => ForecastDay.fromJson(i))
|
|
.toList();
|
|
return Forecast(forecastday: forecastDayList);
|
|
}
|
|
}
|
|
|
|
class ForecastDay {
|
|
final String date;
|
|
final Day day;
|
|
final Astro astro;
|
|
final List<Hour> hour;
|
|
|
|
ForecastDay({
|
|
required this.date,
|
|
required this.day,
|
|
required this.astro,
|
|
required this.hour,
|
|
});
|
|
|
|
factory ForecastDay.fromJson(Map<String, dynamic> json) {
|
|
var hourList = json['hour'] as List;
|
|
List<Hour> hours = hourList.map((i) => Hour.fromJson(i)).toList();
|
|
|
|
return ForecastDay(
|
|
date: json['date'],
|
|
day: Day.fromJson(json['day']),
|
|
astro: Astro.fromJson(json['astro']),
|
|
hour: hours,
|
|
);
|
|
}
|
|
}
|
|
|
|
class Day {
|
|
final double maxtempC;
|
|
final double mintempC;
|
|
final double avgtempC;
|
|
final Condition condition;
|
|
final int dailyChanceOfRain;
|
|
|
|
Day({
|
|
required this.maxtempC,
|
|
required this.mintempC,
|
|
required this.avgtempC,
|
|
required this.condition,
|
|
required this.dailyChanceOfRain,
|
|
});
|
|
|
|
factory Day.fromJson(Map<String, dynamic> json) {
|
|
return Day(
|
|
maxtempC: json['maxtemp_c'].toDouble(),
|
|
mintempC: json['mintemp_c'].toDouble(),
|
|
avgtempC: json['avgtemp_c'].toDouble(),
|
|
condition: Condition.fromJson(json['condition']),
|
|
dailyChanceOfRain: json['daily_chance_of_rain'],
|
|
);
|
|
}
|
|
}
|
|
|
|
class Astro {
|
|
final String sunrise;
|
|
final String sunset;
|
|
|
|
Astro({required this.sunrise, required this.sunset});
|
|
|
|
factory Astro.fromJson(Map<String, dynamic> json) {
|
|
return Astro(sunrise: json['sunrise'], sunset: json['sunset']);
|
|
}
|
|
}
|
|
|
|
class Hour {
|
|
final String time;
|
|
final double tempC;
|
|
final Condition condition;
|
|
final int chanceOfRain;
|
|
|
|
Hour({
|
|
required this.time,
|
|
required this.tempC,
|
|
required this.condition,
|
|
required this.chanceOfRain,
|
|
});
|
|
|
|
factory Hour.fromJson(Map<String, dynamic> json) {
|
|
return Hour(
|
|
time: json['time'],
|
|
tempC: json['temp_c'].toDouble(),
|
|
condition: Condition.fromJson(json['condition']),
|
|
chanceOfRain: json['chance_of_rain'],
|
|
);
|
|
}
|
|
}
|