MotorSignal-App/lib/features/map/map_screen.dart

79 lines
2.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import '../../services/location_service.dart';
class MapScreen extends ConsumerStatefulWidget {
const MapScreen({super.key});
@override
ConsumerState<MapScreen> createState() => _MapScreenState();
}
class _MapScreenState extends ConsumerState<MapScreen> {
GoogleMapController? _mapController;
MapType _currentMapType = MapType.normal;
@override
Widget build(BuildContext context) {
final locationAsync = ref.watch(locationStreamProvider);
// Position is handled inside the when block
return Scaffold(
appBar: AppBar(
title: const Text('Map'),
actions: [
IconButton(
icon: Icon(
_currentMapType == MapType.normal
? Icons.satellite
: Icons.map,
),
onPressed: () {
setState(() {
_currentMapType = _currentMapType == MapType.normal
? MapType.satellite
: MapType.normal;
});
},
),
],
),
body: locationAsync.when(
data: (position) {
final currentLatLng = LatLng(position.latitude, position.longitude);
if (_mapController != null) {
_mapController!.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: currentLatLng,
zoom: 17.0,
bearing: position.heading,
),
),
);
}
return GoogleMap(
initialCameraPosition: CameraPosition(
target: currentLatLng,
zoom: 17.0,
),
mapType: _currentMapType,
myLocationEnabled: true,
myLocationButtonEnabled: true,
compassEnabled: true,
onMapCreated: (controller) {
_mapController = controller;
},
);
},
loading: () => const Center(child: CircularProgressIndicator()),
error: (error, stack) => Center(child: Text('Error: $error')),
),
);
}
}