Locations screen and add locations form

This commit is contained in:
Konstantin Kollar
2022-09-03 21:36:18 +02:00
parent 399a99346b
commit b0c4afe1f3
4 changed files with 189 additions and 0 deletions
@@ -0,0 +1,143 @@
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")
],
)))
],
)
]));
}
}
+9
View File
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:wetter/screens/locations.dart';
import 'package:wetter/screens/settings.dart';
class NavBar extends StatelessWidget {
@@ -23,6 +24,14 @@ class NavBar extends StatelessWidget {
MaterialPageRoute(builder: (context) => const Settings()));
},
),
ListTile(
leading: const Icon(Icons.public),
title: const Text("Locations"),
onTap: (() {
Navigator.push(context,
MaterialPageRoute(builder: (context) => const Locations()));
}),
)
],
),
);
+36
View File
@@ -0,0 +1,36 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wetter/components/locations/add_location_form.dart';
class Locations extends StatefulWidget {
const Locations({super.key});
@override
State<Locations> createState() => _LocationsState();
}
class _LocationsState extends State<Locations> {
late final SharedPreferences? prefs;
@override
void initState() {
SharedPreferences.getInstance().then((value) => prefs = value);
super.initState();
}
Future<void> initializePreferences() async {
prefs = await SharedPreferences.getInstance();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Locations'),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: AddLocationsForm(prefs!),
));
}
}
+1
View File
@@ -41,6 +41,7 @@ class _MainWeatherViewState extends State<MainWeatherView>
@override
Widget build(BuildContext context) {
super.build(context);
return RefreshIndicator(
onRefresh: () {
return Future(() {