134 lines
4.1 KiB
Dart
134 lines
4.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
|
import 'package:location/location.dart';
|
|
|
|
class MapContent extends StatefulWidget {
|
|
const MapContent({super.key});
|
|
|
|
@override
|
|
State<MapContent> createState() => _MapContentState();
|
|
}
|
|
|
|
class _MapContentState extends State<MapContent> {
|
|
Location locationController = Location();
|
|
|
|
final Completer<GoogleMapController> _mapController =
|
|
Completer<GoogleMapController>();
|
|
|
|
LatLng? currentPosition;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
getLocationUpdate();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(8.0),
|
|
child: Column(
|
|
children: [
|
|
TextField(
|
|
decoration: InputDecoration(
|
|
hintText: 'Rechercher un lieu...',
|
|
prefixIcon: Icon(Icons.search),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8.0),
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 8.0),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 50,
|
|
child: ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Theme.of(context).colorScheme.primary,
|
|
foregroundColor: Colors.white,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8.0),
|
|
),
|
|
),
|
|
onPressed: () {
|
|
// TODO : Logique de recherche à implémenter
|
|
},
|
|
child: Text('Chercher'),
|
|
),
|
|
),
|
|
SizedBox(height: 8.0),
|
|
Expanded(
|
|
child: currentPosition == null
|
|
? const Center(child: Text("Chargement..."))
|
|
: GoogleMap(
|
|
onMapCreated: (GoogleMapController controller) =>
|
|
_mapController.complete(controller),
|
|
initialCameraPosition: CameraPosition(
|
|
target: currentPosition!,
|
|
zoom: 14.4746,
|
|
),
|
|
markers: {
|
|
Marker(
|
|
markerId: MarkerId('currentLocation'),
|
|
position: currentPosition!,
|
|
icon: BitmapDescriptor.defaultMarkerWithHue(
|
|
BitmapDescriptor.hueAzure,
|
|
),
|
|
),
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _cameraToPosition(LatLng pos) async {
|
|
final GoogleMapController controller = await _mapController.future;
|
|
CameraPosition newCameraPosition = CameraPosition(target: pos, zoom: 13);
|
|
await controller.animateCamera(
|
|
CameraUpdate.newCameraPosition(newCameraPosition),
|
|
);
|
|
}
|
|
|
|
Future<void> getLocationUpdate() async {
|
|
bool serviceEnabled;
|
|
PermissionStatus permissionGranted;
|
|
|
|
serviceEnabled = await locationController.serviceEnabled();
|
|
if (serviceEnabled) {
|
|
serviceEnabled = await locationController.requestService();
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
permissionGranted = await locationController.hasPermission();
|
|
if (permissionGranted == PermissionStatus.denied) {
|
|
permissionGranted = await locationController.requestPermission();
|
|
if (permissionGranted != PermissionStatus.granted) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
locationController.onLocationChanged.listen((LocationData currentLocation) {
|
|
if (currentLocation.latitude != null &&
|
|
currentLocation.longitude != null) {
|
|
setState(() {
|
|
currentPosition = LatLng(
|
|
currentLocation.latitude!,
|
|
currentLocation.longitude!,
|
|
);
|
|
_cameraToPosition(currentPosition!);
|
|
print(currentPosition);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|