Files
tripz/lib/controller/home/points_for_rider_controller.dart
Hamza-Ayed 13a7c3db81 25-1/31/1
2025-01-31 14:57:17 +03:00

149 lines
4.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:Tripz/constant/style.dart';
import 'package:Tripz/controller/home/map_passenger_controller.dart';
import '../../constant/api_key.dart';
import '../../constant/links.dart';
import '../functions/crud.dart';
import '../functions/location_controller.dart';
class PointsForRiderController extends GetxController {
List<String> locations = [];
String hintTextDestinationPoint = 'Search for your destination'.tr;
TextEditingController placeStartController = TextEditingController();
void addLocation(String location) {
locations.add(location);
update();
}
void getTextFromList(String location) {
locations.add(location);
update();
Get.back();
}
void removeLocation(int index) {
locations.removeAt(index);
update();
}
void onReorder(int oldIndex, int newIndex) {
if (newIndex > oldIndex) {
newIndex -= 1;
update();
}
final item = locations.removeAt(oldIndex);
locations.insert(newIndex, item);
update();
}
}
class LocationModel {
String name;
double lat, lon;
LocationModel({required this.name, required this.lat, required this.lon});
}
class WayPointController extends GetxController {
// A list of text editing controllers for each text field
// final textFields = [TextEditingController()].obs;
List<String> wayPoints = [];
List<List<dynamic>> placeListResponse = [];
double wayPointHeight = 400;
String hintTextDestinationPoint = 'Search for your destination'.tr;
TextEditingController textSearchCotroller = TextEditingController();
// A list of places corresponding to each text field
final places = <String>[];
final hintTextPointList = <String>[];
late LatLng myLocation;
void addWayPoints() {
String wayPoint = 'Add a Stop'.tr;
if (wayPoints.length < 5) {
wayPoints.add(wayPoint);
update();
} else {
Get.defaultDialog(
title: 'This is most WayPoints',
titleStyle: AppStyle.title,
middleText: '');
}
update();
}
void removeTextField(int index) {
wayPoints.removeAt(index);
update();
}
// A method to reorder the text fields and the places
void reorderTextFields(int oldIndex, int newIndex) {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final wayPoint = wayPoints.removeAt(oldIndex);
wayPoints.insert(newIndex, wayPoint);
update();
}
void updatePlace(int index, String input) async {
var url =
'${AppLink.googleMapsLink}place/nearbysearch/json?keyword=$input&location=${myLocation.latitude},${myLocation.longitude}&radius=50000&language=en&key=${AK.mapAPIKEY.toString()}';
var response = await CRUD().getGoogleApi(link: url, payload: {});
// final place = input;
// if (index == 0) {
List<dynamic> newList = [];
placeListResponse.add(newList);
newList = response['results'];
placeListResponse[index].add(newList);
update();
// }
update();
}
@override
void onInit() {
Get.put(LocationController());
addWayPoints();
myLocation = Get.find<MapPassengerController>().passengerLocation;
super.onInit();
}
}
class PlaceList extends StatelessWidget {
// Get the controller instance
final controller = Get.put(WayPointController());
@override
Widget build(BuildContext context) {
// Use the Obx widget to rebuild the widget when the controller changes
return Obx(() {
// Use the ListView widget to display the list of places
return ListView(
// The children of the list are the places
children: [
// Loop through the places in the controller
for (final place in controller.places)
// Create a text widget for each place
Text(
// Use the place as the text
place,
// Add some style and padding
style: const TextStyle(fontSize: 18.0),
),
],
);
});
}
}