fixed popup buttons added location stream and center button

This commit is contained in:
2024-03-06 11:40:00 +05:30
parent 2f329669e9
commit e6a7d37519
6 changed files with 112 additions and 44 deletions

View File

@ -3,6 +3,7 @@ import 'dart:typed_data';
import 'package:camera_camera/camera_camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:geojson_vi/geojson_vi.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
@ -756,9 +757,17 @@ class DestinationController extends GetxController {
@override
void onInit() async {
ever(locationController.currentPosition, handleLocationUpdate);
startGame();
super.onInit();
locationController.locationMarkerPositionStream.listen(
(locationMarkerPosition) {
if (locationMarkerPosition != null) {
handleLocationUpdate(locationMarkerPosition);
}
}, onError: (err) {
print("Error: $err");
});
startGame();
}
@override
@ -766,7 +775,7 @@ class DestinationController extends GetxController {
locationController.stopPositionStream();
}
void handleLocationUpdate(Position? position) async {
void handleLocationUpdate(LocationMarkerPosition? position) async {
try {
if (position != null) {
if (distanceToStart() >= 1000) {
@ -795,7 +804,7 @@ class DestinationController extends GetxController {
}
} finally {
if (position != null &&
(position.latitude != 0 || position.longitude != 0)) {
(position.latitude != 0 && position.longitude != 0)) {
currentLat = position.latitude;
currentLon = position.longitude;
}
@ -912,13 +921,13 @@ class DestinationController extends GetxController {
indexController.currentBound.clear();
indexController.currentBound.add(bnds);
indexController.loadLocationsBound();
centerMapToCurrentLocation();
}
});
centerMapToCurrentLocation();
}
void centerMapToCurrentLocation() {
print("center is ${currentLat}, ${currentLon}");
print("center is ${currentLon}, ${currentLon}");
if (currentLat != 0 || currentLon != 0) {
indexController.mapController.move(LatLng(currentLat, currentLon), 17.0);
}

View File

@ -51,7 +51,7 @@ class MatrixService {
Map<String, dynamic> cats = {};
String url =
"https://maps.googleapis.com/maps/api/directions/json?destination=$destination&mode=$mode&waypoints=$locs&origin=$origin&key=AIzaSyAUBI1ablMKuJwGj2-kSuEhvYxvB1A-mOE";
"https://maps.googleapis.com/maps/api/directions/json?destination=$destination&mode=$mode&waypoints=$locs&origin=$origin&key=AIzaSyCN2xFsqFyadWwpjiFxymrxzS6G1tNzraI";
//print('++++++++$url');
final http.Response response =
await http.get(Uri.parse(url), headers: <String, String>{

View File

@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:geolocator/geolocator.dart';
import 'package:rogapp/widgets/debug_widget.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
class LocationController extends GetxController {
// Reactive variable to hold the current position
@ -10,8 +11,15 @@ class LocationController extends GetxController {
// Subscription to the position stream
StreamSubscription<Position>? positionStream;
final locationMarkerPositionStreamController =
StreamController<LocationMarkerPosition?>.broadcast();
bool isStreamPaused = false;
Stream<LocationMarkerPosition?> get locationMarkerPositionStream =>
locationMarkerPositionStreamController.stream;
@override
void onInit() {
super.onInit();
@ -99,36 +107,33 @@ class LocationController extends GetxController {
// Set up the location options
const locationOptions =
LocationSettings(accuracy: LocationAccuracy.high, distanceFilter: 10);
LocationSettings(accuracy: LocationAccuracy.high, distanceFilter: 0);
// Start listening to the position stream
// positionStream =
// Geolocator.getPositionStream(locationSettings: locationOptions).listen(
// (Position position) {
// currentPosition.value = position;
// }, onError: (e) {
// // Handle errors or exceptions in the stream
// // You might want to log this error or use a state to show error messages
// });
await positionStream?.cancel();
StreamSubscription<Position> positionStream =
Geolocator.getPositionStream(locationSettings: locationOptions)
.listen((Position? position) {
// print(position == null
// ? 'Unknown'
// : '${position.latitude.toString()}, ${position.longitude.toString()}');
if (position?.accuracy != null && position!.accuracy <= 15) {
currentPosition.value = position;
}
// LogManager().addLog(
// "GPS : ${position!.latitude.toString()}, ${position.longitude.toString()} - ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}:${DateTime.now().microsecond}");
});
positionStream =
Geolocator.getPositionStream(locationSettings: locationOptions).listen(
(Position? position) {
if (position != null) {
final LocationMarkerPosition locationMarkerPosition =
LocationMarkerPosition(
latitude: position.latitude,
longitude: position.longitude,
accuracy: position.accuracy);
locationMarkerPositionStreamController.add(locationMarkerPosition);
} else {
locationMarkerPositionStreamController.add(null);
}
},
onError: (e) {
locationMarkerPositionStreamController.addError(e);
},
);
// Resume stream if it was paused previously
if (isStreamPaused) {
isStreamPaused = false;
positionStream.resume();
positionStream!.resume();
}
}

View File

@ -111,6 +111,11 @@ class BottomSheetNew extends GetView<BottomSheetController> {
}
Widget getActionButton(BuildContext context, Destination destination) {
print("getActionButton ${destinationController.rogainingCounted.value}");
print("getActionButton ${destinationController.distanceToStart()}");
print("getActionButton ${destination.cp}");
print("getActionButton ${DestinationController.ready_for_goal}");
Destination cdest = destinationController
.festuretoDestination(indexController.currentFeature[0]);
var distance = const Distance();
@ -121,8 +126,8 @@ class BottomSheetNew extends GetView<BottomSheetController> {
LatLng(cdest.lat!, cdest.lon!));
if (destinationController.rogainingCounted.value == true &&
destinationController.distanceToStart() <= 500 //destination.cp == -1
&&
destinationController.distanceToStart() <= 500 &&
destination.cp == -1 &&
DestinationController.ready_for_goal == true) {
//goal
return ElevatedButton(
@ -161,9 +166,12 @@ class BottomSheetNew extends GetView<BottomSheetController> {
),
onPressed: () async {
// Check conditions to show confirmation dialog
if (destination.cp == -1 &&
destinationController.isInRog.value == false &&
if (destinationController.isInRog.value == false &&
destination.cp == -1 &&
DestinationController.ready_for_goal &&
destinationController.rogainingCounted.value == false) {
print("counted ${destinationController.rogainingCounted.value}");
// Show confirmation dialog
Get.dialog(
AlertDialog(
@ -182,8 +190,7 @@ class BottomSheetNew extends GetView<BottomSheetController> {
onPressed: () async {
// Clear data and start game logic here
destinationController.isInRog.value = true;
destinationController
.resetRogaining(); // Assuming you have a method to clear data
destinationController.resetRogaining();
destinationController.addToRogaining(
destinationController.currentLat,
destinationController.currentLon,
@ -199,13 +206,16 @@ class BottomSheetNew extends GetView<BottomSheetController> {
barrierDismissible:
false, // User must tap a button to close the dialog
);
} else {
} else if (destinationController.isInRog.value == true) {
//print("counted ${destinationController.rogainingCounted.value}");
// Existing logic for other conditions
if (destination.cp == -1) {
return;
}
Get.back();
await destinationController.callforCheckin(destination);
} else {
return;
}
},
child: Text(
@ -218,7 +228,9 @@ class BottomSheetNew extends GetView<BottomSheetController> {
? "ゲーム中"
: isAlreadyCheckedIn == true
? "ゲーム中"
: "チェックイン",
: destinationController.isInRog.value == true
? "チェックイン"
: "始まっていない",
style: TextStyle(color: Theme.of(context).colorScheme.onSecondary),
),
);

View File

@ -0,0 +1,34 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:rogapp/pages/destination/destination_controller.dart';
class CurrentPosition extends StatefulWidget {
const CurrentPosition({super.key});
@override
State<CurrentPosition> createState() => _CurrentPositionState();
}
class _CurrentPositionState extends State<CurrentPosition> {
final DestinationController destinationController =
Get.find<DestinationController>();
@override
Widget build(BuildContext context) {
return Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: Colors.grey, borderRadius: BorderRadius.circular(20.0)),
child: IconButton(
onPressed: () {
destinationController.centerMapToCurrentLocation();
},
icon: const Icon(
Icons.location_searching,
color: Colors.white,
),
),
);
}
}

View File

@ -11,9 +11,11 @@ import 'package:rogapp/model/destination.dart';
import 'package:rogapp/pages/destination/destination_controller.dart';
import 'package:rogapp/pages/index/index_controller.dart';
import 'package:rogapp/utils/database_helper.dart';
import 'package:rogapp/utils/location_controller.dart';
import 'package:rogapp/utils/text_util.dart';
import 'package:rogapp/widgets/base_layer_widget.dart';
import 'package:rogapp/widgets/bottom_sheet_new.dart';
import 'package:rogapp/widgets/current_position_widget.dart';
import 'package:rogapp/widgets/game_state_view.dart';
class MapWidget extends StatefulWidget {
@ -28,6 +30,7 @@ class _MapWidgetState extends State<MapWidget> {
final DestinationController destinationController =
Get.find<DestinationController>();
final LocationController locationController = Get.find<LocationController>();
StreamSubscription? subscription;
Timer? _timer;
@ -50,6 +53,7 @@ class _MapWidgetState extends State<MapWidget> {
DatabaseHelper db = DatabaseHelper.instance;
db.getDestinationByLatLon(des.lat!, des.lon!).then((value) {
destinationController.shouldShowBottomSheet = false;
showModalBottomSheet(
constraints:
BoxConstraints.loose(Size(Get.width, Get.height * 0.85)),
@ -60,6 +64,7 @@ class _MapWidgetState extends State<MapWidget> {
destination: des, isAlreadyCheckedIn: value.isNotEmpty))
//builder:((context) => BottomSheetWidget())
).whenComplete(() {
destinationController.shouldShowBottomSheet = true;
destinationController.skipGps = false;
});
});
@ -176,10 +181,10 @@ class _MapWidgetState extends State<MapWidget> {
}
void _centerMapOnUser() {
//print("showBottomSheet ${destinationController.shouldShowBottomSheet}");
if (destinationController.shouldShowBottomSheet) {
destinationController.centerMapToCurrentLocation();
}
print("showBottomSheet ${destinationController.shouldShowBottomSheet}");
//if (destinationController.shouldShowBottomSheet) {
destinationController.centerMapToCurrentLocation();
//}
}
@override
@ -246,7 +251,9 @@ class _MapWidgetState extends State<MapWidget> {
: Container(),
),
CurrentLocationLayer(
followOnLocationUpdate: FollowOnLocationUpdate.never,
positionStream: locationController
.locationMarkerPositionStreamController.stream,
alignDirectionOnUpdate: AlignOnUpdate.never,
turnOnHeadingUpdate: TurnOnHeadingUpdate.never,
style: const LocationMarkerStyle(
marker: DefaultLocationMarker(
@ -284,6 +291,7 @@ class _MapWidgetState extends State<MapWidget> {
],
)),
const Positioned(top: 0, left: 0, child: GameStateWidget()),
const Positioned(bottom: 10, right: 10, child: CurrentPosition())
// const Positioned(
// bottom: 10,
// left: 0,