【Flutter】Widget 轉(zhuǎn) Image,實(shí)現(xiàn)高德地圖Marker自定義,大坑

目的是為了實(shí)現(xiàn) 高德地圖Marker自定義,坑爹的只支持 本地素材 和 Uint8List,所以只能轉(zhuǎn)換了。

網(wǎng)上搜 Widget 轉(zhuǎn) Image,基本都是需要在 build 內(nèi)部實(shí)現(xiàn),然后賦值一個(gè)key,生成 Uint8List。也就是將已經(jīng)顯示到頁(yè)面上的 Widget 轉(zhuǎn)成圖片。
而地圖的 Marker 需要在加載數(shù)據(jù)庫(kù)時(shí)執(zhí)行,顯然不可行。

直接搜“高德地圖Marker自定義”,還能搜到幾篇文章?;径际峭粋€(gè)答案,代碼如下:

  static Future<ByteData> widgetToImage(
    Widget widget, {
    Alignment alignment = Alignment.center,
    Size size = const Size(double.maxFinite, double.maxFinite),
    double devicePixelRatio = 1.0,
    double pixelRatio = 1.0,
  }) async {
    RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();

    RenderView renderView = RenderView(
      child: RenderPositionedBox(alignment: alignment, child: repaintBoundary),
      configuration: ViewConfiguration(
        size: size,
        devicePixelRatio: devicePixelRatio,
      ),
      view: WidgetsBinding.instance.platformDispatcher.views.first,
    );

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

    BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());
    RenderObjectToWidgetElement rootElement = RenderObjectToWidgetAdapter(
      container: repaintBoundary,
      child: widget,
    ).attachToRenderTree(buildOwner);
    buildOwner.buildScope(rootElement);
    buildOwner.finalizeTree();

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

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

    return byteData!;
  }

然后直接使用就會(huì)成這樣,樣式根本顯示不出來


最終真是皇天不負(fù)苦心人,找到了坑。

問題點(diǎn)在這里:

調(diào)用的時(shí)候,一定要手動(dòng)給于 Size 大小

    var byteData = await MapImageUtil.widgetToImage(widget, size: const Size(300, 400));
    return BitmapDescriptor.fromBytes(byteData.buffer.asUint8List());

真的是坑啊

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容