Jonathon
Jonathon

Reputation: 1591

Error when adding container color: Incorrect use of ParentDataWidget

I'm trying to add a background color to my container, then maybe add some border radius with BoxDecoration(). When adding color: Colors.red to the container, it's throwing out:

Incorrect use of ParentDataWidget. Expanded widget must be placed directly inside Flex widgets.

Container(
  color: Colors.red,
  child: Expanded(
    child: ListView.builder(
      padding: EdgeInsets.all(0.0),
      itemCount: _clients.length,
      itemBuilder: (context, index) {
        final client = _clients[index];

        return Dismissible(
          key: Key(client),
          direction: DismissDirection.startToEnd,
          onDismissed: (direction) {
            setState(() {
              _clients.removeAt(index);
            });
          },
          background: Container(color: Color(0xff171e24)),
          child: _clientListTile(context, client)
        );
      }
    ),
  ),
)

Upvotes: 2

Views: 2540

Answers (1)

Anthony Sette
Anthony Sette

Reputation: 837

What this error is stating is that the Widget Expanded must be placed in a Flex widget. Flex widgets include rows, columns, etc. Think about whether you want to have multiple items inside the container and whether you want them to be horizontal or vertical.

A quick fix is to wrap the Expanded with a Row or Column, but it depends on what you want.

Here is an example using your code:

class Test extends StatelessWidget {

  final _clients = ["1","2","3","4","5"];

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child: Row(             //or column
        children: <Widget>[
          Expanded(
            child: ListView.builder(
              padding: EdgeInsets.all(0.0),
              itemCount: _clients.length,
              itemBuilder: (context, index) {
                final client = _clients[index];

                return Dismissible(
                  key: Key(client),
                  direction: DismissDirection.startToEnd,
                  onDismissed: (direction) {print(direction);},
                  background: Container(color: Color(0xff171e24)),
                  child: Text("Client: " + client)
                );
              }
            ),
          ),
          //add another item if you want its a row or column after all :-)
        ],
      )
    );
  }
}

Let me know if you have any questions!

EDIT

Since you said it was nested in a column try using Expanded() to stretch it across the Flex Widget.

Shown here:

class Test extends StatelessWidget {

  final _clients = ["1","2","3","4","5"];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget> [
        Expanded(         //RIGHT HERE
          child: Container(
            color: Colors.red,
            child: Row( //or column
              children: <Widget>[
                Expanded(
                  child: ListView.builder(
                    padding: EdgeInsets.all(0.0),
                    itemCount: _clients.length,
                    itemBuilder: (context, index) {
                      final client = _clients[index];

                      return Dismissible(
                        key: Key(client),
                        direction: DismissDirection.startToEnd,
                        onDismissed: (direction) {print(direction);},
                        background: Container(color: Color(0xff171e24)),
                        child: Text("Client: " + client)
                      );
                    },
                  ),
                ),
                //add another item if you want its a row
              ],
            ),
          ),
        ),
      ],
    );
  }
}

Upvotes: 2

Related Questions