【问题标题】:Allowing a widget to overflow to another widget允许一个小部件溢出到另一个小部件
【发布时间】:2018-03-01 12:14:52
【问题描述】:

我试图实现允许一个小部件溢出到另一个小部件的效果
here所见@

到目前为止我的代码是这样的:

  @override
  Widget build(BuildContext context) {
    Size screenSize = MediaQuery.of(context).size;
    return new Column(children: <Widget>[
      new Container(
      color: Colors.blue,
      height: screenSize.height / 2,
      width: screenSize.width,
      child: new Center(
        child: new Container(
          margin: const EdgeInsets.only(top: 320.0),
          color: Colors.red,
          height: 40.0,
          width: 40.0,
        ),
      ))
]);
}

这导致following,正方形被容器截断了。
还有其他方法吗?

【问题讨论】:

标签: flutter


【解决方案1】:

一般来说,你应该永远不要在flutter中溢出。

如果你想要一个特定的布局,你需要在不同的层上明确地分离“溢出”小部件。

一个解决方案是Stack 小部件,它允许小部件彼此之上。

到底要实现这个:

颤振代码是

new Stack(
  fit: StackFit.expand,
  children: <Widget>[
    new Column(
      mainAxisSize: MainAxisSize.max,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        new Expanded(
          child: new Container(
            color: Colors.blue,
          ),
        ),
        new Expanded(
          child: new Container(
            color: Colors.white,
          ),
        ),
      ],
    ),
    new Center(
      child: new Container(
        color: Colors.red,
        height: 40.0,
        width: 40.0,
      ),
    )
  ],
);

【讨论】:

  • 有什么办法可以把屏幕底部的容器拿走?我尝试了alignment,但似乎我可能必须增加Container 的高度才能看到效果。
  • 我没有得到你想要的。你能提出一个问题吗?我很乐意回答
  • IMO,这只是一件小事,评论可以完成工作。无论如何,让我澄清一下,在上面的屏幕截图中,我们可以看到红色的Container 位于屏幕中间,我们如何对齐该特定容器以显示在屏幕的中心底部(这意味着在Column的第二个容器的白色容器的中心底部)
  • 将中心替换为与alignment 对齐为Alignment.bottomCenter。您也可以完全移除 Center 并直接在 Stack 上设置对齐方式
  • 也看看 LayoutBuilder。如果你有复杂的布局,想要按照当前的大小进行布局,LayoutBuilder加Stack就很方便了。
猜你喜欢
  • 2020-02-17
  • 2020-02-22
  • 2012-08-05
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-09-01
  • 2019-12-17
相关资源
最近更新 更多