添加容器颜色时出错:不正确使用ParentDataWidget

时间:2019-07-15 23:52:06

标签: flutter dart

我正在尝试向容器中添加背景色,然后使用BoxDecoration()添加一些边框半径。在向容器添加颜色:Colors.red时,它会被丢弃:

  

不正确使用ParentDataWidget。展开的窗口小部件必须直接放在Flex窗口小部件内部。

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)
                    );
                  }
                ),
              ),
            )
  

不正确使用ParentDataWidget。展开的窗口小部件必须直接放在Flex窗口小部件内部。

1 个答案:

答案 0 :(得分:1)

此错误表示,必须将小部件Expanded放在Flex widget中。 Flex窗口小部件包括行,列等。请考虑是否要在容器中包含多个项目,以及要将它们设置为水平还是垂直。

一种快速的解决方法是用ExpandedRow包装Column,但这取决于您想要的内容。

以下是使用您的代码的示例:

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 :-)
        ],
      )
    );
  }
}

让我知道您是否有任何疑问!

编辑

由于您说过它嵌套在列中,请尝试使用Expanded()将其拉伸到Flex Widget上。

此处显示:

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
              ],
            ),
          ),
        ),
      ],
    );
  }
}