Newer
Older
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:geolocator/geolocator.dart';
@override
State<AddEvent> createState() => _AddEventPageState();
}
class _AddEventPageState extends State<AddEvent> {
late LocationPermission permission;
late Position position;
late String long = "";
late String lat = "";
late String alt = "";
late StreamSubscription<Position> positionStream;
LocationSettings locationSettings = const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 0,
);
getLocation() async {
StreamSubscription<Position> positionStream = Geolocator.getPositionStream(
locationSettings: locationSettings).listen((Position position) {
debugPrint('Get Location: Lat:' + position.latitude.toString() +
' Long:' + position.longitude.toString() +
' Alt:' + position.altitude.toString());
long = position.longitude.toString();
lat = position.latitude.toString();
alt = position.altitude.toString();
setState(() {
//refresh UI on update
});}
debugPrint("Check Location Permission");
bool serviceStatus = false;
bool hasPermission = false;
serviceStatus = await Geolocator.isLocationServiceEnabled();
if(serviceStatus){
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
}else if(permission == LocationPermission.deniedForever){
debugPrint('Location permissions are permanently denied');
if(hasPermission){
debugPrint('Location permissions granted');
if(mounted){
setState(() {
//refresh the UI
});};
debugPrint("GPS Service is not enabled, turn on GPS location");
setState(() {
//refresh the UI
});}
void _storeCurrentEvent() {
final EventStoreInstance storedEvents = EventStoreInstance();
final EventCurrentInstance currentEvent = EventCurrentInstance();
currentEvent.store.status = "PENDING";
storedEvents.store.add(
Event(
currentEvent.store.id,
currentEvent.store.urn,
currentEvent.store.label,
currentEvent.store.type,
currentEvent.store.description,
currentEvent.store.status,
currentEvent.store.latitude,
currentEvent.store.longitude,
currentEvent.store.elevation
//TODO: add field validators for freetext fields. Check allowed characters in sensor.awi.de
@override
Widget build(BuildContext context) {
/* Get singletons to access relevant data here.*/
final EventTypeStoreInstance eventTypes = EventTypeStoreInstance();
final DeviceStoreInstance availableDevice = DeviceStoreInstance();
final EventStoreInstance storedEvents = EventStoreInstance();
final EventCurrentInstance currentEvent = EventCurrentInstance();
final formKey = GlobalKey<FormState>(); //key for form
// Update current event coordinates from GNSS stream
currentEvent.store.latitude = lat;
currentEvent.store.longitude = long;
currentEvent.store.elevation = alt;
if(accuracy == 0.0){
gnssStatusText = "No-Fix";
else{
gnssStatusText = "Precision: "+ accuracy.toStringAsFixed(2) +"m";
}
gnssStatusText = "GNSS Disabled"; // Just display existing event coordinates
return Scaffold(
appBar: AppBar(title: const Text("Add Event")),
body:
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
TextFormField(
initialValue: currentEvent.store.label,
autovalidateMode: AutovalidateMode.onUserInteraction,
decoration: const InputDecoration(
labelText: 'Label',
errorText: 'Only: a-z , A-Z , _ , 0-9 , ,(Comma) , ( , ) , + , - , . , :'
onChanged: (value) {
currentEvent.store.label = value;
},
validator: (value){
if(!RegExp(r'^[a-z A-Z . \- 0-9 , ( ) + - _ :]+$').hasMatch(value!)){
return "Only: a-z , A-Z , _ , 0-9 , ,(Comma) , ( , ) , + , - , . , :";
}else{
currentEvent.store.label = value;
return ''; // Entered Text is valid
}
},
),
DropdownButtonFormField(
decoration: const InputDecoration(
labelText: 'Event Type',
),
eventTypes.store.map((EventType event) {
return DropdownMenuItem(
value: event.name,
child: Text(event.name),
);
}).toList(),
currentEvent.store.type = value.toString();
}
),
DropdownButtonFormField(
decoration: const InputDecoration(
labelText: 'URN',
),
items:
availableDevice.store.map((Device device) {
return DropdownMenuItem(
value: device.urn,
child: Text(device.urn),
);
}).toList(),
currentEvent.store.urn = value.toString();
currentEvent.store.id = availableDevice.getDeviceIdFromUrn(value.toString());
}
),
TextFormField(
initialValue: currentEvent.store.description,
decoration: const InputDecoration(
labelText: 'Description'
),
onChanged: (value) {
currentEvent.store.description = value;
},
TextFormField(
readOnly: false,
enabled: !syncGNSSData,
controller: TextEditingController(text: currentEvent.store.latitude.toString()),
decoration: const InputDecoration(
labelText: 'Latitude',
border: OutlineInputBorder(),
),
onChanged: (value) {
currentEvent.store.latitude = value;
}
TextFormField(
readOnly: false,
enabled: !syncGNSSData,
controller: TextEditingController(text: currentEvent.store.longitude.toString()),
decoration: const InputDecoration(
labelText: 'Longitude',
border: OutlineInputBorder(),
),
onChanged: (value) {
currentEvent.store.longitude = value;
}
TextFormField(
readOnly: false,
enabled: !syncGNSSData,
controller: TextEditingController(text: currentEvent.store.elevation.toString()),
decoration: const InputDecoration(
labelText: 'Elevation',
border: OutlineInputBorder(),
),
onChanged: (value) {
currentEvent.store.elevation = value;
}
]),
bottomNavigationBar: Row(
mainAxisAlignment: MainAxisAlignment.end,
Text(gnssStatusText),
const SizedBox(width: 50),
syncGNSSData = _isGNSSSelected[index];
//TODO: cancel position stream if GNSS sync disabled here!
FloatingActionButton(
heroTag: null,
onPressed: () {
_storeCurrentEvent();
HapticFeedback.vibrate();
},
],
),
);
}
}