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

Mar*_*ary 20 containers image stretch flutter

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

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

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

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,
  ],
);
Run Code Online (Sandbox Code Playgroud)

Rém*_*let 35

错误的方法在这里.

请改用DecorativeBox.或者Container的属性decoration/foregroundDecoration(然后使用DecoratedBox).

FittedBox(
  child: Image.asset('foo.png'),
  fit: BoxFit.fill,
)
Run Code Online (Sandbox Code Playgroud)

  • Ty,我有我认为更新的版本,我做了:_SizedBox.expand(child: Image.asset("assets/bg.png", fit: BoxFit.fill))_ (2认同)

Jos*_*ush 22

以下将使图像适合容器宽度的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"),
    ),
  ),
)
Run Code Online (Sandbox Code Playgroud)

图像填充模式:

  • 切勿使用 MediaQuery 使小部件填充其父级。那不是你想要的 (5认同)

Mar*_*ary 18

在Stack中,你应该将你的background小部件包装在Positioned.fill中.

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


CNK*_*CNK 13

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

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


Dmi*_*huk 11

可能不是 OP 正在寻找的内容,但是这个页面是我在查找问题后发现自己的地方,因此与遇到类似问题的每个人共享此页面:)

Stack 的fit属性满足了我的需求。否则里面的图像(OctoImageIn my case)被填充并且提供其他Image.fit值没有产生任何效果。

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


Swi*_*Pro 10

您的问题包含第一步,但您需要宽度和高度。你可以得到屏幕的宽度和高度。这是一个小编辑

//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,
  ],
);
Run Code Online (Sandbox Code Playgroud)

您还可以使用 Width 和 Height 根据屏幕大小调整其他对象的大小。

前任: width: Height/2, height: Height/2 //using height for both keeps aspect ratio

  • Image 上的“fit: BoxFit.fill”简直是救星,谢谢:) (2认同)

小智 8

我在此页面上找到的这个问题的最佳示例: https ://flutterbeads.com/set-background-image-in-flutter/

通过使用 BoxDecoration 和 DecorationImage:

Container(
  constraints: BoxConstraints.expand(),
  decoration: const BoxDecoration(
    image: DecorationImage(
        image: AssetImage("assets/images/cat2.jpg"), 
        fit: BoxFit.cover),
  )
Run Code Online (Sandbox Code Playgroud)


M_L*_*ude 6

我认为就您的目的而言,Flex可以比 Container() 更好地工作:

new Flex(
    direction: Axis.vertical,
    children: <Widget>[
      Image.asset(asset.background)
    ],
   )
Run Code Online (Sandbox Code Playgroud)


小智 5

这应该有效,

Image.asset('assets/bg.jpg',fit: BoxFit.cover,),
Run Code Online (Sandbox Code Playgroud)