Refactor excursion_main.dart to implement a confirmation dialog on page exit, allowing users to save templates or exit without saving. Update TrackingService to support instance reset for better state management.
This commit is contained in:
@@ -458,8 +458,59 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
),
|
||||
];
|
||||
|
||||
// Begin of widget tree
|
||||
return Scaffold(
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvoked: (didPop) async {
|
||||
if (didPop) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Show confirmation dialog
|
||||
final result = await showDialog<int>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Seite verlassen?'),
|
||||
content: const Text('Möchten Sie die Seite wirklich verlassen?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(0),
|
||||
child: const Text('Nein'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(1),
|
||||
child: const Text('Ja und Template speichern'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(2),
|
||||
child: const Text('Ja ohne Template'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (result == null || result == 0) {
|
||||
return;
|
||||
} else if (result == 1) {
|
||||
// Save as template and leave
|
||||
if (context.mounted) {
|
||||
saveTemplate(
|
||||
getFieldsText(),
|
||||
DatabasesEnum.excursion,
|
||||
);
|
||||
}
|
||||
TrackingService.resetInstance();
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
} else {
|
||||
// Just leave without saving
|
||||
TrackingService.resetInstance();
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)!.excursion),
|
||||
actions: [
|
||||
@@ -550,6 +601,7 @@ class _ExcursionMainState extends State<ExcursionMain> {
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,22 @@ import 'package:latlong2/latlong.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class TrackingService {
|
||||
static final TrackingService _instance = TrackingService._internal();
|
||||
factory TrackingService() => _instance;
|
||||
static TrackingService? _instance;
|
||||
|
||||
factory TrackingService() {
|
||||
_instance ??= TrackingService._internal();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
TrackingService._internal();
|
||||
|
||||
static void resetInstance() {
|
||||
if (_instance != null) {
|
||||
_instance!.dispose();
|
||||
_instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
List<LatLng> pathList = [];
|
||||
List<double> accuracyList = [];
|
||||
Timer? _positionTimer;
|
||||
|
||||
Reference in New Issue
Block a user