Flutter custom Google Map marker info window

后端 未结 6 2023
广开言路
广开言路 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 01:57

    You can display marker made of widgets as custom 'info window'. Basically you are creating png image of your widget and displaying it as a marker.

    import 'dart:typed_data';
    import 'dart:ui';
    
    import 'package:flutter/rendering.dart';
    import 'package:flutter/widgets.dart';
    
    class MarkerInfo extends StatefulWidget {
      final Function getBitmapImage;
      final String text;
      MarkerInfo({Key key, this.getBitmapImage, this.text}) : super(key: key);
    
      @override
      _MarkerInfoState createState() => _MarkerInfoState();
    }
    
    class _MarkerInfoState extends State {
      final markerKey = GlobalKey();
    
      void initState() {
        super.initState();
        WidgetsBinding.instance.addPostFrameCallback((_) => getUint8List(markerKey)
            .then((markerBitmap) => widget.getBitmapImage(markerBitmap)));
      }
    
      Future getUint8List(GlobalKey markerKey) async {
        RenderRepaintBoundary boundary =
            markerKey.currentContext.findRenderObject();
        var image = await boundary.toImage(pixelRatio: 2.0);
        ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
        return byteData.buffer.asUint8List();
      }
    
      @override
      Widget build(BuildContext context) {
        return RepaintBoundary(
          key: markerKey,
          child: Container(
            padding: EdgeInsets.only(bottom: 29),
            child: Container(
              width: 100,
              height: 100,
              color: Color(0xFF000000),
              child: Text(
                widget.text,
                style: TextStyle(
                  color: Color(0xFFFFFFFF),
                ),
              ),
            ),
          ),
        );
      }
    }
    

    If you use this approach you have to make sure you render the widget, because this will not work otherwise. For converting widgets to images - widget has to be rendered in order to convert it. I'm hiding my widget under the map in Stack.

    return Stack(
            children: [
              MarkerInfo(
                  text: tripMinutes.toString(),
                  getBitmapImage: (img) {
                    customMarkerInfo = img;
                  }),
              GoogleMap(
                markers: markers,
     ...
    

    Last step is to create a Marker. Data passed from the widget is saved in customMarkerInfo - bytes, so convert it to Bitmap.

    markers.add(
              Marker(
                position: position,
                icon: BitmapDescriptor.fromBytes(customMarkerInfo),
                markerId: MarkerId('MarkerID'),
              ),
            );
    

    Example

提交回复
热议问题