Files
rog_app/lib/widgets/list_widget.dart

214 lines
9.5 KiB
Dart
Raw Normal View History

2022-04-17 11:45:21 +05:30
import 'package:flutter/material.dart';
2023-12-06 11:16:17 +05:30
import 'package:geojson_vi/geojson_vi.dart';
2022-04-17 11:45:21 +05:30
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';
2024-04-20 12:34:49 +09:00
//import 'package:rogapp/widgets/bottom_sheets/bottom_sheet_start.dart';
//import 'package:rogapp/widgets/bottom_sheets/bottom_sheet_goal.dart';
//import 'package:rogapp/widgets/bottom_sheets/bottom_sheet_normal_point.dart';
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();
}
// IndexControllerから目的地のリストを取得し、ListView.builderを使用してリストを表示します。
// 各リストアイテムは、目的地の画像、名前、カテゴリ、サブロケーションID、現在地からの距離を表示します。
// リストアイテムがタップされると、changeCurrentFeatureメソッドを呼び出して現在の目的地を更新し、 BottomSheetウィジェットを表示します。
// 主なロジック:
// IndexControllerから目的地のリストを取得し、ListView.builderを使用してリストを構築します。
// getImageメソッドを使用して、目的地の画像を取得し表示します。画像が存在しない場合は、デフォルトの画像を表示します。
// matrixDistanceメソッドを使用して、現在地から目的地までの距離を計算し表示します。
// リストアイテムがタップされると、changeCurrentFeatureメソッドを呼び出して現在の目的地を更新し、showModalBottomSheetを使用してBottomSheetウィジェットを表示します。
//
2023-06-11 21:03:30 +05:30
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) {
2023-12-06 11:16:17 +05:30
if (indexController.locations[0].features[index]!.properties!["photos"] ==
2023-09-06 21:36:11 +05:30
null ||
2023-12-06 11:16:17 +05:30
indexController.locations[0].features[index]!.properties!["photos"] ==
2023-09-06 21:36:11 +05:30
"") {
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-10-06 16:25:21 +05:30
//print("==== photo index is $index ===");
2023-08-16 14:53:32 +05:30
String serverUrl = ConstValues.currentServer();
2023-12-06 11:16:17 +05:30
GeoJSONFeature gf = indexController.locations[0].features[index]!;
2023-10-06 16:25:21 +05:30
String photo = gf.properties!["photos"];
2022-06-27 12:15:54 +05:30
return Image(
2023-10-06 16:25:21 +05:30
image: NetworkImage('$serverUrl/media/compressed/$photo'),
2023-09-06 21:36:11 +05:30
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-12-06 11:16:17 +05:30
void changeCurrentFeature(GeoJSONFeature fs) {
2023-09-06 21:36:11 +05:30
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
}
2023-12-06 11:16:17 +05:30
Destination createDestination(GeoJSONFeature feature) {
2023-06-14 11:27:11 +05:30
final props = feature.properties;
2023-12-06 11:16:17 +05:30
GeoJSONMultiPoint pt = feature.geometry as GeoJSONMultiPoint;
2023-06-14 11:27:11 +05:30
return Destination(
cp: props!['cp'],
2023-12-06 11:16:17 +05:30
lat: pt.coordinates[0][1],
lon: pt.coordinates[0][0],
2023-06-14 11:27:11 +05:30
);
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(
2023-10-06 16:25:21 +05:30
lat: indexController.currentLat, lon: indexController.currentLon);
//Destination dest1 = createDestination(indexController.locations[0].collection[0]);
2023-09-06 21:36:11 +05:30
Destination dest2 =
2023-12-06 11:16:17 +05:30
createDestination(indexController.locations[0].features[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 {
2023-10-06 16:25:21 +05:30
//print("pull to refesh");
2023-12-06 11:16:17 +05:30
indexController.locations[0].features.sort((a, b) =>
(a!.properties!['cp'] as Comparable)
.compareTo(b!.properties!['cp'] as Comparable));
2023-09-06 21:36:11 +05:30
setState(() {});
2023-06-11 21:03:30 +05:30
}
2022-04-17 11:45:21 +05:30
@override
Widget build(BuildContext context) {
2024-04-20 12:34:49 +09:00
debugPrint("_ListWidgetState");
2023-09-06 21:36:11 +05:30
return Obx(
() => indexController.locations.isNotEmpty
? RefreshIndicator(
onRefresh: _pullRefresh,
child: ListView.builder(
2023-12-06 11:16:17 +05:30
itemCount: indexController.locations[0].features.length,
2023-06-11 21:03:30 +05:30
shrinkWrap: true,
2023-09-06 21:36:11 +05:30
itemBuilder: (_, index) {
2023-10-06 16:25:21 +05:30
bool isFound = false;
2023-09-06 21:36:11 +05:30
for (Destination d in destinationController.destinations) {
2023-12-06 11:16:17 +05:30
if (indexController.locations[0].features[index]!
2023-09-06 21:36:11 +05:30
.properties!['location_id'] ==
d.location_id) {
2023-10-06 16:25:21 +05:30
isFound = true;
2023-06-11 21:03:30 +05:30
break;
}
2022-09-22 20:36:56 +05:30
}
2023-06-11 21:03:30 +05:30
return Card(
child: ListTile(
2023-10-06 16:25:21 +05:30
selected: isFound,
2023-09-06 21:36:11 +05:30
selectedTileColor: Colors.yellow.shade200,
onTap: () {
2023-12-06 11:16:17 +05:30
GeoJSONFeature gf =
indexController.locations[0].features[index]!;
2023-10-06 16:25:21 +05:30
Destination des =
destinationController.festuretoDestination(gf);
2023-09-06 21:36:11 +05:30
changeCurrentFeature(gf);
2024-04-20 12:34:49 +09:00
Widget bottomSheet = BottomSheetNew(destination: des);
/*
if (des.cp == -1 || des.cp == 0) {
bottomSheet = BottomSheetStart(destination: des);
} else if (des.cp == -2 || des.cp == 0) {
bottomSheet = BottomSheetGoal(destination: des);
} else {
bottomSheet = BottomSheetNormalPoint(destination: des);
}
2024-04-20 12:34:49 +09:00
*/
2023-09-06 21:36:11 +05:30
showModalBottomSheet(
constraints: BoxConstraints.loose(
2023-11-07 17:17:47 +05:30
Size(Get.width, Get.height * 0.85)),
2023-09-06 21:36:11 +05:30
isScrollControlled: true,
context: context,
builder: ((context) => bottomSheet ),
);
2023-09-06 21:36:11 +05:30
},
leading: getImage(index),
2023-12-06 11:16:17 +05:30
title: indexController.locations[0].features[index]!
2023-09-06 21:36:11 +05:30
.properties!['location_name'] !=
null
2023-12-06 11:16:17 +05:30
? Text(indexController.locations[0].features[index]!
.properties!['location_name']
2023-09-06 21:36:11 +05:30
.toString())
: const Text(""),
2023-12-06 11:16:17 +05:30
subtitle: indexController.locations[0].features[index]!
2023-09-06 21:36:11 +05:30
.properties!['category'] !=
null
2023-12-06 11:16:17 +05:30
? Text(indexController.locations[0].features[index]!
.properties!['category'])
2023-09-06 21:36:11 +05:30
: const Text(""),
trailing: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
2023-12-06 11:16:17 +05:30
indexController.locations[0].features[index]!
2023-09-06 21:36:11 +05:30
.properties!['sub_loc_id'] !=
null
2023-12-06 11:16:17 +05:30
? Text(indexController.locations[0]
.features[index]!.properties!['sub_loc_id'])
2023-09-06 21:36:11 +05:30
: 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
}