此错误说明的是 WidgetExpanded
必须放在Flex widget
. Flex 小部件包括行、列等。考虑您是否希望容器内有多个项目,以及您希望它们是水平的还是垂直的。
一个快速的解决方法是Expanded
用Row
or包裹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
],
),
),
),
],
);
}
}