2

我正在尝试为我的容器添加背景颜色,然后可能会使用 BoxDecoration() 添加一些边框半径。将 color: 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)
        );
      }
    ),
  ),
)
4

1 回答 1

2

此错误说明的是 WidgetExpanded必须放在Flex widget. Flex 小部件包括行、列等。考虑您是否希望容器内有多个项目,以及您希望它们是水平的还是垂直的。

一个快速的解决方法是ExpandedRowor包裹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
              ],
            ),
          ),
        ),
      ],
    );
  }
}
于 2019-07-16T00:30:26.433 回答