1

我是 Flutter 的新手,我正在构建一个应用程序,我需要将使用 image_picker 包后生成的文件(图像)转换为资产图像以在应用程序中使用。

示例代码如下,创建文件(图像)

final Function onSelectImage;

ImageInput(this.onSelectImage);

File _storedImage;


Future<void> _takePicture() async {

        final imageFile = await ImagePicker.pickImage(
                        source: ImageSource.camera,
                        maxWidth: 600,
        );

        if (imageFile == null) {
                return;
        }

        setState(() {
                _storedImage = imageFile;
        });

        final appDir = await syspaths.getApplicationDocumentsDirectory();

        final fileName = path.basename(imageFile.path);

        final savedImage = await imageFile.copy('${appDir.path}/$fileName');

        widget.onSelectImage(savedImage);
}

提前致谢

4

1 回答 1

0

您可以创建一个图像变量,您可以在选择图像时恢复和更新该变量。

请参阅以下代码:

final Function onSelectImage;

ImageInput(this.onSelectImage);

File _storedImage;

Image _tempImage;


Future<void> _takePicture() async {

        final imageFile = await ImagePicker.pickImage(
                        source: ImageSource.camera,
                        maxWidth: 600,
        );

        if (imageFile == null) {
                return;
        }

        setState(() {
                _storedImage = imageFile;
        });

        final appDir = await syspaths.getApplicationDocumentsDirectory();

        final fileName = path.basename(imageFile.path);

        final savedImage = await imageFile.copy('${appDir.path}/$fileName');

        widget.onSelectImage(savedImage);
        setState(() {
          _tempImage = imageFile;
        });


}


  @override
  Widget build(BuildContext context) {
    return _tempImage == null ? Container(child:null) : Image(image: _tempImage);
  }
于 2019-09-20T08:28:10.983 回答