Add locations and save them, remove locations and delete them

This commit is contained in:
Konstantin Kollar
2022-09-05 11:59:52 +02:00
parent 730634bdb2
commit e96a33f0a3
6 changed files with 75 additions and 48 deletions
+5 -1
View File
@@ -24,7 +24,11 @@ class _LocationsState extends State<Locations> {
});
}
void deleteLocation(String name) {}
void deleteLocation(String name) {
setState(() {
locations = UserPreferences.deleteLocation(name);
});
}
@override
Widget build(BuildContext context) {
+4 -12
View File
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wetter/services/user_preferences.dart';
import '../components/compact_weather_data.dart';
import '../models/daily_weather_model.dart';
@@ -17,8 +17,7 @@ class MainWeatherView extends StatefulWidget {
class _MainWeatherViewState extends State<MainWeatherView>
with AutomaticKeepAliveClientMixin<MainWeatherView> {
SharedPreferences? prefs;
int _daysToLoad = 7;
int _daysToLoad = UserPreferences.preferences!.getInt("daysToLoad") ?? 7;
late Future<List<DailyWeatherModel>> dailyWeather;
@override
@@ -27,18 +26,10 @@ class _MainWeatherViewState extends State<MainWeatherView>
@override
void initState() {
super.initState();
initializePreferences();
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
DateTime.now(), _daysToLoad, widget.lat, widget.lon);
}
Future<void> initializePreferences() async {
prefs = await SharedPreferences.getInstance();
setState(() {
_daysToLoad = prefs!.getInt("daysToLoad") ?? 7;
});
}
@override
Widget build(BuildContext context) {
super.build(context);
@@ -46,7 +37,8 @@ class _MainWeatherViewState extends State<MainWeatherView>
onRefresh: () {
return Future(() {
setState(() {
_daysToLoad = prefs?.getInt("daysToLoad") ?? 7;
_daysToLoad =
UserPreferences.preferences!.getInt("daysToLoad") ?? 7;
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
DateTime.now(), _daysToLoad, widget.lat, widget.lon);
});