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:
@@ -1,10 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class AddLocationsForm extends StatefulWidget {
|
||||
final SharedPreferences prefs;
|
||||
|
||||
const AddLocationsForm(this.prefs, {Key? key}) : super(key: key);
|
||||
final Function saveNewLocation;
|
||||
final List<String> savedLocations;
|
||||
const AddLocationsForm(
|
||||
{Key? key, required this.saveNewLocation, required this.savedLocations})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
AddLocationsFormState createState() => AddLocationsFormState();
|
||||
@@ -16,12 +17,10 @@ class AddLocationsFormState extends State<AddLocationsForm> {
|
||||
String? newLocationName;
|
||||
double? newLatitude;
|
||||
double? newLongitude;
|
||||
late List<String> savedLocations;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
savedLocations = widget.prefs.getStringList('savedLocations') ?? [];
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -33,7 +32,7 @@ class AddLocationsFormState extends State<AddLocationsForm> {
|
||||
void _submitLocation() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_formKey.currentState!.save();
|
||||
_saveLocationToPrefs(newLocationName!, newLatitude!, newLongitude!);
|
||||
widget.saveNewLocation(newLocationName!, newLatitude!, newLongitude!);
|
||||
_formKey.currentState!.reset();
|
||||
newLocationName = null;
|
||||
newLatitude = null;
|
||||
@@ -41,13 +40,6 @@ class AddLocationsFormState extends State<AddLocationsForm> {
|
||||
}
|
||||
}
|
||||
|
||||
void _saveLocationToPrefs(String name, double lat, double lon) {
|
||||
savedLocations.add(name);
|
||||
widget.prefs.setStringList('savedLocations', savedLocations);
|
||||
widget.prefs.setDouble("$name-lat", lat);
|
||||
widget.prefs.setDouble("$name-lon", lon);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Form(
|
||||
@@ -65,7 +57,7 @@ class AddLocationsFormState extends State<AddLocationsForm> {
|
||||
if (value == null || value.isEmpty) {
|
||||
return "Please enter a name for the new location";
|
||||
}
|
||||
if (savedLocations.contains(value)) {
|
||||
if (widget.savedLocations.contains(value)) {
|
||||
return "Location name already in use";
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ManageSingleLocation extends StatelessWidget {
|
||||
final String name;
|
||||
final Function delete;
|
||||
const ManageSingleLocation(
|
||||
{Key? key, required this.name, required this.delete})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: Row(children: [
|
||||
Text(name, style: Theme.of(context).textTheme.headline5),
|
||||
const Spacer(),
|
||||
const Icon(Icons.delete_sharp)
|
||||
])));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:wetter/services/user_preferences.dart';
|
||||
|
||||
class SettingsForm extends StatefulWidget {
|
||||
const SettingsForm({Key? key}) : super(key: key);
|
||||
@@ -9,20 +9,13 @@ class SettingsForm extends StatefulWidget {
|
||||
}
|
||||
|
||||
class SettingsFormState extends State<SettingsForm> {
|
||||
SharedPreferences? prefs;
|
||||
int _daysToLoad = 7;
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_daysToLoad = UserPreferences.preferences!.getInt("daysToLoad") ?? 7;
|
||||
super.initState();
|
||||
initializePreferences().whenComplete(() => setState(() {
|
||||
_daysToLoad = prefs?.getInt("daysToLoad") ?? 7;
|
||||
}));
|
||||
}
|
||||
|
||||
Future<void> initializePreferences() async {
|
||||
prefs = await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -35,12 +28,13 @@ class SettingsFormState extends State<SettingsForm> {
|
||||
value: _daysToLoad.toDouble(),
|
||||
min: 1.0,
|
||||
max: 14.0,
|
||||
divisions: 14,
|
||||
divisions: 13,
|
||||
label: _daysToLoad.round().toString(),
|
||||
onChanged: (double value) {
|
||||
setState(() {
|
||||
_daysToLoad = value.round();
|
||||
prefs?.setInt("daysToLoad", _daysToLoad);
|
||||
UserPreferences.preferences!
|
||||
.setInt("daysToLoad", _daysToLoad);
|
||||
});
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user