简体   繁体   English

如何在 Flutter 中创建一个可滚动的列,如果它不占用所有空间,它会在某处扩展?

[英]How to create a scrollable Column in Flutter that if it doesn't take up all the space it expands somewhere?

I'm trying to create a scrollable column that has a widget at the bottom of the screen if it does not fill it up.我正在尝试创建一个可滚动的列,如果它没有填满,则在屏幕底部有一个小部件。 However, if it does it scrolls.但是,如果它滚动。

First I tried with a simple Column with MainAxisSize.max and Expand with an empty Container which worked well until the column overflowed.首先,我尝试使用一个带有MainAxisSize.max的简单Column并使用一个空Container进行Expand ,在该列溢出之前效果很好。

[...]

Column(
  mainAxisSize: MainAxisSize.max,
  children: [
    MyWidget(),
    MyWidget(),
    [...],
    MyWidget(),
    Expand(child: Container()),
    ElevatedButton()
  ],
)

Then I searched and found the recommended solution for making a Column scrollable is the SingleChildScrollView .然后我搜索并发现使Column可滚动的推荐解决方案是SingleChildScrollView This made all of my widgets in the column disappear (which I am still unsure why) until I removed the Expand which although solved one problem, introduced another.这使列中的所有小部件都消失了(我仍然不确定为什么),直到我删除了Expand ,它虽然解决了一个问题,但引入了另一个问题。

[...]

SingleChildScrollView(
  child: Column(
    mainAxisSize: MainAxisSize.min,
    children: [
      MyWidget(),
      MyWidget(),
      [...],
      MyWidget(),
      // Expand(child: Container()),
      ElevatedButton()
    ],
  )
)

So my question is, how can I make a Column both scrollable and if it does not need all the space to expand so that the last child is at the bottom of the screen?所以我的问题是,如何使 Column 既可滚动又不需要所有空间来扩展以便最后一个子项位于屏幕底部?

IntrinsicHeight is too expensive to use but in this situation you have no choice. IntrinsicHeight使用成本太高,但在这种情况下您别无选择。 Try this:尝试这个:

LayoutBuilder(builder: (context, constraint) {
    return SingleChildScrollView(
      child: ConstrainedBox(
        constraints: BoxConstraints(minHeight: constraint.maxHeight),
        child: IntrinsicHeight(
          child: Column(
            children: [
              Container(
                color: Colors.red,
                height: 400,
                width: double.infinity,
              ),
              Expanded(
                child: Container(
                  color: Colors.blue,
                  width: double.infinity,
                ),
              ),
            ],
          ),
        ),
      ),          
    );
  })

in this example if you set height for blue container you will see the scrollview works, and if you use Expanded on it, it takes the available size.在此示例中,如果您为蓝色container设置高度,您将看到scrollview有效,如果您在其上使用Expanded ,它会采用可用大小。

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

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