如何在Flutter中将小部件转换为背景图像?

时间:2019-06-24 06:47:49

标签: flutter flutter-layout

我知道有一种将窗口小部件导出到图像的方法:使用RepaintBoundary扭曲子窗口小部件,然后:

class PngHome extends StatefulWidget {
  PngHome({Key key}) : super(key: key);

  @override
  _PngHomeState createState() => _PngHomeState();
}

class _PngHomeState extends State<PngHome> {
  GlobalKey globalKey = GlobalKey();

  Future<void> _capturePng() async {
    RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    print(pngBytes);
  }

  @override
  Widget build(BuildContext context) {
    return RepaintBoundary(
      key: globalKey,
      child: Center(
        child: FlatButton(
          child: Text('Hello World', textDirection: TextDirection.ltr),
          onPressed: _capturePng,
        ),
      ),
    );
  }
}


但是这种方法需要首先“显示”该小部件。我只是想知道是否可以以更优雅的方式做到这一点,例如将小部件转换为背景图像而不是先“显示”该小部件?

0 个答案:

没有答案