【问题标题】:How to have a Stack widget constrain itself to a Positioned child in Flutter?如何让 Stack 小部件将自己约束到 Flutter 中的定位子项?
【发布时间】:2018-12-05 19:12:29
【问题描述】:

在这段代码中,我尝试在 Stack 小部件中绘制一个 Text 小部件。但是,堆栈小部件不会根据定位的小部件调整自身大小,因此文本会被截断。

如何定位文本同时仍会影响 Stack 的大小?

return Column(
  children: <Widget>[
    Stack(
      children: <Widget>[
        Container(height:20, width: 20, color: Colors.green,),
        Positioned(
          top: 10,
          child: Text('Does not render because the stack is only 20x20'),
        )
      ],
    ),
    Container(height:40, width: 40, color: Colors.red,)
  ],
);

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    如文档中所述“堆栈大小本身包含所有未定位的子项,这些子项根据对齐方式进行定位(默认为从左到右环境中的左上角和右上角在从右到左的环境中)。”,因此我们不能将堆栈本身约束到定位的小部件。但我认为将溢出设置为 Overflow.visible 对你有用。

    return Column(
      children: <Widget>[
        Stack(
          overflow: Overflow.visible,
          children: <Widget>[
            Container(height:20, width: 20, color: Colors.green,),
            Positioned(
              top: 10,
              child: Text('Does not render because the stack is only 20x20'),
            )
          ],
        ),
        Container(height:40, width: 40, color: Colors.red,)
      ],
    );
    

    【讨论】:

    【解决方案2】:

    Overflow 属性已被弃用,因此现在使用此属性:

     clipBehavior: Clip.none,
    

    例子:

    return Column(
      children: <Widget>[
        Stack(
          clipBehavior: Clip.none,
          children: <Widget>[
            Container(height:20, width: 20, color: Colors.green,),
            Positioned(
              top: 10,
              child: Text('Does not render because the stack is only 20x20'),
            )
          ],
        ),
        Container(height:40, width: 40, color: Colors.red,)
      ],
    );
    

    【讨论】:

      猜你喜欢
      • 2020-10-10
      • 2022-11-03
      • 2020-09-29
      • 2019-02-24
      • 2021-09-09
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多