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);
|
||||
});
|
||||
},
|
||||
)
|
||||
|
||||
+6
-1
@@ -1,8 +1,13 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wetter/components/main_menu.dart';
|
||||
import 'package:wetter/screens/main_weather_view.dart';
|
||||
import 'package:wetter/services/user_preferences.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await UserPreferences.init();
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
],
|
||||
]))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class UserPreferences {
|
||||
static List<String> _savedLocations = [];
|
||||
static SharedPreferences? preferences;
|
||||
static Future<void> init() async {
|
||||
preferences = await SharedPreferences.getInstance();
|
||||
_savedLocations = preferences!.getStringList('savedLocations') ?? [];
|
||||
}
|
||||
|
||||
static List<String> getSavedLocations() {
|
||||
return _savedLocations;
|
||||
}
|
||||
|
||||
static List<String> saveNewLocation(String name, double lat, double lon) {
|
||||
_savedLocations.add(name);
|
||||
preferences!.setStringList('savedLocations', _savedLocations);
|
||||
preferences!.setDouble("$name-lat", lat);
|
||||
preferences!.setDouble("$name-lon", lon);
|
||||
return getSavedLocations();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user