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:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
|
|
||||||
class AddLocationsForm extends StatefulWidget {
|
class AddLocationsForm extends StatefulWidget {
|
||||||
final SharedPreferences prefs;
|
final Function saveNewLocation;
|
||||||
|
final List<String> savedLocations;
|
||||||
const AddLocationsForm(this.prefs, {Key? key}) : super(key: key);
|
const AddLocationsForm(
|
||||||
|
{Key? key, required this.saveNewLocation, required this.savedLocations})
|
||||||
|
: super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
AddLocationsFormState createState() => AddLocationsFormState();
|
AddLocationsFormState createState() => AddLocationsFormState();
|
||||||
@@ -16,12 +17,10 @@ class AddLocationsFormState extends State<AddLocationsForm> {
|
|||||||
String? newLocationName;
|
String? newLocationName;
|
||||||
double? newLatitude;
|
double? newLatitude;
|
||||||
double? newLongitude;
|
double? newLongitude;
|
||||||
late List<String> savedLocations;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
savedLocations = widget.prefs.getStringList('savedLocations') ?? [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -33,7 +32,7 @@ class AddLocationsFormState extends State<AddLocationsForm> {
|
|||||||
void _submitLocation() {
|
void _submitLocation() {
|
||||||
if (_formKey.currentState!.validate()) {
|
if (_formKey.currentState!.validate()) {
|
||||||
_formKey.currentState!.save();
|
_formKey.currentState!.save();
|
||||||
_saveLocationToPrefs(newLocationName!, newLatitude!, newLongitude!);
|
widget.saveNewLocation(newLocationName!, newLatitude!, newLongitude!);
|
||||||
_formKey.currentState!.reset();
|
_formKey.currentState!.reset();
|
||||||
newLocationName = null;
|
newLocationName = null;
|
||||||
newLatitude = 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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Form(
|
return Form(
|
||||||
@@ -65,7 +57,7 @@ class AddLocationsFormState extends State<AddLocationsForm> {
|
|||||||
if (value == null || value.isEmpty) {
|
if (value == null || value.isEmpty) {
|
||||||
return "Please enter a name for the new location";
|
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 "Location name already in use";
|
||||||
}
|
}
|
||||||
return null;
|
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:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:wetter/services/user_preferences.dart';
|
||||||
|
|
||||||
class SettingsForm extends StatefulWidget {
|
class SettingsForm extends StatefulWidget {
|
||||||
const SettingsForm({Key? key}) : super(key: key);
|
const SettingsForm({Key? key}) : super(key: key);
|
||||||
@@ -9,20 +9,13 @@ class SettingsForm extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class SettingsFormState extends State<SettingsForm> {
|
class SettingsFormState extends State<SettingsForm> {
|
||||||
SharedPreferences? prefs;
|
|
||||||
int _daysToLoad = 7;
|
int _daysToLoad = 7;
|
||||||
final _formKey = GlobalKey<FormState>();
|
final _formKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
|
_daysToLoad = UserPreferences.preferences!.getInt("daysToLoad") ?? 7;
|
||||||
super.initState();
|
super.initState();
|
||||||
initializePreferences().whenComplete(() => setState(() {
|
|
||||||
_daysToLoad = prefs?.getInt("daysToLoad") ?? 7;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> initializePreferences() async {
|
|
||||||
prefs = await SharedPreferences.getInstance();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -35,12 +28,13 @@ class SettingsFormState extends State<SettingsForm> {
|
|||||||
value: _daysToLoad.toDouble(),
|
value: _daysToLoad.toDouble(),
|
||||||
min: 1.0,
|
min: 1.0,
|
||||||
max: 14.0,
|
max: 14.0,
|
||||||
divisions: 14,
|
divisions: 13,
|
||||||
label: _daysToLoad.round().toString(),
|
label: _daysToLoad.round().toString(),
|
||||||
onChanged: (double value) {
|
onChanged: (double value) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_daysToLoad = value.round();
|
_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:flutter/material.dart';
|
||||||
import 'package:wetter/components/main_menu.dart';
|
import 'package:wetter/components/main_menu.dart';
|
||||||
import 'package:wetter/screens/main_weather_view.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 {
|
class MyApp extends StatefulWidget {
|
||||||
const MyApp({super.key});
|
const MyApp({super.key});
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
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/add_location_form.dart';
|
||||||
|
import 'package:wetter/components/locations/manage_single_location.dart';
|
||||||
|
import 'package:wetter/services/user_preferences.dart';
|
||||||
|
|
||||||
class Locations extends StatefulWidget {
|
class Locations extends StatefulWidget {
|
||||||
const Locations({super.key});
|
const Locations({super.key});
|
||||||
@@ -10,18 +11,21 @@ class Locations extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _LocationsState extends State<Locations> {
|
class _LocationsState extends State<Locations> {
|
||||||
late final SharedPreferences? prefs;
|
List<String> locations = [];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
SharedPreferences.getInstance().then((value) => prefs = value);
|
locations = UserPreferences.getSavedLocations();
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> initializePreferences() async {
|
void saveNewLocation(String name, double lat, double lon) {
|
||||||
prefs = await SharedPreferences.getInstance();
|
setState(() {
|
||||||
|
locations = UserPreferences.saveNewLocation(name, lat, lon);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void deleteLocation(String name) {}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -29,8 +33,20 @@ class _LocationsState extends State<Locations> {
|
|||||||
title: const Text('Locations'),
|
title: const Text('Locations'),
|
||||||
),
|
),
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: const EdgeInsets.all(20),
|
padding: const EdgeInsets.all(20),
|
||||||
child: AddLocationsForm(prefs!),
|
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