我有一个堆栈小部件,里面有两个小部件. 一张画背景,另一张画在上面. 我希望两个小工具都有相同的大小.

我想要顶部的小部件,它是一个列,将堆栈垂直扩展并填充到背景小部件设置的大小.

我怎么才能让它工作呢?

推荐答案

Use Positioned.fill

    Stack(
      children: [
        Positioned.fill(
          child: Column(
            children: [
              //...
            ],
          ),
        ),
        //...
      ],
    );

更多关于Positioned in Flutter Widget of the Week的信息

它怎麽工作?

This is all about constraints. Constraints are min/max width/height values that are passed from the parent to its children. In the case of Stack. The constraints it passes to its children state that they can size themselves as small as they want, which in the case of some widgets means they will be their "natural" size. After being sized, Stack will place its children in the top left corner.
Positioned.fill gets the same constraints from Stack but passes different constraints to its child, stating the it (the child) must be of exact size (which meant to fill the Stack).

Positioned.fill()等于:

Positioned(
  top: 0,
  right: 0,
  left: 0,
  bottom:0,
  child: //...
)

For even more examples and info: How to fill a Stack widget and why? and Understanding constraints.

Flutter相关问答推荐

如何更改大纲按钮的边框 colored颜色 和文本 colored颜色

为什么小部件会在其父文件中重建,但放在单独文件中时却不会重建?

ProviderContainer和GoRouter

如何在FiRestore中正确编写OR查询

我的flutter代码显示一个小部件被认为是死代码,为什么?

Flutter 应用程序中的Firebase实时数据库中的orderByChild()不适用于我

设置Flutter 中的ListView.Builder()的最小高度

确保通过在CheckIfFavoriteMovieEvent((event,emit){...})上注册处理程序误差

将列表<;Events>;从Flutter 应用程序中的函数传回EventLoader属性时出错

try 使用fl_Chart of Ffltter,但在导入它时遇到以下错误:Error:';TextScaler';is;t a type

Firebase WHERE过滤器在日期字段中不起作用

为什么用户界面不会在Flight中的复选框中更改?

Flutter:我的应用程序是否需要包含退款购买?

点击按钮后重新启动定时器

如何在Flutter中显示ListView Builder区域外的列表数据?

Flutter Firebase 升级后出现问题? ARC 语义问题 (Xcode): Select 器appleCredentialWithIDToken:rawNonce:fullName:没有已知的类方法

如何在 GridView.builder 中设置 textform 字段并在 flutter 中将 12 个 textformfield 作为单个字符串获取?

为什么将 bloc 提供程序放在单独的文件/类中会引发错误?

单元测试中如何比较NaN和NaN是否相等

在 Android 12+ 中,REST API 调用在 flutter 中非常慢,在 WiFi 中需要 10 秒,而在移动数据中不到一秒