flutter - Listview 在右侧显示溢出问题
问题描述
我有下面的代码来显示 2 张卡片并尝试进行水平移动。Listview 已经到位。轴方向是水平的。
右侧仍然存在溢出问题。也试过收缩包装,但没有用。
问题可以在飞镖垫上轻松复制:https ://dartpad.dartlang.org/dart ?只需复制以下代码。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(body: HomeScreen()),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Container(
margin: EdgeInsets.symmetric(horizontal: 10),
width: double.infinity,
child: Column(
children: [
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Text("Text 1", style: TextStyle(fontWeight: FontWeight.bold)),
Text("Text 2", style: TextStyle(fontWeight: FontWeight.bold))
]),
ListView(
scrollDirection: Axis.horizontal,
children: [
Card(
child: Container(
height: 230,
width: 170,
child: Column(
children: [
Container(
height: 170,
width: 150,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/user.png"))),
)
],
),
))
],
)
],
),
));
}
}
解决方案
您在您的内部添加了column
和,然后将两者都添加到您的这是错误的方法,因此您只需先将其全部分开。因此,我更改了您的代码并添加了还包装您的列表视图以提供高度和边距。您可以检查以下答案row
listview
cards
row widget
scrollDirection
Container
第一个答案
SafeArea(
child: Container(
height: 230,
margin: EdgeInsets.symmetric(horizontal: 10),
width: double.infinity,
child: ListView(scrollDirection: Axis.horizontal, children: [
Card(
child: Container(
height: 230,
width: 170,
//color: Colors.blue,
child: Column(
children: [
Container(
height: 170,
width: 150,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://cdn.searchenginejournal.com/wp-content/uploads/2018/09/shutterstock_1138149659-760x400.png"))),
)
],
),
)),
Card(
child: Container(
height: 230,
width: 180,
//color: Colors.blue,
child: Column(
children: [
Container(
height: 170,
width: 160,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://cdn.searchenginejournal.com/wp-content/uploads/2018/09/shutterstock_1138149659-760x400.png"))),
)
],
),
))
])))
第二个答案
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Container(
margin: EdgeInsets.symmetric(horizontal: 10),
width: double.infinity,
child: Column(
children: [
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Text("Text 1", style: TextStyle(fontWeight: FontWeight.bold)),
Text("Text 2", style: TextStyle(fontWeight: FontWeight.bold))
]),
Container(
height: 230,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
Card(
child: Container(
height: 230,
width: 170,
child: Column(
children: [
Container(
height: 170,
width: 150,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://cdn.searchenginejournal.com/wp-content/uploads/2018/09/shutterstock_1138149659-760x400.png"))),
)
],
),
))
],
),
),
],
),
));
}
}
推荐阅读
- mongodb - 在 MongoDb 中创建嵌套聚合集
- javascript - 下拉列表不起作用...未从数据库添加到列表的值以及选择显示输出的值以供显示
- javascript - 颜色选择器组件在 Vuetify 模态中不像在整页上那样工作
- ethereum - 元掩码处理错误
- javascript - 替换当前值后求平均值
- html - Google Analytics - 我可以为不同的页面添加一些唯一标识符吗?
- sql - 正则表达式替换字符串模式
- javascript - 根据反应式编程启动和停止计数器
- docker - 在 Docker 容器中计算时间戳(在 ECS 中运行)并传递给 AWS CLI DynamoDB Put-Item
- r - 如何在折叠其他列时按唯一行值对列求和?