如何防止水平 ListView 项目拉伸
how to prevent horizontal ListView items from stretching
我在 Column 中有一个水平的 ListView。
我的 ListView 正在渲染 100 x 100 个项目,我想稍后将它们的高度设置为 200。
我将我的 ListView 包装在一个高度为 200 的容器中,否则我会出错并且代码不会 运行
现在的问题是列表项将它们的高度拉伸到 200。
有没有办法防止这种行为,我尝试对齐但没有用。
这是到目前为止的代码
Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: 200,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
CCard(),
CCard(),
CCard(),
CCard(),
CCard(),
],
),
),
],
));
和列表项
class CCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 8.0),
height: 100,
width: 100,
color: Colors.red[200],
);
}
}
尝试将 Container
包裹在 Align
或 Center
内,像这样:
class CCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: EdgeInsets.symmetric(horizontal: 8.0),
height: 100,
width: 100,
color: Colors.red[200],
),
);
}
}
更多信息:https://docs.flutter.io/flutter/widgets/Container-class.html
我在 Column 中有一个水平的 ListView。
我的 ListView 正在渲染 100 x 100 个项目,我想稍后将它们的高度设置为 200。
我将我的 ListView 包装在一个高度为 200 的容器中,否则我会出错并且代码不会 运行
现在的问题是列表项将它们的高度拉伸到 200。 有没有办法防止这种行为,我尝试对齐但没有用。 这是到目前为止的代码
Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: 200,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
CCard(),
CCard(),
CCard(),
CCard(),
CCard(),
],
),
),
],
));
和列表项
class CCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 8.0),
height: 100,
width: 100,
color: Colors.red[200],
);
}
}
尝试将 Container
包裹在 Align
或 Center
内,像这样:
class CCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
margin: EdgeInsets.symmetric(horizontal: 8.0),
height: 100,
width: 100,
color: Colors.red[200],
),
);
}
}
更多信息:https://docs.flutter.io/flutter/widgets/Container-class.html