【问题标题】:Stack paints Container, but not DecoratedBox堆栈油漆容器,但不是 DecoratedBox
【发布时间】:2019-08-10 14:12:05
【问题描述】:

我只是想在堆栈中显示一个带有其他小部件的红色框,但是当我使用 DecoratedBox 而不是 Container 时,它不会在屏幕上绘制。我认为 DecoratedBox 只是一个更明确的 Container 版本,我错过了什么?

Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        DecoratedBox( // doesn't work
          decoration: BoxDecoration(
            color: Colors.red,
          ),
        ),
      ],
    );
  }

【问题讨论】:

    标签: android ios flutter dart flutter-layout


    【解决方案1】:

    Stack 不渲染 DecoratedBox 的原因是它没有默认大小。另一方面,Container 默认是它的父级。

    Container 不仅仅是DecoratedBox。它还使用其他小部件,包括SizedBoxAlignment

    这样:

    Container(
      color: Colors.red,
    )
    

    不等于:

    DecoratedBox(
      decoration: BoxDecoration(color: Colors.red),
    )
    

    但改为:

    SizedBox.expand(
      child: DecoratedBox(
        decoration: BoxDecoration(color: Colors.red),
      ),
    )
    

    【讨论】:

    • 谢谢!没想到这么快就得到了答案。我想我对DecoratedBox的用法没有一个清晰的认识。它应该只与指定宽度和高度的父 Widget 一起使用吗?当我可以在Container 中指定decoration 时,为什么还要使用DecoratedBox?对于额外的问题,我们深表歉意。
    • Container 使用DecoratedBox。所以它不是“一个或另一个”。在这两种情况下,您都使用DecoratedBox
    猜你喜欢
    • 2022-08-22
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多