Flutter Positioned widget results in an error: Incorrect use of ParentDataWidget

Viewed 3233

The following code results in an error: Incorrect use of ParentDataWidget. The cause for this error is the Positioned widget, but I'm not sure why...

return Scaffold(
      body: Container(
        color: Colors.red,
        child: Positioned(
          left: 32.0,
          child: Container(
            width: 128.0,
            height: 128.0,
            color: Colors.yellow,
          ),
        ),
      ),
    );
1 Answers

Positioned has to be in a Stack according to the documentation that says

A widget that controls where a child of a Stack is positioned.

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).

return Scaffold(
      body: Container(
        color: Colors.red,
        child: Stack(
          children: <Widget>[
            Positioned(
              left: 32.0,
              child: Container(
                width: 128.0,
                height: 128.0,
                color: Colors.yellow,
              ),
            ),
          ],
        ),
      ),
    );
Related