简体   繁体   English

Flutter。 列 mainAxisAlignment spaceBetween 不工作

[英]Flutter. Column mainAxisAlignment spaceBetween not working

I need to position the widgets at the top and bottom, as in this picture.我需要 position 顶部和底部的小部件,如图所示。

在此处输入图像描述

For this I am using the following structure:为此,我使用以下结构:

    Column(
       mainAxisAlignment: MainAxisAlignment.spaceBetween,
       children:[
         Column(
            children:[Container(), Container()] // top widgets
               ),
         Container() // bottom widget

             ])

But this does not work, my widgets are located strictly one under the other.但这不起作用,我的小部件严格位于另一个之下。 See also image:另见图片: 在此处输入图像描述

I tried to wrap the outer Column with an Expanded widget, but then I get this error:我试图用 Expanded 小部件包装外部 Column,但随后出现此错误:

RenderFlex children have non-zero flex but incoming width constraints are unbounded. RenderFlex 子项具有非零 flex,但传入的宽度约束是无界的。

When a row is in a parent that does not provide a finite width constraint, for example if it is in a horizontal scrollable, it will try to shrink-wrap its children along the horizontal axis.当一行位于不提供有限宽度约束的父行中时,例如,如果它在水平可滚动中,它将尝试沿水平轴收缩其子代。 Setting a flex on a child (eg using Expanded) indicates that the child is to expand to fill the remaining space in the horizontal direction.在子节点上设置 flex(例如使用 Expanded)表示子节点将展开以填充水平方向上的剩余空间。 These two directives are mutually exclusive.这两个指令是互斥的。 If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent.如果父级要收缩包裹其子级,则子级不能同时扩展以适应其父级。

Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded).考虑将 mainAxisSize 设置为 MainAxisSize.min 并为灵活的子项使用 FlexFit.loose(使用 Flexible 而不是 Expanded)。 This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.这将允许灵活的子级将自己的大小调整为小于他们将被迫占用的无限剩余空间,然后将导致 RenderFlex 收缩包裹子级,而不是扩展以适应父级提供的最大约束。

I also tried adding mainAxisSize: MainAxisSize.max for the Column but it didn't help.我还尝试为 Column 添加 mainAxisSize: MainAxisSize.max 但它没有帮助。

PS my outer Column is inside the Row, maybe this is the problem? PS我的外列在行内,也许这是问题所在?

My full structure:我的完整结构:

Card(
 child:Padding(
   ...,
   child:Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(... ),
              const SizedBox(width: 4),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: const [
                      Text(...),
                      Text(...),
                    ],
                  ),
                  Container(...)
                ],
              )
            ],
          )

The final screen I'm trying to get looks like this:我试图获得的最终屏幕如下所示: 在此处输入图像描述

Please help me请帮我

Just use spacer() between two Column只需在两Column之间使用spacer()

Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(child: Container(
            color: Colors.yellow,
            height: 50,
          ),),
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(child: Container(
                  color: Colors.green,
                  height: 50,
                ),),
                Expanded(
                  child: Column( children: [
                    Column(
                      children: [
                        Container(
                          color: Colors.green,
                          height: 50,
                        ),
                        Container(
                          color: Colors.blue,
                          height: 50,
                        ),
                      ], // top widgets
                    ),
                    Spacer(),
                    Container(
                      color: Colors.yellow,
                      height: 50,
                    ) // bottom widget
                  ]),
                )
              ],
            ),
          )
        ],
      )

output: output:

在此处输入图像描述

test this example测试这个例子

Column(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
      Expanded(
        flex: 3,
        child: Column(children: [
          Container(height: 100, color: Colors.green),
          Container(height: 100, color: Colors.red)
        ] // top widgets
            ),
      ),
      Container(height: 100, color: Colors.red) // bottom widget
    ]);

here you go !给你 go !

          Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
            Expanded(child: containerWithBorder),
            Expanded(
                child: Column(
              children: [
                containerWithBorder,
                containerWithBorder,
                Spacer(),
                containerWithBorder,
              ],
            )),
            Expanded(
                child: Column(
              children: [
                containerWithBorder,
                Spacer(),
              ],
            ))
          ]),

来自模拟器的图像

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

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