如何解决Flutter 显示本地图像或资产
我建立了一个用户个人资料页面,您可以在其中更改个人资料图片。默认图片在资产中。从画廊或相机中选择的图片存储在本地。所有这些都有效。我显示个人资料图片的代码如下所示:
Container(
width: 140.0,height: 140.0,decoration: new BoxDecoration(
shape: BoxShape.circle,image: new DecorationImage(
image: new FileImage(File(_image.path)),//image: new ExactAssetImage(_image.path),fit: BoxFit.cover,),))
FileImage 正确显示本地图像。 ExactAssetImage 正确显示默认资产图像。我的问题:如何检查本地图片是否存在并显示它或使用默认资产图片?
更新:
我像这样初始化我的 _image:
File _image = File('assets/images/user_profile_static.png');
选择新图像后,我使用以下代码将其保存在本地:
void _imgFromGallery() async {
final imageFile = await ImagePicker().getImage(source: ImageSource.gallery);
if (imageFile == null) return;
Directory directory = await getApplicationDocumentsDirectory();
File tmpFile = File(imageFile.path);
tmpFile = await tmpFile.copy('${directory.path}/my_profile.jpg');
setState(() {
_image = tmpFile;
});
}
因此在下次访问页面时,如果 my_profile.jpg 存在,则显示它,否则显示默认图像。
解决方法
我认为这应该可行
Image(image: FileImage(image)??AssetImage(assetName),),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。