add edit and delete
This commit is contained in:
@@ -40,6 +40,9 @@ class TankController extends GetxController {
|
||||
|
||||
final FocusNode firstFocusNode = FocusNode(); // Deklariere den FocusNode
|
||||
|
||||
final isEditMode = false.obs;
|
||||
String documentId = '';
|
||||
|
||||
// Methode für das ausgewählte Datum
|
||||
Future<void> selectDateTime(BuildContext context) async {
|
||||
// 1. Datum auswählen
|
||||
@@ -150,8 +153,38 @@ class TankController extends GetxController {
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
// Rufe den Standort direkt beim Initialisieren des Controllers ab, falls gewünscht.
|
||||
fetchCurrentLocation();
|
||||
handleInputOrUpdate();
|
||||
}
|
||||
|
||||
void handleInputOrUpdate() {
|
||||
var mapFromArguments = Get.arguments as Map<String, dynamic>?;
|
||||
if (mapFromArguments != null && mapFromArguments['from'] != null) {
|
||||
isEditMode.value = mapFromArguments['from'] == 'Input' ? true : false;
|
||||
}
|
||||
//bei true ein insert bei false ein update
|
||||
if (isEditMode.value) {
|
||||
fetchCurrentLocation();
|
||||
} else {
|
||||
// Im Editiermodus, keine Standortabfrage durchführen
|
||||
print('Im Editiermodus, keine Standortabfrage durchführen');
|
||||
// Den Fokus auf das erste Eingabefeld setzen
|
||||
FocusScope.of(Get.context!).requestFocus(firstFocusNode);
|
||||
print('TankController is in Edit Mode');
|
||||
if (mapFromArguments != null && mapFromArguments['data'] != null) {
|
||||
var dataMap = mapFromArguments['data'] as Map<String, dynamic>;
|
||||
print('Data Map for Edit Mode: $dataMap');
|
||||
// Setze die documentId für spätere Updates
|
||||
documentId = dataMap['\$id'] ?? '';
|
||||
// Setze die Werte in die TextEditingController
|
||||
dateController.text = dataMap['date'] ?? '';
|
||||
kilometerStandEdittingController.text = dataMap['odometer'] ?? '';
|
||||
mengeController.text = dataMap['liters'] ?? '';
|
||||
pricePerLiterController.text = dataMap['pricePerLiter'] ?? '';
|
||||
ortController.text = dataMap['location'] ?? '';
|
||||
// Berechne den Gesamtpreis
|
||||
updateSumPrice();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -213,33 +246,64 @@ class TankController extends GetxController {
|
||||
print('Formular ist gültig');
|
||||
formKeyTank.currentState!.save();
|
||||
try {
|
||||
//creatiung a tank stop
|
||||
await _authRepository.createTankStop({
|
||||
'userId': _dataBox.read('userId'),
|
||||
'date': f.format(DateTime.parse(dateController.text)),
|
||||
'odometer': kilometerStandEdittingController.text,
|
||||
'liters': mengeController.text,
|
||||
'pricePerLiter': pricePerLiterController.text,
|
||||
'location': ortController.text,
|
||||
}).then((models.Document document) {
|
||||
print('Tankstopp erfolgreich gespeichert: ${document.data}');
|
||||
messageToUser = 'Tankstopp erfolgreich gespeichert!';
|
||||
isErrorBySaving = false;
|
||||
})
|
||||
// Handle specific Appwrite exceptions
|
||||
.catchError((error) {
|
||||
isErrorBySaving = true;
|
||||
if (error is AppwriteException) {
|
||||
// Handle specific Appwrite exceptions
|
||||
messageToUser = 'Appwrite Fehler: ${error.message}';
|
||||
print('Appwrite Fehler: ${error.message}');
|
||||
} else {
|
||||
// Handle other types of errors
|
||||
messageToUser = 'Allgemeiner Fehler: $error';
|
||||
print('Allgemeiner Fehler: $error');
|
||||
}
|
||||
print('Fehler bei der speicherung: $error');
|
||||
});
|
||||
//false update a tank stop
|
||||
if (isEditMode.value == false) {
|
||||
await _authRepository.updateTankStop(documentId, {
|
||||
'userId': _dataBox.read('userId'),
|
||||
'date': f.format(DateTime.parse(dateController.text)),
|
||||
'odometer': kilometerStandEdittingController.text,
|
||||
'liters': mengeController.text,
|
||||
'pricePerLiter': pricePerLiterController.text,
|
||||
'location': ortController.text,
|
||||
}).then((models.Document document) {
|
||||
print('Tankstopp erfolgreich aktualisiert: ${document.data}');
|
||||
messageToUser = 'Tankstopp erfolgreich aktualisiert!';
|
||||
isErrorBySaving = false;
|
||||
})
|
||||
// Handle specific Appwrite exceptions
|
||||
.catchError((error) {
|
||||
isErrorBySaving = true;
|
||||
if (error is AppwriteException) {
|
||||
// Handle specific Appwrite exceptions
|
||||
messageToUser = 'Appwrite Fehler: ${error.message}';
|
||||
print('Appwrite Fehler: ${error.message}');
|
||||
} else {
|
||||
// Handle other types of errors
|
||||
messageToUser = 'Allgemeiner Fehler: $error';
|
||||
print('Allgemeiner Fehler: $error');
|
||||
}
|
||||
print('Fehler bei der speicherung: $error');
|
||||
});
|
||||
} else {
|
||||
//true creating a tank stop
|
||||
await _authRepository.createTankStop({
|
||||
'userId': _dataBox.read('userId'),
|
||||
'date': f.format(DateTime.parse(dateController.text)),
|
||||
'odometer': kilometerStandEdittingController.text,
|
||||
'liters': mengeController.text,
|
||||
'pricePerLiter': pricePerLiterController.text,
|
||||
'location': ortController.text,
|
||||
}).then((models.Document document) {
|
||||
print('Tankstopp erfolgreich gespeichert: ${document.data}');
|
||||
messageToUser = 'Tankstopp erfolgreich gespeichert!';
|
||||
isErrorBySaving = false;
|
||||
})
|
||||
// Handle specific Appwrite exceptions
|
||||
.catchError((error) {
|
||||
isErrorBySaving = true;
|
||||
if (error is AppwriteException) {
|
||||
// Handle specific Appwrite exceptions
|
||||
messageToUser = 'Appwrite Fehler: ${error.message}';
|
||||
print('Appwrite Fehler: ${error.message}');
|
||||
} else {
|
||||
// Handle other types of errors
|
||||
messageToUser = 'Allgemeiner Fehler: $error';
|
||||
print('Allgemeiner Fehler: $error');
|
||||
}
|
||||
print('Fehler bei der speicherung: $error');
|
||||
});
|
||||
// bei true ein insert
|
||||
}
|
||||
}
|
||||
// Handle any other exceptions that might occur
|
||||
catch (e) {
|
||||
@@ -259,6 +323,7 @@ class TankController extends GetxController {
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
duration: const Duration(seconds: 4),
|
||||
);
|
||||
goToTankStopsView();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ class TankPage extends GetView<TankController> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var tankCtrl = controller;
|
||||
return SafeArea(
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
@@ -126,23 +127,30 @@ class TankPage extends GetView<TankController> {
|
||||
SizedBox(
|
||||
width: 350,
|
||||
height: 50,
|
||||
child: ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.blue.withValues(
|
||||
alpha: 0.9,
|
||||
), // Button-Farbe
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(
|
||||
10,
|
||||
), // Abgerundete Ecken
|
||||
child: Obx(
|
||||
() => ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.blue.withValues(
|
||||
alpha: 0.9,
|
||||
), // Button-Farbe
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(
|
||||
10,
|
||||
), // Abgerundete Ecken
|
||||
),
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
controller.saveTankstopp();
|
||||
},
|
||||
child: Text(
|
||||
'Tankstop erfassen',
|
||||
style: TextStyle(color: Colors.white, fontSize: 20),
|
||||
onPressed: () {
|
||||
controller.saveTankstopp();
|
||||
},
|
||||
child: tankCtrl.isEditMode.value == true ? Text(
|
||||
'Tankstop erfassen',
|
||||
style:
|
||||
TextStyle(color: Colors.white, fontSize: 20),
|
||||
): Text(
|
||||
'Tankstop updaten',
|
||||
style: TextStyle(
|
||||
color: Colors.white, fontSize: 20),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user