import 'dart:async'; import 'dart:typed_data'; import 'package:enum_to_string/enum_to_string.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'package:manager_api/api.dart'; import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart' as mapBox; import 'package:provider/provider.dart'; import 'package:tablet_app/Components/multi_select_container.dart'; import 'package:tablet_app/Models/map-marker.dart'; import 'package:tablet_app/Screens/Map/map_context.dart'; import 'package:html/parser.dart' show parse; import 'package:tablet_app/Screens/Map/map_view.dart'; import 'package:tablet_app/constants.dart'; class MapBoxView extends StatefulWidget { final MapDTO? mapDTO; final Uint8List? selectedMarkerIcon; final String? language; const MapBoxView({ Key? key, this.mapDTO, this.selectedMarkerIcon, this.language, }) : super(key: key); @override _MapBoxViewState createState() => _MapBoxViewState(); } class AnnotationClickListener extends mapBox.OnPointAnnotationClickListener { late List markersList; late MapContext mapContext; AnnotationClickListener({ required this.markersList, required this.mapContext, }); @override void onPointAnnotationClick(mapBox.PointAnnotation annotation) { try{ var markerToShow = markersList.firstWhere((ml) => ml.id.toString() == annotation.textField); mapContext.setSelectedMarker(markerToShow); } catch(e) { print("ISSSUE setSelectedMarker"); print(e); } } } class _MapBoxViewState extends State { late mapBox.MapboxMap? mapboxMap; late mapBox.PointAnnotationManager? pointAnnotationManager; createPoints() { var options = []; pointsToShow!.forEach((point) { var textSansHTML = parse(point.title!.firstWhere((translation) => translation.language == widget.language).value); var mapMarker = new MapMarker( id: point.id, title: parse(textSansHTML.body!.text).documentElement!.text, description: point.description!.firstWhere((translation) => translation.language == widget.language).value, longitude: point.longitude, latitude: point.latitude, contents: point.contents ); markersList.add(mapMarker); print(widget.selectedMarkerIcon); options.add(mapBox.PointAnnotationOptions( geometry: mapBox.Point( coordinates: mapBox.Position( double.tryParse(mapMarker.longitude!)!, double.tryParse(mapMarker.latitude!)!, )).toJson(), iconSize: 1.3, textField: mapMarker.id.toString(), textOpacity: 0.0, iconOffset: [0.0, 0.0], symbolSortKey: 10, iconColor: 0, iconImage: null, image: widget.selectedMarkerIcon, //widget.selectedMarkerIcon, )); // , }); print(options.length); return options; } _onMapCreated(mapBox.MapboxMap mapboxMap, MapContext mapContext) { this.mapboxMap = mapboxMap; mapboxMap.annotations.createPointAnnotationManager().then((pointAnnotationManager) async { this.pointAnnotationManager = pointAnnotationManager; pointAnnotationManager.createMulti(createPoints()); pointAnnotationManager.addOnPointAnnotationClickListener(AnnotationClickListener(mapContext: mapContext, markersList: markersList)); init = true; }); } ConfigurationDTO? configurationDTO; //Completer _controller = Completer(); //Set markers = {}; List? pointsToShow = []; List? selectedCategories = []; bool init = false; @override void initState() { pointsToShow = widget.mapDTO!.points; selectedCategories = widget.mapDTO!.categories!.map((categorie) => categorie.label!.firstWhere((element) => element.language == widget.language).value!).toList(); super.initState(); } @override void dispose() { // TODO: implement dispose super.dispose(); } @override Widget build(BuildContext context) { final mapContext = Provider.of(context); Size size = MediaQuery.of(context).size; var type = mapBox.MapboxStyles.STANDARD; if(widget.mapDTO!.mapTypeMapbox != null) { switch(widget.mapDTO!.mapTypeMapbox!) { case MapTypeMapBox.standard: type = mapBox.MapboxStyles.STANDARD; break; case MapTypeMapBox.streets: type = mapBox.MapboxStyles.MAPBOX_STREETS; break; case MapTypeMapBox.outdoors: type = mapBox.MapboxStyles.OUTDOORS; break; case MapTypeMapBox.light: type = mapBox.MapboxStyles.LIGHT; break; case MapTypeMapBox.dark: type = mapBox.MapboxStyles.DARK; break; case MapTypeMapBox.satellite: type = mapBox.MapboxStyles.SATELLITE; break; case MapTypeMapBox.satellite_streets: type = mapBox.MapboxStyles.SATELLITE_STREETS; break; } } return Stack( children: [ Center( child: mapBox.MapWidget( key: ValueKey("mapBoxWidget"), styleUri: type, onMapCreated: (maBoxMap) { _onMapCreated(maBoxMap, mapContext); }, onTapListener: (listener) { // close on tap mapContext.setSelectedMarker(new MapMarker(longitude: null, latitude: null, title: '', contents: null, description: '')); }, cameraOptions: mapBox.CameraOptions( center: mapBox.Point(coordinates: mapBox.Position(4.865105, 50.465503)).toJson(), //TODO add element in manager // MDLF 50.416639, 4.879169 / Namur 50.465503, 4.865105 zoom: widget.mapDTO!.zoom != null ? widget.mapDTO!.zoom!.toDouble() : 12), ) ), Positioned( bottom: 35, left: 10, child: SizedBox( width: size.width * 0.55, child: MultiSelectContainer( label: null, color: kBackgroundGrey, initialValue: selectedCategories!, isMultiple: true, values: widget.mapDTO!.categories!.map((categorie) => categorie.label!.firstWhere((element) => element.language == widget.language).value!).toList(), onChanged: (value) { var tempOutput = new List.from(value); print(tempOutput); if(init) { setState(() { selectedCategories = tempOutput; pointsToShow = widget.mapDTO!.points!.where((point) => tempOutput.any((tps) => point.categorie?.label?.firstWhere((lab) => lab.language == widget.language).value == tps) || point.categorie == null).toList(); //markers = getMarkers(widget.language, mapContext); pointAnnotationManager!.deleteAll(); pointAnnotationManager!.createMulti(createPoints()); mapContext.notifyListeners(); }); } }, ), ), ), ], ); } }