【问题标题】:How to get constraints like Height and Width of container in flutter如何在颤动中获得容器的高度和宽度等约束
【发布时间】:2018-03-30 19:55:52
【问题描述】:

我目前正在开发一个关于 Flutter 的聊天应用界面。我尝试使用以下容器自定义聊天消息,以在每条消息旁边显示一条垂直线,就像 Snapchat 一样:

child: new Container(
    margin: const EdgeInsets.symmetric(vertical: 10.0),
    child: new Row(
      mainAxisAlignment: MainAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        new Container(
          alignment: Alignment.centerRight,
          width: 300.0,
          child: new Text(text),
        ),
        new Container(width: 2.0, height: 10.0, color: Colors.amber, child: null)
      ],
    ),
  )

问题是,这个:

新容器(宽度:2.0,高度:10.0,颜色:Colors.amber,子项:null)

当我指定一个明确的高度时,比如上面的 10.0,它不会随消息缩放,它只是像这样停留在顶部:

所以我想知道是否有一种方法可以在消息文本的另一个容器高度增加时动态缩放行(容器)的高度。

【问题讨论】:

  • 您能否提供更多上下文并详细说明您的用例?
  • 好的,已编辑问题。对此感到抱歉

标签: dart flutter


【解决方案1】:

LayoutBuilder 是你想要的。

构建器委托接收一个 BoxConstraint 作为参数,对应于容器的大小。

【讨论】:

    【解决方案2】:

    虽然 Darky 的回答是正确的,但在您的情况下,您不需要知道容器尺寸。一个更简单的方法是在容器右侧放置一个 border

    例如:

    new Container(
        margin: const EdgeInsets.symmetric(vertical: 10.0),
        decoration: new BoxDecoration(
          border: new Border(
            right: new BorderSide(
              width: 2.0,
              color: Colors.amber,
            ),
          ),
        ),
        child: new Text('Hello World!'),
    );
    

    【讨论】:

    • 在过去的四个小时里,我一直为此头疼……谢谢
    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多