【问题标题】:Positioned widgets must be placed directly inside Stack widgets定位的小部件必须直接放置在 Stack 小部件内
【发布时间】:2018-05-23 10:06:08
【问题描述】:

我正在尝试使用其中的图像和文本来实现 gridview。我想要黑色背景图像底部的文本。这是我的ListItem代码

class ListItem extends StatelessWidget {
    String url;
    String name;

    ListItem(this.url, this.name);
    @override
    Widget build(BuildContext context) {

        return new Container(
            child: new Column(
            children: <Widget>[
                new Image.network('${url}', fit: BoxFit.cover),
                new Positioned(
                    child: new Container(
                         child: new Text('${name}',
                         style: new TextStyle(fontSize: 20.0, color: Colors.white)),
                    decoration: new BoxDecoration(color: Colors.black),
                    padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0)),
                left: 0.0,
                bottom: 108.0,
           )
        ],
     ));
    }
 }

这段代码显示错误

Positioned widgets must be placed directly inside Stack widgets.
Positioned(no depth, dirty) has a Stack ancestor, but there are other widgets between them:
- Column(direction: vertical, mainAxisAlignment: start, crossAxisAlignment: center)

【问题讨论】:

    标签: flutter flutter-layout flutter-positioned


    【解决方案1】:

    问题出在Column,在这里和那里更改了几行后,我终于发现这是因为Column

    一旦我将Column 更改为Stack,它就可以正常工作了。

    return new Container(
            child: new Stack(
    

    【讨论】:

      【解决方案2】:

      我们昨天刚刚讨论过这个问题。 Positioned 实际上不仅可以在 Stack 中使用,因此文档对此并不完全正确。它不能用于任何渲染,文档对 RenderObjectWidget 非常具体:

      “定位小部件必须是堆栈的后代,并且从定位小部件到其封闭堆栈的路径必须仅包含无状态小部件或有状态小部件(而不是其他类型的小部件,如 RenderObjectWidgets)。

      来源:https://docs.flutter.io/flutter/widgets/Positioned-class.html

      列来自 RenderObjectWidget: ... Widget > RenderObjectWidget > MultiChildRenderObjectWidget > Flex > Column

      大多数刚开始使用 Flutter 的人只知道 StatelessWidget 和 StatefulWidget,但还有其他一些,了解它们有时非常重要。

      小部件:
      有状态的小部件
      无状态小部件
      渲染对象小部件
      代理小部件
      PreferredSizeWidget

      更多信息: https://docs.flutter.io/flutter/widgets/Widget-class.html

      【讨论】:

        猜你喜欢
        • 2018-10-19
        • 2020-07-03
        • 1970-01-01
        • 2019-07-21
        • 1970-01-01
        • 2020-08-29
        • 2020-09-09
        • 2019-01-20
        • 2022-08-05
        相关资源
        最近更新 更多