如何在Flutter中拉伸图像以适应整个背景(100%高度x 100%宽度)?

时间:2017-08-17 21:49:38

标签: image containers flutter stretch

我的图像与设备屏幕的宽高比不匹配。我想拉伸图像以使其完全填满屏幕,我不想裁剪图像的任何部分。

CSS具有百分比的概念,因此我可以将高度和宽度设置为100%。但看起来Flutter似乎没有这个概念,只是硬编码高度和宽度是不好的,所以我被卡住了。

这就是我所拥有的(我正在使用堆栈,因为我在图像的前景中有一些东西):

Widget background = new Container(
  height: // Not sure what to put here!
  width: // Not sure what to put here!
  child: new Image.asset(
    asset.background,
    fit: BoxFit.fill, // I thought this would fill up my Container but it doesn't
  ),
);

return new Stack(
  children: <Widget>[
    background,
    foreground,
  ],
);

16 个答案:

答案 0 :(得分:19)

要使图像填充其父级,只需将其包装到JSON.stringify

SizedBox.expand

SizedBox.expand( child: Image.assert('foo.png'), fit: BoxFit.fill, ) 这里将拉伸图像以填充空间。

或者,对于复杂装饰,您可以使用BoxFit.fill代替Container - 并使用Image / decoration字段。

要使foregroundDecoration成为其父级,它应该是:

  • 没有孩子
  • Container属性不是alignment

这是一个将两个图片和一个null组合在一个Text中,同时占据其父级宽度/高度100%的示例:

enter image description here

Container

答案 1 :(得分:10)

以下将使图像适合容器宽度的100%,而高度不变。 对于本地资产,请使用AssetImage

Container(
  width: MediaQuery.of(context).size.width,
  height: 100,
  decoration: BoxDecoration(
    image: DecorationImage(
      fit: BoxFit.fill,
      image: NetworkImage("https://picsum.photos/250?image=9"),
    ),
  ),
)

图像填充模式:

  • 填充-图片被拉伸

    fit: BoxFit.fill
    

    enter image description here


  • 适合高度-图像保持比例,同时确保显示了图像的整个高度(可能会溢出)

    fit: BoxFit.fitHeight
    

    enter image description here


  • 适合宽度-图像保持成比例,同时确保显示了图像的整个宽度(可能会溢出)

    fit: BoxFit.fitWidth
    

    enter image description here


  • 盖子-图片保持比例,确保最大程度地覆盖容器(可能会溢出)

    fit: BoxFit.cover
    

    enter image description here


  • 包含-图像保持比例,并尽可能缩小,如果需要显示整个图像,则将减小其大小

    fit: BoxFit.contain
    

    enter image description here

答案 2 :(得分:6)

在您的堆栈中,您应该将background小部件包装在Positioned.fill

return new Stack(
  children: <Widget>[
    new Positioned.fill(
      child: background,
    ),
    foreground,
  ],
);

答案 3 :(得分:6)

对我来说,要为网络开发,以下工作正常:

Image(
  image: AssetImage('lib/images/portadaSchamann5.png'),
  alignment: Alignment.center,
  height: double.infinity,
  width: double.infinity,
  fit: BoxFit.fill,
),

答案 4 :(得分:3)

可能不完全是OP所要查找的内容,但是此页面是我在查找问题后发现的地方,因此,请与有类似问题的所有人共享此内容:)

Stack的fit属性满足了我的需求。否则,将填充内部图像(在我的情况下为OctoImage),并且提供其他Image.fit值不会产生任何效果。

Stack(
  fit: StackFit.expand, 
  children: [
    Image(
      image: provider,
      fit: BoxFit.cover,
    ),
    // other irrelevent children here
  ]
);

答案 5 :(得分:2)

以上所有答案均不适用于我。并且由于没有可接受的答案,我发现以下内容将我的图像从水平边缘扩展到了水平边缘:

val doc = Document().append("\$regex", ".*" + Pattern.quote("Med") + ".*")

doc.append("\$options", "i")

val docQuery = Document().append("title", doc)

val query = coll.find(docQuery).limit(10)

答案 6 :(得分:2)

我将容器的宽度和高度设置为 double.infinity,如下所示:

Container(
        width: double.infinity,
        height: double.infinity,
        child: //your child
)

答案 7 :(得分:1)

我认为,Flex可以比Container()更好地工作:

new Flex(
    direction: Axis.vertical,
    children: <Widget>[
      Image.asset(asset.background)
    ],
   )

答案 8 :(得分:1)

对我来说,在有界容器中使用Image(fit: BoxFit.fill ...)可以正常工作。

答案 9 :(得分:1)

我在使用 FittedBox 时遇到了问题,因此我将图像包裹在 LayoutBuilder 中:

LayoutBuilder( 
   builder: (_, constraints) => Image(
      fit: BoxFit.fill,
      width: constraints.maxWidth,
      image: AssetImage(assets.example),
   ),
)

这很有效,我建议你试一试。
当然你可以用高度代替宽度,这正是我用的。

答案 10 :(得分:0)

为了填充,我有时使用SizedBox.expand

答案 11 :(得分:0)

尝试设置contentPadding

ListTile(
  contentPadding: EdgeInsets.all(0.0),
  ...
)

答案 12 :(得分:0)

您的问题包含第一步,但您需要宽度和高度。您可以获得屏幕的宽度和高度。这是一个小修改

//gets the screen width and height
double Width = MediaQuery.of(context).size.width;
double Height = MediaQuery.of(context).size.height;

Widget background = new Image.asset(
  asset.background,
  fit: BoxFit.fill,
  width: Width,
  height: Height,
);

return new Stack(
  children: <Widget>[
    background,
    foreground,
  ],
);

您还可以使用“宽度”和“高度”来根据屏幕尺寸调整其他对象的尺寸。

例如:width: Height/2, height: Height/2 //using height for both keeps aspect ratio

答案 13 :(得分:0)

访问https://youtu.be/TQ32vqvMR80

例如,如果父约束对象的身高为200,则

<Box display={{ xs: 'none',sm:"block"}}>
  <Row direction={"row-reverse"} divider={true} />
</Box>

答案 14 :(得分:0)

这对我有用

class _SplashScreenState extends State<SplashScreen> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: FittedBox(
        child: Image.asset("images/my_image.png"),
        fit: BoxFit.fill,
      ),);
  }
}

答案 15 :(得分:0)

这应该可行,

Image.asset('assets/bg.jpg',fit: BoxFit.cover,),
相关问题