【问题标题】:Convert Widget to Image.png and Save it In Created Directory Flutter将 Widget 转换为 Image.png 并将其保存在创建的目录中 Flutter
【发布时间】:2021-12-11 09:08:07
【问题描述】:

我希望我的应用在安装时首先在内部存储中创建特定目录,然后单击按钮将特定小部件(屏幕)转换为 image.png,然后将其保存在创建的目录中。

我需要完整的代码。我一直在寻找,但没有找到有效的方法。

【问题讨论】:

标签: image flutter directory save internal-storage


【解决方案1】:

这个功能是您截取小部件并保存在本地所需的全部:

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);
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2020-03-09
    • 1970-01-01
    • 2017-04-18
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多