show daily weather at different times, use weather icons, get prevalent weather icon for the day
This commit is contained in:
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
import 'package:weather_icons/weather_icons.dart';
|
import 'package:weather_icons/weather_icons.dart';
|
||||||
import 'package:wetter/models/daily_weather_model.dart';
|
import 'package:wetter/models/daily_weather_model.dart';
|
||||||
|
import 'package:wetter/models/hourly_weather_model.dart';
|
||||||
|
import 'package:wetter/services/utils.dart';
|
||||||
|
|
||||||
class CompactWeatherData extends StatefulWidget {
|
class CompactWeatherData extends StatefulWidget {
|
||||||
final DailyWeatherModel dwm;
|
final DailyWeatherModel dwm;
|
||||||
@@ -18,13 +20,13 @@ class _CompactWeatherDataState extends State<CompactWeatherData> {
|
|||||||
return Card(
|
return Card(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(20),
|
padding: const EdgeInsets.all(20),
|
||||||
child: Column(
|
child: Column(children: [
|
||||||
children: [
|
|
||||||
Text(
|
Text(
|
||||||
"${DateFormat('EEEE').format(widget.dwm.dateOfDay())}, ${widget.dwm.dateOfDay().day}.${widget.dwm.dateOfDay().month}.",
|
"${DateFormat('EEEE').format(widget.dwm.dateOfDay())}, ${widget.dwm.dateOfDay().day}.${widget.dwm.dateOfDay().month}.",
|
||||||
style: Theme.of(context).textTheme.headline4,
|
style: Theme.of(context).textTheme.headline5,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 20),
|
||||||
|
Icon(weatherIcon(widget.dwm.prevalentWeatherIcon())),
|
||||||
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
||||||
const Icon(WeatherIcons.thermometer),
|
const Icon(WeatherIcons.thermometer),
|
||||||
Text(
|
Text(
|
||||||
@@ -40,7 +42,29 @@ class _CompactWeatherDataState extends State<CompactWeatherData> {
|
|||||||
style: Theme.of(context).textTheme.bodyLarge,
|
style: Theme.of(context).textTheme.bodyLarge,
|
||||||
)
|
)
|
||||||
]),
|
]),
|
||||||
|
const SizedBox(height: 20),
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
children: [
|
||||||
|
_hourlyInfo(widget.dwm.hourlyWeatherModelAtTimeOfDay(4)),
|
||||||
|
_hourlyInfo(widget.dwm.hourlyWeatherModelAtTimeOfDay(8)),
|
||||||
|
_hourlyInfo(widget.dwm.hourlyWeatherModelAtTimeOfDay(12)),
|
||||||
|
_hourlyInfo(widget.dwm.hourlyWeatherModelAtTimeOfDay(16)),
|
||||||
|
_hourlyInfo(widget.dwm.hourlyWeatherModelAtTimeOfDay(20))
|
||||||
],
|
],
|
||||||
)));
|
),
|
||||||
|
])));
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _hourlyInfo(HourlyWeatherModel hwm) {
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
Text(DateFormat.Hm().format(hwm.timestamp)),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Icon(weatherIcon(hwm.icon ?? "not defined")),
|
||||||
|
const SizedBox(height: 14),
|
||||||
|
Text("${hwm.temperature.round()} °C")
|
||||||
|
],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
import 'package:wetter/models/hourly_weather_model.dart';
|
import 'package:wetter/models/hourly_weather_model.dart';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
|
||||||
@@ -54,4 +56,26 @@ class DailyWeatherModel {
|
|||||||
return csm.toString();
|
return csm.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HourlyWeatherModel hourlyWeatherModelAtTimeOfDay(int tod) {
|
||||||
|
return hourlyForecasts
|
||||||
|
.firstWhere((element) => element.timestamp.hour == tod);
|
||||||
|
}
|
||||||
|
|
||||||
|
String prevalentWeatherIcon() {
|
||||||
|
List<String> icons = hourlyForecasts.map((e) => e.icon!).toList();
|
||||||
|
Map<String, int> usedIconCount = {};
|
||||||
|
for (String i in icons) {
|
||||||
|
if (!usedIconCount.containsKey(i)) {
|
||||||
|
usedIconCount[i] = 1;
|
||||||
|
} else {
|
||||||
|
usedIconCount[i] = (usedIconCount[i]! + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Map<String, int> sortedValuesDesc = SplayTreeMap<String, int>.from(
|
||||||
|
usedIconCount,
|
||||||
|
(keys1, keys2) =>
|
||||||
|
usedIconCount[keys2]!.compareTo(usedIconCount[keys1]!));
|
||||||
|
return sortedValuesDesc.keys.first;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:weather_icons/weather_icons.dart';
|
||||||
|
|
||||||
|
IconData weatherIcon(String icon) {
|
||||||
|
switch (icon) {
|
||||||
|
case "clear-day":
|
||||||
|
return WeatherIcons.day_sunny;
|
||||||
|
case "clear-night":
|
||||||
|
return WeatherIcons.night_clear;
|
||||||
|
case "partly-cloudy-day":
|
||||||
|
return WeatherIcons.day_sunny_overcast;
|
||||||
|
case "partly-cloudy-night":
|
||||||
|
return WeatherIcons.night_partly_cloudy;
|
||||||
|
case "cloudy":
|
||||||
|
return WeatherIcons.cloudy;
|
||||||
|
case "fog":
|
||||||
|
return WeatherIcons.fog;
|
||||||
|
case "wind":
|
||||||
|
return WeatherIcons.day_windy;
|
||||||
|
case "rain":
|
||||||
|
return WeatherIcons.day_rain;
|
||||||
|
case "sleet":
|
||||||
|
return WeatherIcons.day_sleet;
|
||||||
|
case "snow":
|
||||||
|
return WeatherIcons.day_snow;
|
||||||
|
case "hail":
|
||||||
|
return WeatherIcons.day_hail;
|
||||||
|
case "thunderstorm":
|
||||||
|
return WeatherIcons.day_thunderstorm;
|
||||||
|
default:
|
||||||
|
return WeatherIcons.alien;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user