Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioned widgets must be placed directly inside Stack widgets

I am trying to implement gridview with image and a text inside it. where i want text at the bottom of image with black background. Here is my code for 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,
           )
        ],
     ));
    }
 }

With this code it is showing error

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)
like image 220
Ravi Avatar asked May 23 '18 10:05

Ravi


2 Answers

Issue was with Column, after changing few lines from here and there i finally found that it was because of Column

Once i change Column to Stack, it works fine.

return new Container(
        child: new Stack(
like image 119
Ravi Avatar answered Oct 17 '22 20:10

Ravi


We were just discussing this yesterday. Positioned can actually be used in more than just a Stack, so the docs aren't exactly right about that. It can't be used in anything that renders, and the docs are very specific about RenderObjectWidget:

"A Positioned widget must be a descendant of a Stack, and the path from the Positioned widget to its enclosing Stack must contain only StatelessWidgets or StatefulWidgets (not other kinds of widgets, like RenderObjectWidgets).

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

Column is descended from RenderObjectWidget: ... Widget > RenderObjectWidget > MultiChildRenderObjectWidget > Flex > Column

Most people starting out in Flutter are only aware of StatelessWidget and StatefulWidget, but there are others and knowing them can be very important at times.

Widget:
     StatefulWidget
     StatelessWidget
     RenderObjectWidget
     ProxyWidget
     PreferredSizeWidget

More at: https://docs.flutter.io/flutter/widgets/Widget-class.html

like image 4
scottstoll2017 Avatar answered Oct 17 '22 19:10

scottstoll2017