Files
rog_app/lib/widgets/list_widget.dart

190 lines
7.7 KiB
Dart
Raw Normal View History

2022-04-17 11:45:21 +05:30
import 'package:flutter/material.dart';
import 'package:geojson/geojson.dart';
import 'package:get/get.dart';
2022-09-22 20:36:56 +05:30
import 'package:rogapp/model/destination.dart';
import 'package:rogapp/pages/destination/destination_controller.dart';
2022-05-12 02:17:08 +05:30
import 'package:rogapp/pages/index/index_controller.dart';
2023-06-11 21:03:30 +05:30
import 'package:rogapp/services/maxtrix_service.dart';
2023-02-02 15:43:35 +05:30
import 'package:rogapp/utils/const.dart';
2022-06-27 12:15:54 +05:30
import 'package:rogapp/widgets/bottom_sheet_new.dart';
2022-04-17 11:45:21 +05:30
2023-06-11 21:03:30 +05:30
class ListWidget extends StatefulWidget {
2023-09-06 21:36:11 +05:30
const ListWidget({Key? key}) : super(key: key);
2022-04-17 11:45:21 +05:30
2023-06-11 21:03:30 +05:30
@override
State<ListWidget> createState() => _ListWidgetState();
}
class _ListWidgetState extends State<ListWidget> {
2022-04-17 11:45:21 +05:30
final IndexController indexController = Get.find<IndexController>();
2023-06-11 21:03:30 +05:30
2023-09-06 21:36:11 +05:30
final DestinationController destinationController =
Get.find<DestinationController>();
2022-04-17 11:45:21 +05:30
2023-09-06 21:36:11 +05:30
Image getImage(int index) {
if (indexController.locations[0].collection[index].properties!["photos"] ==
null ||
indexController.locations[0].collection[index].properties!["photos"] ==
"") {
2023-08-16 14:53:32 +05:30
return const Image(image: AssetImage('assets/images/empty_image.png'));
2023-09-06 21:36:11 +05:30
} else {
2023-08-16 14:53:32 +05:30
print("==== photo index is $index ===");
String serverUrl = ConstValues.currentServer();
2023-09-06 21:36:11 +05:30
GeoJsonFeature<dynamic> gf =
indexController.locations[0].collection[index];
2023-08-16 14:53:32 +05:30
String _photo = gf.properties!["photos"];
2022-06-27 12:15:54 +05:30
return Image(
2023-09-06 21:36:11 +05:30
image: NetworkImage('$serverUrl/media/compressed/' + _photo),
errorBuilder:
(BuildContext context, Object exception, StackTrace? stackTrace) {
2022-06-27 12:15:54 +05:30
return Image.asset("assets/images/empty_image.png");
},
2023-09-06 21:36:11 +05:30
);
2022-04-17 11:45:21 +05:30
}
}
2023-09-06 21:36:11 +05:30
void changeCurrentFeature(GeoJsonFeature fs) {
if (indexController.currentFeature.isNotEmpty) {
2022-04-17 11:45:21 +05:30
indexController.currentFeature.clear();
}
indexController.currentFeature.add(fs);
}
2023-06-11 21:03:30 +05:30
@override
void initState() {
super.initState();
2023-06-14 11:27:11 +05:30
}
Destination createDestination(GeoJsonFeature feature) {
final props = feature.properties;
GeoJsonMultiPoint _pt = feature.geometry;
return Destination(
cp: props!['cp'],
lat: _pt.geoSerie!.geoPoints.first.latitude,
lon: _pt.geoSerie!.geoPoints.first.longitude,
);
2023-06-11 21:03:30 +05:30
}
Future<String> matrixDistance(int i) async {
2023-06-14 11:27:11 +05:30
// Create two destinations directly from indexController.locations[0].collection
2023-09-06 21:36:11 +05:30
Destination desCurr = Destination(
lat: indexController.current_lat, lon: indexController.current_lon);
//Destination dest1 = createDestination(indexController.locations[0].collection[0]);
2023-09-06 21:36:11 +05:30
Destination dest2 =
createDestination(indexController.locations[0].collection[i]);
2023-06-14 11:27:11 +05:30
// Get the distance between these two destinations
final res = await MatrixService.getDestinations([desCurr, dest2]);
2023-06-14 11:27:11 +05:30
2023-06-11 21:03:30 +05:30
return res["routes"][0]["legs"][0]["distance"]["text"];
//print("matrix result is ${i} : ${res["routes"][0]["legs"][0]["distance"]["text"]} ");
}
Future<void> _pullRefresh() async {
print("pull to refesh");
2023-09-06 21:36:11 +05:30
indexController.locations[0].collection.sort((a, b) =>
2023-06-11 21:03:30 +05:30
(a.properties!['cp'] as Comparable)
2023-09-06 21:36:11 +05:30
.compareTo(b.properties!['cp'] as Comparable));
setState(() {});
2023-06-11 21:03:30 +05:30
}
2022-04-17 11:45:21 +05:30
@override
Widget build(BuildContext context) {
2023-09-06 21:36:11 +05:30
return Obx(
() => indexController.locations.isNotEmpty
? RefreshIndicator(
onRefresh: _pullRefresh,
child: ListView.builder(
2023-06-11 21:03:30 +05:30
itemCount: indexController.locations[0].collection.length,
shrinkWrap: true,
2023-09-06 21:36:11 +05:30
itemBuilder: (_, index) {
2023-06-11 21:03:30 +05:30
bool _is_found = false;
2023-09-06 21:36:11 +05:30
for (Destination d in destinationController.destinations) {
if (indexController.locations[0].collection[index]
.properties!['location_id'] ==
d.location_id) {
2023-06-11 21:03:30 +05:30
_is_found = true;
break;
}
2022-09-22 20:36:56 +05:30
}
2023-06-11 21:03:30 +05:30
return Card(
child: ListTile(
2023-09-06 21:36:11 +05:30
selected: _is_found,
selectedTileColor: Colors.yellow.shade200,
onTap: () {
GeoJsonFeature gf =
indexController.locations[0].collection[index];
2023-09-14 22:53:51 +05:30
Destination des = destinationController.festuretoDestination(gf);
2023-09-06 21:36:11 +05:30
changeCurrentFeature(gf);
showModalBottomSheet(
constraints: BoxConstraints.loose(
Size(Get.width, Get.height * 0.75)),
isScrollControlled: true,
context: context,
//builder: (context) => BottomSheetWidget(),
2023-09-14 22:53:51 +05:30
builder: ((context) => BottomSheetNew(destination: des,)));
2023-09-06 21:36:11 +05:30
},
leading: getImage(index),
title: indexController.locations[0].collection[index]
.properties!['location_name'] !=
null
? Text(indexController.locations[0]
.collection[index].properties!['location_name']
.toString())
: const Text(""),
subtitle: indexController.locations[0].collection[index]
.properties!['category'] !=
null
? Text(indexController.locations[0]
.collection[index].properties!['category'])
: const Text(""),
trailing: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
indexController.locations[0].collection[index]
.properties!['sub_loc_id'] !=
null
? Text(indexController
.locations[0]
.collection[index]
.properties!['sub_loc_id'])
: const Text(""),
SizedBox(
width: 100,
child: FutureBuilder<String>(
future: matrixDistance(index),
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.hasError) {
return const Text("-");
} else {
return Text(
snapshot.data ?? '',
style: const TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold),
);
}
},
),
)
],
)),
2023-06-11 21:03:30 +05:30
);
},
2023-09-06 21:36:11 +05:30
),
)
: const SizedBox(
width: 0,
height: 0,
),
);
2022-04-17 11:45:21 +05:30
}
2023-09-06 21:36:11 +05:30
}