Files
wetterApp/lib/components/locations/add_location_form.dart
T
2022-09-03 21:36:18 +02:00

144 lines
4.7 KiB
Dart

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);
@override
AddLocationsFormState createState() => AddLocationsFormState();
}
class AddLocationsFormState extends State<AddLocationsForm> {
final _formKey = GlobalKey<FormState>();
final locationFormController = TextEditingController();
String? newLocationName;
double? newLatitude;
double? newLongitude;
late List<String> savedLocations;
@override
void initState() {
super.initState();
savedLocations = widget.prefs.getStringList('savedLocations') ?? [];
}
@override
void dispose() {
locationFormController.dispose();
super.dispose();
}
void _submitLocation() {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
_saveLocationToPrefs(newLocationName!, newLatitude!, newLongitude!);
_formKey.currentState!.reset();
newLocationName = null;
newLatitude = null;
newLongitude = null;
}
}
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(
key: _formKey,
child: Column(children: [
Flexible(
flex: 4,
child: TextFormField(
//Location Name
onSaved: (input) {
newLocationName = input;
},
controller: locationFormController,
validator: ((value) {
if (value == null || value.isEmpty) {
return "Please enter a name for the new location";
}
if (savedLocations.contains(value)) {
return "Location name already in use";
}
return null;
}),
decoration: const InputDecoration(
border: UnderlineInputBorder(), labelText: "Location Name"),
)),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Flexible(
flex: 1,
child: TextFormField(
//Latitude
validator: ((value) {
if (value == null || value.isEmpty) {
return "Please enter a value";
}
try {
double.parse(value);
} catch (e) {
return "Please enter a number";
}
return null;
}),
onSaved: (input) {
newLatitude = double.parse(input!);
},
keyboardType: TextInputType.number,
decoration: const InputDecoration(
border: UnderlineInputBorder(), labelText: "Latitude"),
)),
const SizedBox(width: 10),
Flexible(
flex: 1,
child: TextFormField(
//Longitude
validator: ((value) {
if (value == null || value.isEmpty) {
return "Please enter a value";
}
try {
double.parse(value);
} catch (e) {
return "Please enter a number";
}
return null;
}),
onSaved: (input) {
newLongitude = double.parse(input!);
},
keyboardType: TextInputType.number,
decoration: const InputDecoration(
border: UnderlineInputBorder(), labelText: "Longitude"),
)),
const SizedBox(width: 10),
Flexible(
flex: 2,
child: ElevatedButton(
onPressed: () {
_submitLocation();
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: const [
Icon(Icons.add),
SizedBox(width: 5),
Text("Add Location")
],
)))
],
)
]));
}
}