首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将FutureBuilder<File>图像转换为BoxDecoraiton图像

如何将FutureBuilder<File>图像转换为BoxDecoraiton图像
EN

Stack Overflow用户
提问于 2018-06-06 06:47:51
回答 1查看 4.1K关注 0票数 2

使用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 -属性。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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,
        )),
      );
    },
  );
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50709969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档