diff --git a/lib/components/compact_weather_data.dart b/lib/components/compact_weather_data.dart new file mode 100644 index 0000000..fc26802 --- /dev/null +++ b/lib/components/compact_weather_data.dart @@ -0,0 +1,46 @@ +import 'package:flutter/material.dart'; +import 'package:intl/intl.dart'; +import 'package:weather_icons/weather_icons.dart'; +import 'package:wetter/models/daily_weather_model.dart'; + +class CompactWeatherData extends StatefulWidget { + final DailyWeatherModel dwm; + + const CompactWeatherData(this.dwm, {Key? key}) : super(key: key); + + @override + State createState() => _CompactWeatherDataState(); +} + +class _CompactWeatherDataState extends State { + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.all(20), + child: Column( + children: [ + Text( + "${DateFormat('EEEE').format(DateTime.now().toLocal())}, ${DateTime.now().toLocal().day}.${DateTime.now().toLocal().month}.", + style: Theme.of(context).textTheme.headline4, + ), + const SizedBox(height: 20), + Row(mainAxisAlignment: MainAxisAlignment.center, children: [ + const Icon(WeatherIcons.thermometer), + Text( + "${widget.dwm.dailyMaxTemp()} °C", + style: Theme.of(context).textTheme.bodyLarge, + ) + ]), + const SizedBox(height: 20), + Row(mainAxisAlignment: MainAxisAlignment.center, children: [ + const Icon(WeatherIcons.thermometer_exterior), + Text( + "${widget.dwm.dailyMinTemp()} °C", + style: Theme.of(context).textTheme.bodyLarge, + ) + ]), + ], + ))); + } +} diff --git a/lib/main.dart b/lib/main.dart index 8829be2..2fb7c13 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:wetter/components/compact_weather_data.dart'; import 'package:wetter/models/daily_weather_model.dart'; import 'package:wetter/services/brightsky_api_service.dart'; @@ -17,7 +18,7 @@ class _MyAppState extends State { @override void initState() { super.initState(); - futureAlbum = BrightSkyAPI.fetchForecast(); + futureAlbum = BrightSkyAPI.fetchForecast(DateTime.now()); } @override @@ -26,17 +27,22 @@ class _MyAppState extends State { title: 'Fetch Data Example', theme: ThemeData( primarySwatch: Colors.blue, + textTheme: const TextTheme( + headline1: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold), + headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic), + bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'), + ), ), home: Scaffold( appBar: AppBar( - title: const Text('Fetch Data Example'), + title: const Text('Wetter heute in Harburg'), ), body: Center( child: FutureBuilder( future: futureAlbum, builder: (context, snapshot) { if (snapshot.hasData) { - return Text(snapshot.data!.dailyAverageTemp()); + return CompactWeatherData(snapshot.data!); } else if (snapshot.hasError) { return Text('${snapshot.error}'); } diff --git a/lib/models/daily_weather_model.dart b/lib/models/daily_weather_model.dart index 56d252f..1b3018d 100644 --- a/lib/models/daily_weather_model.dart +++ b/lib/models/daily_weather_model.dart @@ -1,4 +1,5 @@ import 'package:wetter/models/hourly_weather_model.dart'; +import 'dart:math'; class DailyWeatherModel { late List hourlyForecasts; @@ -16,6 +17,37 @@ class DailyWeatherModel { for (var forecast in hourlyForecasts) { tempSum = tempSum + forecast.temperature; } - return (tempSum / hourlyForecasts.length).toString(); + return (tempSum / hourlyForecasts.length).round().toString(); + } + + List tempList() { + List temps = []; + for (HourlyWeatherModel hf in hourlyForecasts) { + temps.add(hf.temperature.round()); + } + return temps; + } + + String dailyMaxTemp() { + return tempList().reduce(max).toString(); + } + + String dailyMinTemp() { + return tempList().reduce(min).toString(); + } + + String? cumulatedSunshineMinutes() { + bool dataCorrect = true; + int csm = 0; + for (var hf in hourlyForecasts) { + if (hf.sunshine == null) { + return null; + } else { + csm = csm + hf.sunshine!; + } + } + if (dataCorrect) { + return csm.toString(); + } } } diff --git a/lib/services/brightsky_api_service.dart b/lib/services/brightsky_api_service.dart index 116d682..c899efa 100644 --- a/lib/services/brightsky_api_service.dart +++ b/lib/services/brightsky_api_service.dart @@ -3,10 +3,11 @@ import 'package:http/http.dart' as http; import 'package:wetter/models/daily_weather_model.dart'; class BrightSkyAPI { - static Future fetchForecast() async { + static Future fetchForecast(DateTime d) async { + String dt = "${d.year}-${d.month}-${d.day}"; final response = await http.get( Uri.parse( - 'https://api.brightsky.dev/weather?lat=53.43&lon=20&date=2022-08-30&tz=Europe/Berlin'), + 'https://api.brightsky.dev/weather?lat=53.43&lon=10&date=$dt&tz=Europe/Berlin'), headers: {"Accept": "application/json"}); if (response.statusCode == 200) { return DailyWeatherModel.fromJson(jsonDecode(response.body)); diff --git a/pubspec.lock b/pubspec.lock index a0fdcdf..f0f9207 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -88,6 +88,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.0.1" + intl: + dependency: "direct main" + description: + name: intl + url: "https://pub.dartlang.org" + source: hosted + version: "0.17.0" lints: dependency: transitive description: @@ -184,5 +191,12 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.2" + weather_icons: + dependency: "direct main" + description: + name: weather_icons + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" sdks: dart: ">=2.17.6 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index b4b62e2..9742165 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,8 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 http: ^0.13.5 + weather_icons: ^3.0.0 + intl: ^0.17.0 dev_dependencies: flutter_test: