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: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<MyApp> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
futureAlbum = BrightSkyAPI.fetchForecast();
|
||||
futureAlbum = BrightSkyAPI.fetchForecast(DateTime.now());
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -26,17 +27,22 @@ class _MyAppState extends State<MyApp> {
|
||||
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<DailyWeatherModel>(
|
||||
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}');
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:wetter/models/hourly_weather_model.dart';
|
||||
import 'dart:math';
|
||||
|
||||
class DailyWeatherModel {
|
||||
late List<HourlyWeatherModel> 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<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';
|
||||
|
||||
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(
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user