当前位置 博文首页 > “Allen Su”的博客:【Flutter 常见问题】Flutter 如何设置图片

    “Allen Su”的博客:【Flutter 常见问题】Flutter 如何设置图片

    作者:[db:作者] 时间:2021-07-07 18:43

    我平常用到的一共有三种方式,个人经常使用第一种,详情以及最终效果图请继续往下看。

    方式一

    通过 ConstrainedBox 的 constraints 属性设置图片充满全屏

    ConstrainedBox(
      constraints: BoxConstraints.expand(), // 自适应屏幕
      child: Image.asset(
        "assets/images/girl.jpeg",
         fit: BoxFit.cover,
      ),
    )
    

    方式二

    通过设置 Container 的 width 和 height 为屏幕的宽高,让图片充满全屏

    Container(
      width: MediaQuery.of(context).size.width, // 屏幕宽度
      height: MediaQuery.of(context).size.height, // 屏幕高度
      child: Image.asset(
        "assets/images/girl.jpeg",
         fit: BoxFit.cover,
      ),
    )
    

    方式三

    如果你用了层级组件 Stack ,你也可以这样设置

    Stack(
      children: [
        Positioned.fill(
          child: Image.asset(
            "assets/images/girl.jpeg",
             fit: BoxFit.cover,
          ),
        ),
      ],
    )
    

    三种方式的效果图如下所示

    cs