Show temperature of today after starting app
This commit is contained in:
@@ -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<CompactWeatherData> createState() => _CompactWeatherDataState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CompactWeatherDataState extends State<CompactWeatherData> {
|
||||||
|
@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,
|
||||||
|
)
|
||||||
|
]),
|
||||||
|
],
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
-3
@@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:wetter/components/compact_weather_data.dart';
|
||||||
import 'package:wetter/models/daily_weather_model.dart';
|
import 'package:wetter/models/daily_weather_model.dart';
|
||||||
import 'package:wetter/services/brightsky_api_service.dart';
|
import 'package:wetter/services/brightsky_api_service.dart';
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ class _MyAppState extends State<MyApp> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
futureAlbum = BrightSkyAPI.fetchForecast();
|
futureAlbum = BrightSkyAPI.fetchForecast(DateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -26,17 +27,22 @@ class _MyAppState extends State<MyApp> {
|
|||||||
title: 'Fetch Data Example',
|
title: 'Fetch Data Example',
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
primarySwatch: Colors.blue,
|
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(
|
home: Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Fetch Data Example'),
|
title: const Text('Wetter heute in Harburg'),
|
||||||
),
|
),
|
||||||
body: Center(
|
body: Center(
|
||||||
child: FutureBuilder<DailyWeatherModel>(
|
child: FutureBuilder<DailyWeatherModel>(
|
||||||
future: futureAlbum,
|
future: futureAlbum,
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.hasData) {
|
if (snapshot.hasData) {
|
||||||
return Text(snapshot.data!.dailyAverageTemp());
|
return CompactWeatherData(snapshot.data!);
|
||||||
} else if (snapshot.hasError) {
|
} else if (snapshot.hasError) {
|
||||||
return Text('${snapshot.error}');
|
return Text('${snapshot.error}');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:wetter/models/hourly_weather_model.dart';
|
import 'package:wetter/models/hourly_weather_model.dart';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
class DailyWeatherModel {
|
class DailyWeatherModel {
|
||||||
late List<HourlyWeatherModel> hourlyForecasts;
|
late List<HourlyWeatherModel> hourlyForecasts;
|
||||||
@@ -16,6 +17,37 @@ class DailyWeatherModel {
|
|||||||
for (var forecast in hourlyForecasts) {
|
for (var forecast in hourlyForecasts) {
|
||||||
tempSum = tempSum + forecast.temperature;
|
tempSum = tempSum + forecast.temperature;
|
||||||
}
|
}
|
||||||
return (tempSum / hourlyForecasts.length).toString();
|
return (tempSum / hourlyForecasts.length).round().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<int> tempList() {
|
||||||
|
List<int> 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ import 'package:http/http.dart' as http;
|
|||||||
import 'package:wetter/models/daily_weather_model.dart';
|
import 'package:wetter/models/daily_weather_model.dart';
|
||||||
|
|
||||||
class BrightSkyAPI {
|
class BrightSkyAPI {
|
||||||
static Future<DailyWeatherModel> fetchForecast() async {
|
static Future<DailyWeatherModel> fetchForecast(DateTime d) async {
|
||||||
|
String dt = "${d.year}-${d.month}-${d.day}";
|
||||||
final response = await http.get(
|
final response = await http.get(
|
||||||
Uri.parse(
|
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"});
|
headers: {"Accept": "application/json"});
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
return DailyWeatherModel.fromJson(jsonDecode(response.body));
|
return DailyWeatherModel.fromJson(jsonDecode(response.body));
|
||||||
|
|||||||
@@ -88,6 +88,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.0.1"
|
version: "4.0.1"
|
||||||
|
intl:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: intl
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.17.0"
|
||||||
lints:
|
lints:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -184,5 +191,12 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.2"
|
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:
|
sdks:
|
||||||
dart: ">=2.17.6 <3.0.0"
|
dart: ">=2.17.6 <3.0.0"
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ dependencies:
|
|||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^1.0.2
|
cupertino_icons: ^1.0.2
|
||||||
http: ^0.13.5
|
http: ^0.13.5
|
||||||
|
weather_icons: ^3.0.0
|
||||||
|
intl: ^0.17.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user