简体   繁体   English

将 Widget 转换为 Image.png 并保存在创建的目录中 Flutter

[英]Convert Widget to Image.png and Save it In Created Directory Flutter

I want my app when install first thing create specific directory in internal storage and a button when clicked it's convert specific widget(screen) to image.png then save it in that created directory.我希望我的应用程序在安装时首先在内部存储中创建特定目录,并在单击时创建一个按钮,将特定小部件(屏幕)转换为 image.png,然后将其保存在该创建的目录中。

I need full code for that.我需要完整的代码。 I've been looking, but I haven't found an effective way.我一直在寻找,但我还没有找到有效的方法。

this function is all your need for screenshot a widget and save locally:此功能是您对小部件进行截图并保存在本地的全部需要:

static GlobalKey _repaintKey = GlobalKey();

Widget _yourWidget(){
  return Stack(
    key: _repaintKey,
    children: [
    ],
  );
}

Future<void> _takeScreenShot(context) async {
    RenderRepaintBoundary boundary = _repaintKey.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    final path = join(
      (await getTemporaryDirectory()).path, "screenshot${DateTime.now().toIso8601String()}.png");
    File imgFile = File(path);
    imgFile.writeAsBytes(pngBytes).then((value) {
      Navigator.of(context).pushNamed(Routes.uploadImage, arguments: value.uri.path);
    }).catchError((onError) {
      print(onError);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM