gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:56:08 27 4
gpt4 key购买 nike

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

最佳答案

此错误说明的是 Widget Expanded 必须放置在 Flex widget 中。 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com