【问题标题】:Stack wrap_content all the Widget inside?堆wrap_content里面的所有Widget?
【发布时间】:2020-11-14 22:40:24
【问题描述】:

我有一个这样的 Widget 树:

  Stack(
    children: [
      Text(
        '-',
        style: title,
      ),
      Positioned.fill(
        child: Center(
          child: Text(
            'FT',
            style: normal,
          ),
        ),
      )
    ],
  ),

我希望堆栈将宽度和高度都包装到其中的所有小部件,如下所示:

但它看起来像这样:

堆栈从未填充的小部件中获取宽度和高度。 如果我将两个 Widget 都包装在 Positioned.fill 中,则布局为空。 有人知道怎么修这个东西吗?非常感谢。

【问题讨论】:

    标签: flutter widget flutter-layout flutter-widget stacklayout


    【解决方案1】:

    您可以使用Stack 小部件的alignment 属性

    试试这个

         Stack(
                alignment: Alignment.center,
                children: [
    
                  Text(
                    'GAME',
                    style: TextStyle(color: Colors.blue),
                  ),
                  Text(
                    '  -  ',
                    style: TextStyle(color: Colors.red),
                  ),
                ],
              ),
    

    输出

    【讨论】:

      【解决方案2】:

      根据docs

      堆栈本身的大小以包含所有未定位的子项

      因此,基于此,我们需要使用非定位子级,尽管它仍然可以与Align 或类似Center 的等价物对齐。

      Stack(
        children: [
          Center(
            child: Text(
              '  -  ',
            ),
          ),
          Center(
            child: Text(
              'FT',
            ),
          ),
        ],
      );
      

      【讨论】:

        猜你喜欢
        • 2017-11-27
        • 2020-04-05
        • 2021-12-19
        • 1970-01-01
        • 2021-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多