2023-08-27 07:07:35 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2023-12-05 21:34:37 +02:00
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2023-08-27 07:07:35 +02:00
|
|
|
import 'package:flutter_map/plugin_api.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2023-11-09 18:10:56 +02:00
|
|
|
import 'package:immich_mobile/modules/map/providers/map_state.provider.dart';
|
2023-12-05 21:34:37 +02:00
|
|
|
import 'package:immich_mobile/modules/map/utils/map_controller_hook.dart';
|
2023-08-27 07:07:35 +02:00
|
|
|
import 'package:latlong2/latlong.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
|
|
|
// A non-interactive thumbnail of a map in the given coordinates with optional markers
|
|
|
|
class MapThumbnail extends HookConsumerWidget {
|
|
|
|
final Function(TapPosition, LatLng)? onTap;
|
|
|
|
final LatLng coords;
|
|
|
|
final double zoom;
|
|
|
|
final List<Marker> markers;
|
|
|
|
final double height;
|
2023-12-05 21:34:37 +02:00
|
|
|
final double width;
|
2023-08-27 07:07:35 +02:00
|
|
|
final bool showAttribution;
|
|
|
|
final bool isDarkTheme;
|
|
|
|
|
|
|
|
const MapThumbnail({
|
|
|
|
super.key,
|
|
|
|
required this.coords,
|
2023-12-05 21:34:37 +02:00
|
|
|
this.height = 100,
|
|
|
|
this.width = 100,
|
2023-08-27 07:07:35 +02:00
|
|
|
this.onTap,
|
|
|
|
this.zoom = 1,
|
|
|
|
this.showAttribution = true,
|
|
|
|
this.isDarkTheme = false,
|
|
|
|
this.markers = const [],
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2023-12-05 21:34:37 +02:00
|
|
|
final mapController = useMapController();
|
|
|
|
final isMapReady = useRef(false);
|
2023-11-09 18:10:56 +02:00
|
|
|
ref.watch(mapStateNotifier.select((s) => s.mapStyle));
|
2023-08-27 07:07:35 +02:00
|
|
|
|
2023-12-05 21:34:37 +02:00
|
|
|
useEffect(
|
|
|
|
() {
|
|
|
|
if (isMapReady.value && mapController.center != coords) {
|
|
|
|
mapController.move(coords, zoom);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
[coords],
|
|
|
|
);
|
|
|
|
|
2023-08-27 07:07:35 +02:00
|
|
|
return SizedBox(
|
|
|
|
height: height,
|
2023-12-05 21:34:37 +02:00
|
|
|
width: width,
|
2023-08-27 07:07:35 +02:00
|
|
|
child: ClipRRect(
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(15)),
|
|
|
|
child: FlutterMap(
|
2023-12-05 21:34:37 +02:00
|
|
|
mapController: mapController,
|
2023-08-27 07:07:35 +02:00
|
|
|
options: MapOptions(
|
|
|
|
interactiveFlags: InteractiveFlag.none,
|
|
|
|
center: coords,
|
|
|
|
zoom: zoom,
|
|
|
|
onTap: onTap,
|
2023-12-05 21:34:37 +02:00
|
|
|
onMapReady: () => isMapReady.value = true,
|
2023-08-27 07:07:35 +02:00
|
|
|
),
|
|
|
|
nonRotatedChildren: [
|
|
|
|
if (showAttribution)
|
|
|
|
RichAttributionWidget(
|
|
|
|
animationConfig: const ScaleRAWA(),
|
|
|
|
attributions: [
|
|
|
|
TextSourceAttribution(
|
|
|
|
'OpenStreetMap contributors',
|
|
|
|
onTap: () => launchUrl(
|
|
|
|
Uri.parse('https://openstreetmap.org/copyright'),
|
2023-11-09 18:10:56 +02:00
|
|
|
mode: LaunchMode.externalApplication,
|
2023-08-27 07:07:35 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
children: [
|
2023-11-09 18:10:56 +02:00
|
|
|
ref.read(mapStateNotifier.notifier).getTileLayer(isDarkTheme),
|
2023-08-27 07:07:35 +02:00
|
|
|
if (markers.isNotEmpty) MarkerLayer(markers: markers),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|