add econtrol gasstation view and logic
This commit is contained in:
83
lib/controller/gasstations_controller.dart
Normal file
83
lib/controller/gasstations_controller.dart
Normal file
@@ -0,0 +1,83 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../models/econtrol_model.dart';
|
||||
import '../services/econtrol_service.dart';
|
||||
|
||||
class GasstationsController extends GetxController {
|
||||
final isLoading = false.obs;
|
||||
final isLoadingLocation = false.obs;
|
||||
final eControlData = <EControlModel>[].obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
_loadStationInfosList();
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
@override
|
||||
void onReady() {}
|
||||
|
||||
@override
|
||||
void onClose() {}
|
||||
|
||||
Future<void> _loadStationInfosList() async {
|
||||
isLoading.value = true;
|
||||
bool serviceEnabled;
|
||||
if (eControlData.isNotEmpty) {
|
||||
eControlData.clear();
|
||||
}
|
||||
final eControlService = EControlService();
|
||||
LocationPermission permission;
|
||||
|
||||
try {
|
||||
isLoadingLocation.value = true;
|
||||
|
||||
// 1. Prüfen, ob Standortdienste aktiviert sind
|
||||
serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
return Future.error('Standortdienste sind deaktiviert.');
|
||||
}
|
||||
|
||||
// 2. Berechtigungen prüfen
|
||||
permission = await Geolocator.checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await Geolocator.requestPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
return Future.error('Berechtigung verweigert.');
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Position abrufen
|
||||
Position position = await Geolocator.getCurrentPosition(
|
||||
locationSettings: const LocationSettings(
|
||||
accuracy: LocationAccuracy.high,
|
||||
),
|
||||
);
|
||||
|
||||
// 4. Standort über Backend-Proxy abrufen
|
||||
var lat = position.latitude;
|
||||
var lon = position.longitude;
|
||||
// Simulate fetching data from an API or database
|
||||
await eControlService.getEControlData(
|
||||
lat,
|
||||
lon,
|
||||
'DIE',
|
||||
);
|
||||
eControlData.addAll(eControlService.eControlData);
|
||||
} catch (e) {
|
||||
Get.snackbar(
|
||||
"Fehler",
|
||||
"Standort konnte nicht abgerufen werden: $e",
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
backgroundColor: Colors.red[100],
|
||||
colorText: Colors.red[900],
|
||||
);
|
||||
print("Fehler beim Abrufen des Standorts: $e");
|
||||
} finally {
|
||||
isLoadingLocation.value = false;
|
||||
}
|
||||
isLoading.value = false;
|
||||
update();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:flutter_tank_web_app/pages/gasstations_view.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../models/tank_model.dart';
|
||||
@@ -105,4 +106,8 @@ class HomeController extends GetxController {
|
||||
void goToGraphPage() {
|
||||
Get.toNamed(GraphPage.namedRoute, arguments: listTankModel);
|
||||
}
|
||||
|
||||
void goToGasStations() {
|
||||
Get.toNamed(GasstationsPage.namedRoute);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import '../controller/graph_controller.dart';
|
||||
import '../controller/home_controller.dart';
|
||||
import '../controller/login_controller.dart';
|
||||
import '../controller/signin_controller.dart';
|
||||
import '../controller/gasstations_controller.dart';
|
||||
|
||||
|
||||
|
||||
@@ -19,6 +20,7 @@ class SampleBindings extends Bindings {
|
||||
Get.lazyPut<DetailController>(() => DetailController());
|
||||
Get.lazyPut<EditController>(() => EditController());
|
||||
Get.lazyPut<GraphController>(() => GraphController());
|
||||
Get.lazyPut<GasstationsController>(() => GasstationsController());
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:get/get.dart';
|
||||
import '../pages/detail_view.dart';
|
||||
import '../pages/edit_view.dart';
|
||||
import '../pages/gasstations_view.dart';
|
||||
import '../pages/graph_view.dart';
|
||||
import 'sample_bindings.dart';
|
||||
import '../pages/home_view.dart';
|
||||
@@ -40,5 +41,10 @@ class SampleRouts {
|
||||
page: () => const GraphPage(),
|
||||
binding: sampleBindings,
|
||||
),
|
||||
GetPage(
|
||||
name: GasstationsPage.namedRoute,
|
||||
page: () => const GasstationsPage(),
|
||||
binding: sampleBindings,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
269
lib/models/econtrol_model.dart
Normal file
269
lib/models/econtrol_model.dart
Normal file
@@ -0,0 +1,269 @@
|
||||
|
||||
|
||||
class EControlModel {
|
||||
int? id;
|
||||
String? name;
|
||||
Location? location;
|
||||
Contact? contact;
|
||||
List<OpeningHours>? openingHours;
|
||||
OfferInformation? offerInformation;
|
||||
PaymentMethods? paymentMethods;
|
||||
PaymentArrangements? paymentArrangements;
|
||||
int? position;
|
||||
bool? open;
|
||||
double? distance;
|
||||
List<Prices>? prices;
|
||||
|
||||
EControlModel(
|
||||
{this.id,
|
||||
this.name,
|
||||
this.location,
|
||||
this.contact,
|
||||
this.openingHours,
|
||||
this.offerInformation,
|
||||
this.paymentMethods,
|
||||
this.paymentArrangements,
|
||||
this.position,
|
||||
this.open,
|
||||
this.distance,
|
||||
this.prices});
|
||||
|
||||
EControlModel.fromJson(Map<String, dynamic> json) {
|
||||
id = json['id'];
|
||||
name = json['name'];
|
||||
location = json['location'] != null
|
||||
? Location.fromJson(json['location'])
|
||||
: null;
|
||||
contact =
|
||||
json['contact'] != null ? Contact.fromJson(json['contact']) : null;
|
||||
if (json['openingHours'] != null) {
|
||||
openingHours = <OpeningHours>[];
|
||||
json['openingHours'].forEach((v) {
|
||||
openingHours!.add(OpeningHours.fromJson(v));
|
||||
});
|
||||
}
|
||||
offerInformation = json['offerInformation'] != null
|
||||
? OfferInformation.fromJson(json['offerInformation'])
|
||||
: null;
|
||||
paymentMethods = json['paymentMethods'] != null
|
||||
? PaymentMethods.fromJson(json['paymentMethods'])
|
||||
: null;
|
||||
paymentArrangements = json['paymentArrangements'] != null
|
||||
? PaymentArrangements.fromJson(json['paymentArrangements'])
|
||||
: null;
|
||||
position = json['position'];
|
||||
open = json['open'];
|
||||
distance = json['distance'];
|
||||
if (json['prices'] != null) {
|
||||
prices = <Prices>[];
|
||||
json['prices'].forEach((v) {
|
||||
prices!.add(Prices.fromJson(v));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data['id'] = id;
|
||||
data['name'] = name;
|
||||
if (location != null) {
|
||||
data['location'] = location!.toJson();
|
||||
}
|
||||
if (contact != null) {
|
||||
data['contact'] = contact!.toJson();
|
||||
}
|
||||
if (openingHours != null) {
|
||||
data['openingHours'] = openingHours!.map((v) => v.toJson()).toList();
|
||||
}
|
||||
if (offerInformation != null) {
|
||||
data['offerInformation'] = offerInformation!.toJson();
|
||||
}
|
||||
if (paymentMethods != null) {
|
||||
data['paymentMethods'] = paymentMethods!.toJson();
|
||||
}
|
||||
if (paymentArrangements != null) {
|
||||
data['paymentArrangements'] = paymentArrangements!.toJson();
|
||||
}
|
||||
data['position'] = position;
|
||||
data['open'] = open;
|
||||
data['distance'] = distance;
|
||||
if (prices != null) {
|
||||
data['prices'] = prices!.map((v) => v.toJson()).toList();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class Location {
|
||||
String? address;
|
||||
String? postalCode;
|
||||
String? city;
|
||||
double? latitude;
|
||||
double? longitude;
|
||||
|
||||
Location(
|
||||
{this.address,
|
||||
this.postalCode,
|
||||
this.city,
|
||||
this.latitude,
|
||||
this.longitude});
|
||||
|
||||
Location.fromJson(Map<String, dynamic> json) {
|
||||
address = json['address'];
|
||||
postalCode = json['postalCode'];
|
||||
city = json['city'];
|
||||
latitude = json['latitude'];
|
||||
longitude = json['longitude'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data['address'] = address;
|
||||
data['postalCode'] = postalCode;
|
||||
data['city'] = city;
|
||||
data['latitude'] = latitude;
|
||||
data['longitude'] = longitude;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class Contact {
|
||||
String? telephone;
|
||||
String? fax;
|
||||
String? mail;
|
||||
String? website;
|
||||
|
||||
Contact({this.telephone, this.fax, this.mail, this.website});
|
||||
|
||||
Contact.fromJson(Map<String, dynamic> json) {
|
||||
telephone = json['telephone'];
|
||||
fax = json['fax'];
|
||||
mail = json['mail'];
|
||||
website = json['website'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data['telephone'] = telephone;
|
||||
data['fax'] = fax;
|
||||
data['mail'] = mail;
|
||||
data['website'] = website;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class OpeningHours {
|
||||
String? day;
|
||||
String? label;
|
||||
int? order;
|
||||
String? from;
|
||||
String? to;
|
||||
|
||||
OpeningHours({this.day, this.label, this.order, this.from, this.to});
|
||||
|
||||
OpeningHours.fromJson(Map<String, dynamic> json) {
|
||||
day = json['day'];
|
||||
label = json['label'];
|
||||
order = json['order'];
|
||||
from = json['from'];
|
||||
to = json['to'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data['day'] = day;
|
||||
data['label'] = label;
|
||||
data['order'] = order;
|
||||
data['from'] = from;
|
||||
data['to'] = to;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class OfferInformation {
|
||||
bool? service;
|
||||
bool? selfService;
|
||||
bool? unattended;
|
||||
|
||||
OfferInformation({this.service, this.selfService, this.unattended});
|
||||
|
||||
OfferInformation.fromJson(Map<String, dynamic> json) {
|
||||
service = json['service'];
|
||||
selfService = json['selfService'];
|
||||
unattended = json['unattended'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data['service'] = service;
|
||||
data['selfService'] = selfService;
|
||||
data['unattended'] = unattended;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class PaymentMethods {
|
||||
bool? cash;
|
||||
bool? debitCard;
|
||||
bool? creditCard;
|
||||
String? others;
|
||||
|
||||
PaymentMethods({this.cash, this.debitCard, this.creditCard, this.others});
|
||||
|
||||
PaymentMethods.fromJson(Map<String, dynamic> json) {
|
||||
cash = json['cash'];
|
||||
debitCard = json['debitCard'];
|
||||
creditCard = json['creditCard'];
|
||||
others = json['others'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data['cash'] = cash;
|
||||
data['debitCard'] = debitCard;
|
||||
data['creditCard'] = creditCard;
|
||||
data['others'] = others;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class PaymentArrangements {
|
||||
bool? cooperative;
|
||||
bool? clubCard;
|
||||
|
||||
PaymentArrangements({this.cooperative, this.clubCard});
|
||||
|
||||
PaymentArrangements.fromJson(Map<String, dynamic> json) {
|
||||
cooperative = json['cooperative'];
|
||||
clubCard = json['clubCard'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data['cooperative'] = cooperative;
|
||||
data['clubCard'] = clubCard;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class Prices {
|
||||
String? fuelType;
|
||||
double? amount;
|
||||
String? label;
|
||||
|
||||
Prices({this.fuelType, this.amount, this.label});
|
||||
|
||||
Prices.fromJson(Map<String, dynamic> json) {
|
||||
fuelType = json['fuelType'];
|
||||
amount = json['amount'];
|
||||
label = json['label'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data['fuelType'] = fuelType;
|
||||
data['amount'] = amount;
|
||||
data['label'] = label;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
56
lib/pages/gasstations_view.dart
Normal file
56
lib/pages/gasstations_view.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../controller/gasstations_controller.dart';
|
||||
|
||||
class GasstationsPage extends GetView<GasstationsController> {
|
||||
static const String namedRoute = '/gasstations-page';
|
||||
const GasstationsPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var staCtrl = controller;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
toolbarHeight: 100,
|
||||
backgroundColor: Colors.blueGrey,
|
||||
foregroundColor: Colors.white,
|
||||
title: const Text('Gas Stations'),
|
||||
),
|
||||
body: Container(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
Colors.blueGrey[800]!,
|
||||
Colors.blueGrey[600]!,
|
||||
Colors.blueGrey[300]!,
|
||||
Colors.blue[100]!,
|
||||
],
|
||||
),
|
||||
),
|
||||
child: Center(
|
||||
child: Obx(() {
|
||||
if (staCtrl.isLoading.value) {
|
||||
return const CircularProgressIndicator();
|
||||
} else if (staCtrl.eControlData.isEmpty) {
|
||||
return const Text('No gas stations found.');
|
||||
} else {
|
||||
return ListView.builder(
|
||||
itemCount: staCtrl.eControlData.length,
|
||||
itemBuilder: (context, index) {
|
||||
final station = staCtrl.eControlData[index];
|
||||
return ListTile(
|
||||
title: Text(station.name ?? 'Unknown Station'),
|
||||
subtitle: Text(station.location?.address ?? 'No address'),
|
||||
trailing: Text(station.open == true ? 'Open' : 'Closed'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,12 @@ class HomePage extends GetView<HomeController> {
|
||||
homCtrl.onInit();
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.gas_meter),
|
||||
onPressed: () {
|
||||
homCtrl.goToGasStations();
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.collections),
|
||||
onPressed: () {
|
||||
|
||||
70
lib/services/econtrol_service.dart
Normal file
70
lib/services/econtrol_service.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
|
||||
import '../models/econtrol_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class EControlService {
|
||||
late List<EControlModel> eControlData;
|
||||
|
||||
Future<void> getEControlData(double latitude, double longitude, String fuelType) async {
|
||||
// Simulate fetching data from an API or database
|
||||
await Future.delayed(Duration(seconds: 2)); // Simulate network delay
|
||||
eControlData = [
|
||||
EControlModel(
|
||||
id: 1,
|
||||
name: 'E-Control Station 1',
|
||||
location: Location(
|
||||
latitude: 47.93875449671056,
|
||||
longitude: 13.762706553431048,
|
||||
),
|
||||
contact: Contact(
|
||||
telephone: '+43 123 456789',
|
||||
mail: '',
|
||||
website: 'https://www.econtrol.at',
|
||||
),
|
||||
openingHours: [
|
||||
OpeningHours(
|
||||
day: 'Tuesday',
|
||||
label: '08:00',
|
||||
order: 1,
|
||||
from: '08:00',
|
||||
to: '18:00',
|
||||
),
|
||||
],
|
||||
offerInformation: OfferInformation(
|
||||
service: true,
|
||||
selfService: true,
|
||||
unattended: true,
|
||||
),
|
||||
paymentMethods: PaymentMethods(
|
||||
cash: true,
|
||||
debitCard: true,
|
||||
creditCard: false,
|
||||
others: '',
|
||||
),
|
||||
paymentArrangements: PaymentArrangements(
|
||||
cooperative: false,
|
||||
clubCard: true,
|
||||
),
|
||||
position: 1,
|
||||
open: true,
|
||||
distance: 0.5,
|
||||
prices: [Prices(fuelType: 'DIE', amount: 1.445, label: 'Diesel')],
|
||||
),
|
||||
];
|
||||
// REST Service... URL
|
||||
String apiUrl = 'https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=$latitude&longitude=$longitude&fuelType=$fuelType&includeClosed=false';
|
||||
try {
|
||||
var response = await http.get(Uri.parse(apiUrl));
|
||||
if (response.statusCode == 200) {
|
||||
eControlData.clear(); // Clear existing data before adding new results
|
||||
// Parse the response and update eControlData
|
||||
print('E-Control API response: ${response.body}');
|
||||
eControlData = (response.body as List).map((json) => EControlModel.fromJson(json)).toList();
|
||||
print('E-Control data parsed successfully: ${eControlData.length} stations found');
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error fetching E-Control data: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user