flutter - 添加容器颜色时出错: Incorrect use of ParentDataWidget

标签 flutter dart flutter-layout flutter-listview

我正在尝试向我的容器添加背景颜色,然后可能使用 BoxDecoration() 添加一些边框半径。将颜色:Colors.red 添加到容器时,它会抛出:

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

最佳答案

此错误表明小部件 Expanded 必须放置在 Flex 小部件 中。 Flex 小部件包括行、列等。考虑一下您是否希望容器内有多个项目以及您希望它们水平还是垂直。

一个快速解决方法是用 RowColumn 包装 Expanded,但这取决于您想要的内容。

这是使用您的代码的示例:

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() 将其拉伸(stretch)到 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
              ],
            ),
          ),
        ),
      ],
    );
  }
}

关于flutter - 添加容器颜色时出错: Incorrect use of ParentDataWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57048419/

相关文章:

dart - 如何生成6位随机数

android - flutter 中的响应式用户界面

dart - 使用 InternetAddress.ANY_IP_V4 作为动态地址参数的 bindSecure()

mobile - Flutter - 通知 Column 内的兄弟小部件

flutter - 行和列对齐而不是填充抖动

flutter 'Unhandled Exception: type ' 列表<动态 >' is not a subtype of type ' 列表<类>'

flutter - CircleAvatar 中的淡入图像

list - 如何在 flutter 中将字符串列表转换为字符串?

dart - 配置 AngularDart 模块

android - 如何在 flutter 中绘制自定义动态高度弯曲形状?