Locations screen and add locations form
This commit is contained in:
@@ -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")
|
||||||
|
],
|
||||||
|
)))
|
||||||
|
],
|
||||||
|
)
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:wetter/screens/locations.dart';
|
||||||
import 'package:wetter/screens/settings.dart';
|
import 'package:wetter/screens/settings.dart';
|
||||||
|
|
||||||
class NavBar extends StatelessWidget {
|
class NavBar extends StatelessWidget {
|
||||||
@@ -23,6 +24,14 @@ class NavBar extends StatelessWidget {
|
|||||||
MaterialPageRoute(builder: (context) => const Settings()));
|
MaterialPageRoute(builder: (context) => const Settings()));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.public),
|
||||||
|
title: const Text("Locations"),
|
||||||
|
onTap: (() {
|
||||||
|
Navigator.push(context,
|
||||||
|
MaterialPageRoute(builder: (context) => const Locations()));
|
||||||
|
}),
|
||||||
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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!),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,6 +41,7 @@ class _MainWeatherViewState extends State<MainWeatherView>
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
super.build(context);
|
||||||
return RefreshIndicator(
|
return RefreshIndicator(
|
||||||
onRefresh: () {
|
onRefresh: () {
|
||||||
return Future(() {
|
return Future(() {
|
||||||
|
|||||||
Reference in New Issue
Block a user