简体   繁体   English

如何在 Flutter 的灵活小部件中使用 flex 参数?

[英]How to use flex parameter in Flexible widget in Flutter?

When there is excess space between widgets, the flex property works as expected.当小部件之间有多余的空间时, flex 属性会按预期工作。 As mentioned in the documentation, the formula to use is as follows如文档中所述,使用的公式如下

remainingSpace * (flex / totalOfAllFlexValues)

But when you have to shrink the widgets, the flex property stops making sense.但是当你必须缩小小部件时, flex 属性就不再有意义了。 Giving results like this.给出这样的结果。

Building on this, here the widgets overflow, so I use flex to shrink box 1 and 2在此基础上,这里的小部件溢出,所以我使用 flex 来缩小框 1 和 2

在此处输入图像描述

and while playing with the flex values, the following happens在使用 flex 值时,会发生以下情况

class Caja extends StatelessWidget {

  Color color;
  int numero;
  double ancho;

  Caja(this.color, this.numero, this.ancho);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: this.ancho,
      height: this.ancho,
      color: this.color,
      child: Center(
        child: Text(
          this.numero.toString(),
          style: TextStyle(
            fontSize: 30.0
          ),
        ),
      ),
    );
  }
}

I user Flexible widget below我在下面使用了灵活的小部件

 Column(
      crossAxisAlignment: CrossAxisAlignment.start,

      children: <Widget>[
          Flexible(flex: 1, child: Caja(Colors.blue, 1, 200.0)),

          Flexible(flex: 6, child: Caja(Colors.red, 2, 200.0)),

          Caja(Colors.green, 4, 400.0),
      ],
 )

在此处输入图像描述

Where does this new leftover space come from?这个新的剩余空间从何而来? Can someone explain to me?有人可以向我解释吗?

I changed the flex value of the second box to 1 - there was no white space.我将第二个框的 flex 值更改为 1 - 没有空格。 Then to 2 - there was no white space either.然后到 2 - 也没有空白。 From a flex value of 3 upwards, there appears a white space and it gets bigger and bigger.从 flex 值 3 向上,出现一个空白,并且越来越大。 The documentation states, that a该文件指出,一个

"..., Flexible does not require the child to fill the available space." “...,灵活不需要孩子填满可用空间。”

If you examine the source code of the Flexible, the comments say:如果您检查 Flexible 的源代码,评论会说:

"If non-zero, the amount of space the child's can occupy in the main axis is determined by dividing the free space (after placing the inflexible children) according to the flex factors of the flexible children." “如果非零,则孩子可以在主轴上占据的空间量是通过根据灵活孩子的弹性因子划分可用空间(放置不灵活孩子之后)来确定的。”

I think there might be a problem with the inflexible child - your green box.我认为不灵活的孩子可能有问题 - 你的绿盒子。 If you set the flex of your second box so high, you want your first box to be really small.如果您将第二个盒子的 flex 设置得这么高,那么您希望您的第一个盒子非常小。 But the flexible children just take the free space up to the inflexible child into consideration and not the free space after it.但是灵活的孩子只考虑到不灵活的孩子的自由空间,而不是它之后的自由空间。

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

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