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 { const AddEvent({Key? key}) : super(key: key); @override State<AddEvent> createState() => _AddEventPageState(); } class _AddEventPageState extends State<AddEvent> { bool disableGNSSEditing = true; final List<bool> _isGNSSSelected = [true]; late LocationPermission permission; late Position position; late String long = ""; late String lat = ""; late String alt = ""; late double accuracy = 0.0; 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(); accuracy = position.accuracy; if (disableGNSSEditing == true) { if(mounted){ setState(() { //refresh UI on update });} }; }); } startGNSS() async { 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) { debugPrint('Location permissions are denied'); }else if(permission == LocationPermission.deniedForever){ debugPrint('Location permissions are permanently denied'); }else{ hasPermission = true; } }else{ hasPermission = true; } if(hasPermission){ debugPrint('Location permissions granted'); if(mounted){ setState(() { //refresh the UI });}; getLocation(); } }else{ debugPrint("GPS Service is not enabled, turn on GPS location"); } if(mounted){ setState(() { //refresh the UI });} } @override void initState() { startGNSS(); super.initState(); } 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 @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(); String gnssStatusText = ""; if (true == disableGNSSEditing){ // 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"; } } else{ 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, decoration: const InputDecoration( labelText: 'Label' ), onChanged: (value) { currentEvent.store.label = value; }, ), DropdownButtonFormField( value: currentEvent.store.type, decoration: const InputDecoration( labelText: 'Event Type', ), items: eventTypes.store.map((EventType event) { return DropdownMenuItem( value: event.name, child: Text(event.name), ); }).toList(), onChanged: (value) { currentEvent.store.type = value.toString(); } ), DropdownButtonFormField( value: currentEvent.store.urn, decoration: const InputDecoration( labelText: 'URN', ), items: availableDevice.store.map((Device device) { return DropdownMenuItem( value: device.urn, child: Text(device.urn), ); }).toList(), onChanged: (value) { 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: false, enabled: !disableGNSSEditing, //controller: _controllerLat, controller: TextEditingController(text: currentEvent.store.latitude.toString()), decoration: const InputDecoration( labelText: 'Latitude', border: OutlineInputBorder(), ), ), TextField( readOnly: false, enabled: !disableGNSSEditing, controller: TextEditingController(text: currentEvent.store.longitude.toString()), decoration: const InputDecoration( labelText: 'Longitude', border: OutlineInputBorder(), ), ), TextField( readOnly: false, enabled: !disableGNSSEditing, controller: TextEditingController(text: currentEvent.store.elevation.toString()), decoration: const InputDecoration( labelText: 'Elevation', border: OutlineInputBorder(), ), ), ]), bottomNavigationBar: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ Text(gnssStatusText), const SizedBox(width: 50), ToggleButtons( children: const <Widget>[ Icon(Icons.my_location), ], isSelected: _isGNSSSelected, onPressed: (int index) { setState(() { _isGNSSSelected[index] = !_isGNSSSelected[index]; disableGNSSEditing = _isGNSSSelected[index]; }); }, ), const SizedBox(width: 50), FloatingActionButton( heroTag: null, onPressed: _storeCurrentEvent, tooltip: 'Add Event', child: const Icon(Icons.add), ), const SizedBox(width: 20), ], ), ); } }