revert some things ig?

This commit is contained in:
Nico
2025-06-06 17:16:20 +02:00
parent d4358e38e2
commit 775b7ce0e7
3 changed files with 256 additions and 210 deletions

View File

@@ -112,11 +112,82 @@ class _AddCamMainState extends State<AddCamMain> {
return puff;
}
bool isLoadingPosition = false;
Future<Position> _initializePosition() async {
try {
final position = await GeolocatorService.deteterminePosition();
if (mounted) {
setState(() {
currentPosition = position;
isLoadingPosition = false;
// Update the text controllers with new position
rmap["DECLAT"]!["controller"]!.text = position.latitude.toString();
rmap["DECLNG"]!["controller"]!.text = position.longitude.toString();
});
}
return position;
} catch (error) {
if (!mounted) {
return currentPosition;
}
if (error is LocationDisabledException) {
SnackBarHelper.showSnackBarMessage(
context,
AppLocalizations.of(context)!.locationDisabled,
);
} else if (error is LocationForbiddenException) {
SnackBarHelper.showSnackBarMessage(
context,
AppLocalizations.of(context)!.locationForbidden,
);
}
setState(() {
isLoadingPosition = false;
});
return currentPosition;
}
}
@override
void initState() {
// updates the currentPosition var after the _determine position has finished. Means user view updates with his live location
super.initState();
isTemplate = widget.isTemplate;
// If a template is edited this fills in the existing values
if (widget.existingData?.isNotEmpty ?? false) {
for (var key in widget.existingData!.keys) {
rmap[key]!["controller"]!.text =
widget.existingData?[key].toString() ?? "";
}
} else {
// If it is not a template set default values
rmap["Datum"]!["controller"]!.text = DateTime.now().toString();
rmap["Status"]!["controller"]!.text = "aktiv";
rmap["FotoFilm"]!["controller"]!.text = "Foto";
rmap["MEZ"]!["controller"]!.text = "Sommerzeit";
rmap["Platzung"]!["controller"]!.text = "";
}
// Set initial default position
rmap["DECLAT"]!["controller"]!.text = currentPosition.latitude.toString();
rmap["DECLNG"]!["controller"]!.text = currentPosition.longitude.toString();
// Try to get current position
GeolocatorService.deteterminePosition()
.then((result) => currentPosition = result)
.then((result) {
if (mounted) {
setState(() {
currentPosition = result;
// Update coordinates after getting the position
rmap["DECLAT"]!["controller"]!.text = result.latitude.toString();
rmap["DECLNG"]!["controller"]!.text = result.longitude.toString();
});
}
})
.catchError((error) {
if (error is LocationDisabledException) {
if (mounted) {
@@ -135,28 +206,6 @@ class _AddCamMainState extends State<AddCamMain> {
}
return currentPosition;
});
// select initial werte
rmap["DECLAT"]!["controller"]!.text = currentPosition.latitude.toString();
rmap["DECLNG"]!["controller"]!.text = currentPosition.longitude.toString();
isTemplate = widget.isTemplate;
// If a template is edited this fills in the existing values
if (widget.existingData?.isNotEmpty ?? false) {
for (var key in widget.existingData!.keys) {
rmap[key]!["controller"]!.text =
widget.existingData?[key].toString() ?? "";
}
} else {
// If it is not a template set default values
rmap["Datum"]!["controller"]!.text = DateTime.now().toString();
rmap["Status"]!["controller"]!.text = "aktiv";
rmap["FotoFilm"]!["controller"]!.text = "Foto";
rmap["MEZ"]!["controller"]!.text = "Sommerzeit";
rmap["Platzung"]!["controller"]!.text = "";
}
super.initState();
}
@override
@@ -291,6 +340,24 @@ class _AddCamMainState extends State<AddCamMain> {
const SizedBox(width: 15),
ElevatedButton(
onPressed: () async {
setState(() {
isLoadingPosition = true;
});
try {
await _initializePosition();
} catch (e) {
// Error already handled in _initializePosition
if (mounted) {
setState(() {
isLoadingPosition = false;
});
return;
}
}
if (!mounted) return;
final result = await Navigator.of(context).push<LatLng>(
MaterialPageRoute(
builder: (context) {
@@ -304,7 +371,8 @@ class _AddCamMainState extends State<AddCamMain> {
},
),
);
if (result != null) {
if (result != null && mounted) {
setState(() {
currentPosition = Position(
latitude: result.latitude,
@@ -318,10 +386,33 @@ class _AddCamMainState extends State<AddCamMain> {
speed: 0.0,
speedAccuracy: 0.0,
);
rmap["DECLAT"]!["controller"]!.text = result.latitude.toString();
rmap["DECLNG"]!["controller"]!.text = result.longitude.toString();
isLoadingPosition = false;
});
} else if (mounted) {
setState(() {
isLoadingPosition = false;
});
}
},
child: Text(AppLocalizations.of(context)!.openMap),
child: isLoadingPosition
? Row(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Theme.of(context).colorScheme.onPrimary,
),
),
const SizedBox(width: 8),
Text(AppLocalizations.of(context)!.openMap),
],
)
: Text(AppLocalizations.of(context)!.openMap),
),
],
),