final with gas Stations and navigation to then
This commit is contained in:
86
lib/pages/gaslist/gaslist_controller.dart
Normal file
86
lib/pages/gaslist/gaslist_controller.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:get_storage/get_storage.dart';
|
||||
import '../../data/models/gas_model.dart';
|
||||
import '../../data/repository/appwrite_repository.dart';
|
||||
import '../../pages/login/login_view.dart';
|
||||
import '../../pages/tanklist/tanklist_view.dart';
|
||||
|
||||
import './widgets/map_view.dart' show MapDialogView;
|
||||
import '../../data/repository/gasstation_repository.dart';
|
||||
|
||||
class GaslistController extends GetxController {
|
||||
final _dataBox = GetStorage('MyUserStorage');
|
||||
final szRxGasArt = 'DIE'.obs;
|
||||
//Gas Station Repository
|
||||
final GasStationRepository _gasStationRepository = GasStationRepository();
|
||||
final AppwriteRepository _authRepository = AppwriteRepository();
|
||||
final isLoadingList = false.obs;
|
||||
var gasStationsList = <GasModel>[].obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
loadListData();
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
@override
|
||||
void onReady() {}
|
||||
|
||||
@override
|
||||
void onClose() {}
|
||||
|
||||
Future<void> loadListData() async {
|
||||
isLoadingList(true);
|
||||
var lat = _dataBox.read('lastLatitude');
|
||||
var lng = _dataBox.read('lastLongitude');
|
||||
var gas = szRxGasArt.value;
|
||||
var result =
|
||||
await getGasStationsFromApi({'lat': lat, 'lng': lng, 'gas': gas});
|
||||
print('Gas Stations from API: $result');
|
||||
//Hier die Logik zum Laden der Daten einfügen
|
||||
gasStationsList.clear();
|
||||
gasStationsList.refresh();
|
||||
// add Map to GasModelList
|
||||
for (var element in result) {
|
||||
Map<String, dynamic> gasModelMap = (element as Map<String, dynamic>);
|
||||
var gasModelItem = GasModel.fromJson(gasModelMap);
|
||||
gasStationsList.add(gasModelItem);
|
||||
}
|
||||
//Simulate a delay for loading data
|
||||
|
||||
isLoadingList(false);
|
||||
update();
|
||||
}
|
||||
|
||||
Future<dynamic> getGasStationsFromApi(Map map) async {
|
||||
var result = await _gasStationRepository.getGasStationsLocations(map);
|
||||
return result;
|
||||
}
|
||||
|
||||
void goToListView() {
|
||||
Get.offAndToNamed(TanklistPage.namedRoute);
|
||||
}
|
||||
|
||||
Future<void> logoutSessionAndGoToLoginPage() async {
|
||||
// Handle logout logic here
|
||||
print('Logout session and go to login page');
|
||||
// Clear GetStorage session ID
|
||||
_dataBox.remove('sessionId');
|
||||
_dataBox.remove('userId');
|
||||
_dataBox.remove('userName');
|
||||
_dataBox.remove('userEmail');
|
||||
print('Session ID removed from GetStorage');
|
||||
await _authRepository.logout();
|
||||
Get.offAndToNamed(LoginPage.namedRoute);
|
||||
}
|
||||
|
||||
Future<void> openDirectionMaps(double lat, double lng) async {
|
||||
Get.dialog(
|
||||
MapDialogView(
|
||||
latitude: lat,
|
||||
longitude: lng,
|
||||
),
|
||||
barrierDismissible: true,
|
||||
);
|
||||
}
|
||||
}
|
||||
146
lib/pages/gaslist/gaslist_view.dart
Normal file
146
lib/pages/gaslist/gaslist_view.dart
Normal file
@@ -0,0 +1,146 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'gaslist_controller.dart';
|
||||
|
||||
class GaslistPage extends GetView<GaslistController> {
|
||||
static const namedRoute = '/gas-stations-list-page';
|
||||
const GaslistPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var gasCtrl = controller;
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
child: SafeArea(
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
shadowColor: Colors.grey,
|
||||
title: const Text('Gas Stations'),
|
||||
centerTitle: true,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: Icon(Icons.list, color: Colors.grey.shade300),
|
||||
onPressed: () async {
|
||||
// Handle go to Chart View
|
||||
gasCtrl.goToListView();
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(Icons.logout, color: Colors.grey.shade300),
|
||||
onPressed: () async {
|
||||
// Handle logout logic here
|
||||
gasCtrl.logoutSessionAndGoToLoginPage();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Obx(
|
||||
() => gasCtrl.isLoadingList.value == true
|
||||
? Center(
|
||||
child: Text('GasStations'),
|
||||
)
|
||||
: Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
child: Wrap(
|
||||
alignment: WrapAlignment.center,
|
||||
spacing: 50,
|
||||
children: [
|
||||
Divider(
|
||||
color: Colors.grey.shade300,
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
gasCtrl.szRxGasArt.value = 'DIE';
|
||||
await gasCtrl.loadListData();
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.grey.shade800,
|
||||
foregroundColor: Colors
|
||||
.orange, // Hintergrundfarbe des Buttons
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Text('Diesel'),
|
||||
Text('DIE'),
|
||||
],
|
||||
),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
gasCtrl.szRxGasArt.value = 'SUP';
|
||||
await gasCtrl.loadListData();
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.grey.shade800,
|
||||
foregroundColor: Colors
|
||||
.orange, // Hintergrundfarbe des Buttons
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Text('Benzin'),
|
||||
Text('SUP'),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Divider(
|
||||
color: Colors.grey.shade300,
|
||||
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: 5,
|
||||
itemBuilder: (context, index) {
|
||||
var gasStation = gasCtrl.gasStationsList[index];
|
||||
return ListTile(
|
||||
shape: RoundedRectangleBorder(
|
||||
side: BorderSide(
|
||||
color: Colors.grey, // Border color
|
||||
width: 1.0, // Border thickness
|
||||
),
|
||||
borderRadius: BorderRadius.circular(5.0),
|
||||
),
|
||||
onTap: () {
|
||||
// Handle item tap if needed
|
||||
gasCtrl.openDirectionMaps(
|
||||
gasStation.location!.latitude!,
|
||||
gasStation.location!.longitude!);
|
||||
},
|
||||
title: Text(gasStation.name ?? 'No Name'),
|
||||
subtitle: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(gasStation.location?.address ??
|
||||
'No Address'),
|
||||
Text(gasStation.distance != null
|
||||
? '${gasStation.distance?.toStringAsFixed(2)} km'
|
||||
: 'No Distance'),
|
||||
],
|
||||
),
|
||||
trailing: gasStation.prices != null &&
|
||||
gasStation.prices!.isNotEmpty
|
||||
? Column(
|
||||
children: [
|
||||
Text(gasStation.prices?[0].fuelType ??
|
||||
'N/A'),
|
||||
Text(
|
||||
'${gasStation.prices?[0].amount?.toStringAsPrecision(4) ?? 'N/A'} €'),
|
||||
],
|
||||
)
|
||||
: const Text('N/A'),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
76
lib/pages/gaslist/widgets/map_view.dart
Normal file
76
lib/pages/gaslist/widgets/map_view.dart
Normal file
@@ -0,0 +1,76 @@
|
||||
// map_dialog_view.dart
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
class MapDialogView extends StatelessWidget {
|
||||
final double latitude;
|
||||
final double longitude;
|
||||
|
||||
const MapDialogView({
|
||||
super.key,
|
||||
required this.latitude,
|
||||
required this.longitude,
|
||||
});
|
||||
|
||||
// Funktion zum Öffnen der Karten-URL
|
||||
void _openMap() async {
|
||||
String url = '';
|
||||
final coords = '$latitude,$longitude';
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
// Android: Startet die Google Maps Navigation
|
||||
url =
|
||||
'https://www.google.com/maps/dir/?api=1&destination=$coords'; // mode=d für Fahren
|
||||
} else if (Platform.isIOS) {
|
||||
// iOS: Startet die Apple Maps Navigation
|
||||
url =
|
||||
'https://maps.apple.com/?daddr=$coords&dirflg=d'; // daddr für destination, dirflg=d für driving
|
||||
} else {
|
||||
// Fallback-URL für die Google Maps Website mit Wegbeschreibung
|
||||
url = 'https://www.google.com/maps/dir/?api=1&destination=$coords';
|
||||
}
|
||||
// Hier die URL-Logik einfügen, die Sie bereits kennen
|
||||
//final url = 'http://googleusercontent.com/maps.google.com/8';
|
||||
final uri = Uri.parse(url);
|
||||
|
||||
if (await canLaunchUrl(uri)) {
|
||||
await launchUrl(uri);
|
||||
} else {
|
||||
// Eine Snackbar oder ein Dialog, um den Fehler zu melden
|
||||
print('Fehler: Konnte Karten-App nicht öffnen.');
|
||||
}
|
||||
Get.back();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text('Karte öffnen'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment:
|
||||
CrossAxisAlignment.start, // Passt die Höhe an den Inhalt an
|
||||
children: [
|
||||
Text('Koordinaten'),
|
||||
Text('Latitude: $latitude'),
|
||||
Text('Longitude: $longitude'),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: _openMap, // Ruft die Methode zum Öffnen der Karte auf
|
||||
child: const Text('Karten-App öffnen'),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(), // Schließt den Dialog
|
||||
child: const Text('Schließen'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user