Load user preferences at start of app. Manage locations: user is able to add locations by lat and lon to shared preferences

This commit is contained in:
Konstantin Kollar
2022-09-04 22:04:15 +02:00
parent b0c4afe1f3
commit 730634bdb2
6 changed files with 86 additions and 36 deletions
+25 -9
View File
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wetter/components/locations/add_location_form.dart';
import 'package:wetter/components/locations/manage_single_location.dart';
import 'package:wetter/services/user_preferences.dart';
class Locations extends StatefulWidget {
const Locations({super.key});
@@ -10,18 +11,21 @@ class Locations extends StatefulWidget {
}
class _LocationsState extends State<Locations> {
late final SharedPreferences? prefs;
List<String> locations = [];
@override
void initState() {
SharedPreferences.getInstance().then((value) => prefs = value);
locations = UserPreferences.getSavedLocations();
super.initState();
}
Future<void> initializePreferences() async {
prefs = await SharedPreferences.getInstance();
void saveNewLocation(String name, double lat, double lon) {
setState(() {
locations = UserPreferences.saveNewLocation(name, lat, lon);
});
}
void deleteLocation(String name) {}
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -29,8 +33,20 @@ class _LocationsState extends State<Locations> {
title: const Text('Locations'),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: AddLocationsForm(prefs!),
));
padding: const EdgeInsets.all(20),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
for (String name in locations)
ManageSingleLocation(name: name, delete: deleteLocation),
...[
Expanded(
child: AddLocationsForm(
saveNewLocation: saveNewLocation,
savedLocations: locations),
)
],
]))));
}
}