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)
|
||||
])));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user