使用flutter插件image_picker 0.4.4,我可以使用以下内容显示相机或图库中的图像
Widget _previewImageBkg() {
return FutureBuilder<File>(
future: _imageFileBkg,
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if (snapshot.connectionState == ConnectionState.done && snapshot.data != null) {
print('_previewImage.........check callback for this image. .>>>>>');
final File file = snapshot.data;
myBgURL = uploadFile('user@image.com_B.jpg', file);
return Image.file(snapshot.data);
} else if (snapshot.error != null) {
return const Text(
'Error picking image.',
textAlign: TextAlign.center,
);
} else {
return const Text(
'You have not yet picked an image.',
textAlign: TextAlign.center,
);
}
}
)
如果将图像放入容器中,则图像显示正常
return Scaffold(
appBar: AppBar(
title: Text('Set Profile Images'),
),
body: Center(
child: new Container(
width: screen.width,
height: 250.0,
child: _previewImageBkg()),
),
但是,我想将其显示在图像中(BoxDecoration:...)
child: new Container(
width: screenSize.width,
height: 275.0,
decoration: new BoxDecoration(
image: new DecorationImage(
image: (_previewImageBkg()== null ? new ExactAssetImage('images/nav_header_bg.png') : _previewImageBkg()),
fit: BoxFit.cover,
),
),
),
/flutter (29415): type 'FutureBuilder<File>' is not a subtype of type 'ImageProvider<dynamic>'
如何将此文件转换为Image?更大的目标是获得BoxDecoration的- fit: BoxFit.cover -属性。
发布于 2018-06-06 10:20:22
像这样使用FileImage
来构建你的装饰容器--关联的Future必须返回一个文件:
@override
Widget build(BuildContext context) {
return new FutureBuilder<File>(
future: _imageFileBkg,
builder: (context, snapshot) {
if (!snapshot.hasData) return Container(); // or some other placeholder
return new Container(
width: screenSize.width,
height: 275.0,
decoration: new BoxDecoration(
image: new DecorationImage(
image: new FileImage(snapshot.data),
fit: BoxFit.cover,
)),
);
},
);
}
https://stackoverflow.com/questions/50709969
复制相似问题