【问题标题】:Expanded widget not taking entire width or height展开的小部件不占用整个宽度或高度
【发布时间】:2020-07-03 10:13:52
【问题描述】:

我正在学习 Flutter 中的扩展小部件。 我的理解是 Expanded 小部件采用整个宽度/高度取决于或 Row / Column

所以,这是我用于Row 的代码:

class DicePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Expanded(
          child: Image(
            image: AssetImage('images/dice1.png'),
          ),
        ),
      ],
    );
  }
}

这是输出:

它占据了整个屏幕的宽度,这很好。

但是,对于Column,这是我使用的:

class DicePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: Image(
            image: AssetImage('images/dice1.png'),
          ),
        ),
      ],
    );
  }
}

输出是:

为什么这里的整个高度不被图像拍摄?

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    我认为您的图像尺寸接近 1:1,因此扩展小部件会尽可能多地扩展它。试试这个来拉伸图像:

    child: Image(
                image: AssetImage('images/dice1.png'),
                fit: BoxFit.fill,
              ),
    

    【讨论】:

      【解决方案2】:

      在扩展小部件中使用 flex 属性

      Expanded(
          flex: 0,
      ),
      

      【讨论】:

        猜你喜欢
        • 2018-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-28
        相关资源
        最近更新 更多