Newer
Older
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'datamodel.dart';
import 'dart:async';
import 'package:geolocator/geolocator.dart';
class AddEvent extends StatefulWidget {
@override
State<AddEvent> createState() => _AddEventPageState();
}
class _AddEventPageState extends State<AddEvent> {
final List<bool> _isSelected = [true];
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
bool servicestatus = false;
bool haspermission = false;
late LocationPermission permission;
late Position position;
late String long = "";
late String lat = "";
late String alt = "";
late String accuracy = "";
late StreamSubscription<Position> positionStream;
LocationSettings locationSettings = const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 0,
);
getLocation() async {
position =
await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print('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();
accuracy = position.accuracy.toString();
setState(() {
//refresh UI
});
StreamSubscription<Position> positionStream = Geolocator.getPositionStream(
locationSettings: locationSettings).listen((Position position) {
print('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();
accuracy = position.accuracy.toString();
setState(() {
//refresh UI on update
});
});
}
checkGnssPermission() async {
print("CheckGnssPermission");
servicestatus = await Geolocator.isLocationServiceEnabled();
if(servicestatus){
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
print('Location permissions are denied');
}else if(permission == LocationPermission.deniedForever){
print('Location permissions are permanently denied');
}else{
haspermission = true;
}
}else{
haspermission = true;
}
if(haspermission){
print('Location permissions granted');
setState(() {
//refresh the UI
});
getLocation();
}
}else{
print("GPS Service is not enabled, turn on GPS location");
}
setState(() {
//refresh the UI
});
}
void _storeCurrentEvent() {
final EventStoreInstance storedEvents = EventStoreInstance();
final EventCurrentInstance currentEvent = EventCurrentInstance();
storedEvents.store.add(
Event(
currentEvent.store.id,
currentEvent.store.urn,
currentEvent.store.label,
currentEvent.store.type,
currentEvent.store.description,
currentEvent.store.status,
));
}
//TODO: add field validators for freetext fields. Check allowed characters in sensor.awi.de
//TODO: (Description, Label, Latitude, Longitude, Elevation, )
@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 TextEditingController _controllerLat = TextEditingController(text: lat);
final TextEditingController _controllerLong = TextEditingController(text: long);
final TextEditingController _controllerAlt = TextEditingController(text: alt);
return Scaffold(
appBar: AppBar(title: const Text("Add Event")),
body:
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
TextFormField(
initialValue: currentEvent.store.label,
decoration: const InputDecoration(
labelText: 'Label'
),
onChanged: (value) {
currentEvent.store.label = value;
},
),
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;
},
TextField(
readOnly: true,
enabled: true,
controller: _controllerLat,
decoration: InputDecoration(
labelText: 'Latitude',
border: OutlineInputBorder(),
TextField(
readOnly: true,
enabled: true,
controller: _controllerLong,
decoration: InputDecoration(
labelText: 'Longitude',
border: OutlineInputBorder(),
TextField(
readOnly: true,
enabled: true,
controller: _controllerAlt,
decoration: InputDecoration(
labelText: 'Elevation',
border: OutlineInputBorder(),
]),
bottomNavigationBar: Row(
mainAxisAlignment: MainAxisAlignment.end,
ToggleButtons(
children: <Widget>[
Icon(Icons.my_location),
],
isSelected: _isSelected,
onPressed: (int index) {
setState(() {
_isSelected[index] = !_isSelected[index];
});
},
),
SizedBox(width: 50),
FloatingActionButton(
heroTag: null,