简体   繁体   English

扩展的小部件如何在颤振中工作?

[英]How expanded widget works in flutter?

I have a simple code which produces design like this我有一个简单的代码,它产生这样的设计

设计 This is my code这是我的代码

This is my code for the design:这是我的设计代码:

class InputPage extends StatefulWidget {
  @override
  _InputPageState createState() => _InputPageState();
}

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BMI CALCULATOR'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: ReusableCard(colour: Color(0xff1d1e33),)
                ),
                Expanded(
                  child: ReusableCard(colour: Color(0xff1d1e33),)
                ),
              ],
            ),
          ),
          Expanded(
            child: ReusableCard(colour: Color(0xff1d1e33)),
          ),
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: ReusableCard(colour: Color(0xff1d1e33),)
                ),
                Expanded(
                  child: ReusableCard(colour: Color(0xff1d1e33),)
                ),
              ],
            ),
          ),
          Container(
            width: double.infinity,
            height: 100,
            color: Colors.pink,
          )
        ],
      ),
    );
  }
}

Now when I remove the parent expanded widget, I get a design like this:现在,当我删除父扩展小部件时,我得到了这样的设计:

设计

Here is the code for this design:这是此设计的代码:

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BMI CALCULATOR'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Row(
            children: [
              Expanded(
                child: ReusableCard(colour: Color(0xff1d1e33),)
              ),
              Expanded(
                child: ReusableCard(colour: Color(0xff1d1e33),)
              ),
            ],
          ),
          Expanded(
            child: ReusableCard(colour: Color(0xff1d1e33)),
          ),
          Row(
            children: [
              Expanded(
                child: ReusableCard(colour: Color(0xff1d1e33),)
              ),
              Expanded(
                child: ReusableCard(colour: Color(0xff1d1e33),)
              ),
            ],
          ),
          Container(
            width: double.infinity,
            height: 100,
            color: Colors.pink,
          )
        ],
      ),
    );
  }
}

What is the reason for this?这是什么原因? Even if I remove for the parent I have added expanded for the child, so shouldn't it be the same?即使我为父删除了我为孩子添加的扩展,所以它不应该是一样的吗?

ReusableCard:可重复使用的卡:

class ReusableCard extends StatelessWidget{
  final Color colour;
  ReusableCard({@required this.colour});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(15),
      decoration: BoxDecoration(
          color: colour,
          borderRadius: BorderRadius.circular(20)
      ),
    );
  }
}

Basically The Expanded widget will expand to all of the remaining height the parent widget have,基本上扩展小部件将扩展到小部件具有的所有剩余高度,

So you in the first code, in your body inside the column we have 4 widgets所以你在第一个代码中,在你的身体里,我们有 4 个小部件

      Expanded(
        child: Row(
        ),
      ),
      Expanded(
        child: ReusableCard(colour: Color(0xff1d1e33)),
      ),
      Expanded(
        child: Row(
        ),
      ),
      Container(
        width: double.infinity,
        height: 100,
        color: Colors.pink,
      )

So lets say that the height of the parent widget is 1000, the height of the container at the end is fixed so it will always take 100, so 900 is left所以假设父小部件的高度是 1000,最后容器的高度是固定的,所以它总是需要 100,所以剩下 900

The three expanded widget in the column will take the remaining 900, and because you did not specify any flex property in the Expanded widget all of them will receive same height so 900/3 = 300 for each widget.列中的三个展开的小部件将占用剩余的 900,并且由于您没有在展开的小部件中指定任何 flex 属性,所有这些小部件都将获得相同的高度,因此每个小部件的 900/3 = 300。

in the second code inside the column you have在您拥有的列内的第二个代码中

      Row(
      ),
      Expanded(
        child: ReusableCard(colour: Color(0xff1d1e33)),
      ),
      Row(
      ),
      Container(
        width: double.infinity,
        height: 100,
        color: Colors.pink,
      )

again assuming the height available is 1000, the container is fixed height of 100 so 900 is left, now the expanded widget will take all of the available height which is 900 because the rows don't have any fixed height.再次假设可用高度为 1000,容器的固定高度为 100,因此还剩 900,现在展开的小部件将采用所有可用高度,即 900,因为行没有任何固定高度。

the Expanded widget inside the Rows will make the card inside them expand to the parent height, but the parent row does not have any height (0 height) so it will expand to this 0 height which is useless. Rows 内的 Expanded 小部件将使其中的卡片扩展到父高度,但父行没有任何高度(0 高度),因此它将扩展到这个 0 高度,这是无用的。

always keep it in mind that the Expanded will take the height or width from its parent widget.请始终记住, Expanded 将从其父小部件获取高度或宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM