Flutter 通过后台截图生成图片的方式实现高德地图变更maker图标颜色和size

高德地图flutter sdk制作标记点的maker,对于移动端不支持size和颜色变更,顾采用传递自制的uint8list来实现。

Future createImageFromWidget(Widget widget,
      {Duration? wait, required Size imageSize}) async {
    final RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();

    final RenderView renderView = RenderView(
      window: ui.window,
      child: RenderPositionedBox(alignment: Alignment.center, child: repaintBoundary),
      configuration: ViewConfiguration(
        size: imageSize,
        devicePixelRatio: 1.0,
      ),
    );

    final PipelineOwner pipelineOwner = PipelineOwner();
    final BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());

    pipelineOwner.rootNode = renderView;
    renderView.prepareInitialFrame();

    final RenderObjectToWidgetElement rootElement = RenderObjectToWidgetAdapter(
      container: repaintBoundary,
      child: widget,
    ).attachToRenderTree(buildOwner);

    buildOwner.buildScope(rootElement);

    if (wait != null) {
      await Future.delayed(wait);
    }

    buildOwner.buildScope(rootElement);
    buildOwner.finalizeTree();

    pipelineOwner.flushLayout();
    pipelineOwner.flushCompositingBits();
    pipelineOwner.flushPaint();

    final ui.Image image = await repaintBoundary.toImage(pixelRatio: 1.0);
    final ByteData byteData = (await image.toByteData(format: ui.ImageByteFormat.png))!;

    return byteData.buffer.asUint8List();
  }

你可能感兴趣的:(Flutter 通过后台截图生成图片的方式实现高德地图变更maker图标颜色和size)