1

我正在尝试使用资产图像/网络图像创建自定义标记。我拍摄该图像并创建一个小部件以进行必要的样式设置,然后尝试调用返回MarkerGenerator的List<Uint8List> MarkerGenerator。但我得到了Uint8List没有资产图像。我认为资产图像的渲染在调用 MarkerGenerator 之前还没有完成。渲染assetImage完成后如何调用MarkerGenerator。

import 'dart:typed_data';
import 'marker_helper.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class MyMarkerPage extends StatefulWidget {
    @override
    State<StatefulWidget> createState() {
        return _MyMarkerPageState();
    }
}

class _MyMarkerPageState extends State<MyMarkerPage> {
    Uint8List imageInMemory;
    List<Uint8List> markerImage;
    @override
    void initState() {
        super.initState();
        List<Widget> widgetList = new List();
        for (int i = 0; i < 1; i++) {
            widgetList.add(
            Container(
                height: 50,
                width: 50,
                padding: EdgeInsets.all(10),
                color: Colors.green,
                child: Image.asset("assets/myImage.jpg"),
            ),
            );
         }
         MarkerGenerator(widgetList, (bitmaps) {
             setState(() {
                 markerImage = bitmaps;
             });
         }).generate(context);
     }

     @override
     Widget build(BuildContext context) {
         return new Scaffold(
             appBar: new AppBar(
                 title: new Text('Widget To Image'),
             ),
             body: SingleChildScrollView(
                 child: Center(
                     child: new Column(
                         mainAxisAlignment: MainAxisAlignment.center,
                         children: <Widget>[
                         markerImage != null
                         ? Container(
                         child: Image.memory(markerImage[0]),
                         margin: EdgeInsets.all(10))
                         : Container(),
                         ],
                     ),
                 ),
             ),
         );
     }
}

4

1 回答 1

1

解决方案是将 MarkerGenerator.generate 内容包装在 Future.delayed 中,延迟时间为 50 毫秒。您也可以尝试其他时间。

Future.delayed(Duration(milliseconds: 50), () {
    WidgetsBinding.instance
          .addPostFrameCallback((_) => afterFirstLayout(context));
});
于 2020-01-23T12:58:07.183 回答