Flutter custom Google Map marker info window

后端 未结 6 2020
广开言路
广开言路 2020-12-31 01:24

I am working on Google Map Markers in Flutter.

On the click of each Marker, I want to show a Custom Info Window which can include a button, image etc. But in Flutte

6条回答
  •  半阙折子戏
    2020-12-31 02:01

    To create a widget-based info window you need to stack the widget on google map. With the help of ChangeNotifierProvider, ChangeNotifier, and Consumer you can easily rebuild your widget even when the camera moves on google map.

    InfoWindowModel class:

    class InfoWindowModel extends ChangeNotifier {
      bool _showInfoWindow = false;
      bool _tempHidden = false;
      User _user;
      double _leftMargin;
      double _topMargin;
    
      void rebuildInfoWindow() {
        notifyListeners();
      }
    
      void updateUser(User user) {
        _user = user;
      }
    
      void updateVisibility(bool visibility) {
        _showInfoWindow = visibility;
      }
    
      void updateInfoWindow(
        BuildContext context,
        GoogleMapController controller,
        LatLng location,
        double infoWindowWidth,
        double markerOffset,
      ) async {
        ScreenCoordinate screenCoordinate =
            await controller.getScreenCoordinate(location);
        double devicePixelRatio =
            Platform.isAndroid ? MediaQuery.of(context).devicePixelRatio : 1.0;
        double left = (screenCoordinate.x.toDouble() / devicePixelRatio) -
            (infoWindowWidth / 2);
        double top =
            (screenCoordinate.y.toDouble() / devicePixelRatio) - markerOffset;
        if (left < 0 || top < 0) {
          _tempHidden = true;
        } else {
          _tempHidden = false;
          _leftMargin = left;
          _topMargin = top;
        }
      }
    
      bool get showInfoWindow =>
          (_showInfoWindow == true && _tempHidden == false) ? true : false;
    
      double get leftMargin => _leftMargin;
    
      double get topMargin => _topMargin;
    
      User get user => _user;
    }
    

    Complete Example is available on my blog!

提交回复
热议问题