Sunshine
Sunshine

Reputation: 334

Change active marker appearance - flutter google maps

I'm using the flutter GoogleMap widget in my app to which I am adding markers :

Generated by mapping my bloc state.locationPoints


I've tried doing so using setState but it changes all of my markers appearances while I only want the current one to be changed

BlocBuilder<LocationBloc, LocationState>(
      builder: (context, state) {
        var markers = <Marker>{};
        if (state is LocationLoaded) {
          markers = state.locationPoints!.map((locationPoint) {
            return Marker(
              onTap: () {
            
              },
              icon: customMarker(locationPoint),
              position: LatLng(
                locationPoint.coordinates.latitude,
                locationPoint.coordinates.longitude,
              ),
            );
          }).toSet();
        }

        return GoogleMap(
          markers: markers,
        );
      },
    );

Upvotes: 1

Views: 1045

Answers (1)

B0r1
B0r1

Reputation: 470

You have to find the specific marker in your set of markers. If you provide your marker with an ID that contains some information from locationPoint, you could do something like this (in my case I use my place id as markerId):

  final markers = state.markers
    .map(
      (marker) => marker.markerId.value == state.selectedPlace.id
          ? marker.copyWith(
              iconParam: state.saveSelectedMarkerIcon,
            )
          : marker,
    )
    .toSet();

Upvotes: 1

Related Questions