Flutter 如何将ListView添加到Flatter中的列?
我正在尝试为我的Flitter应用程序构建一个简单的登录页面。我已成功构建文本字段和登录/登录按钮。我想添加一个水平列表视图。当我运行代码时,我的元素消失了,如果我在没有ListView的情况下运行它,它又会好起来。如何才能正确地执行此操作Flutter 如何将ListView添加到Flatter中的列?,flutter,listview,dart,flutter-layout,flutter-widget,Flutter,Listview,Dart,Flutter Layout,Flutter Widget,我正在尝试为我的Flitter应用程序构建一个简单的登录页面。我已成功构建文本字段和登录/登录按钮。我想添加一个水平列表视图。当我运行代码时,我的元素消失了,如果我在没有ListView的情况下运行它,它又会好起来。如何才能正确地执行此操作 返回新材料app( 家:新脚手架( appBar:新的appBar( 标题:新文本(“登录/注册”), ), 主体:新容器( 孩子:新中心( 子:新列( mainAxisAlignment:mainAxisAlignment.center, 儿童:[ 新文本
返回新材料app(
家:新脚手架(
appBar:新的appBar(
标题:新文本(“登录/注册”),
),
主体:新容器(
孩子:新中心(
子:新列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新文本字段(
装饰:新的输入装饰(
hintText:“我是我的朋友”
),
),
新填充物(填充物:新边集。全部(15.00)),
新文本字段(蒙蔽文本:true,
装饰:新的输入装饰(
hintText:“P A S W O R D”
),
),
新填充物(填充物:新边集。全部(15.00)),
新文本字段(装饰:新输入装饰)(
hintText:“U S E R N A M E”
),),
新建RaisedButton(按下时为空),
儿童:新文本(“注册”),),
新填充物(填充物:新边集。全部(15.00)),
新建RaisedButton(按下时为空),
子项:新文本(“登录”),),
新填充物(填充物:新边集。全部(15.00)),
新列表视图(滚动方向:Axis.horizontal,
儿童:[
新建RaisedButton(按下时为空),
儿童:新文本(“Facebook”),),
新填充物(填充物:新边集。全部(5.00)),
新建RaisedButton(按下时为空),
子:新文本(“谷歌”),)
],)
],
),
),
保证金:所有新的边缘套(15.00),
),
),
);
您可以检查控制台输出。它打印错误:
在performResize()期间引发了以下断言:
水平视口具有无限高。
视口在横轴上展开以填充其容器并约束其子对象使其匹配
它们在横轴上的范围。在本例中,为水平视口提供了无限量的
要展开的垂直空间
您需要将高度约束添加到水平列表中。例如,包装在容器中,高度:
容器(
身高:44.0,
子:ListView(
滚动方向:轴水平,
儿童:[
升起的按钮(
onPressed:null,
儿童:文本(“Facebook”),
),
填充(填充:边缘集所有(5.00)),
升起的按钮(
onPressed:null,
子:文本(“谷歌”),
)
],
),
)
我也有这个问题。我的解决方案是使用Expanded
widget来扩展剩余空间
Column(
children: <Widget>[
Expanded(
child: horizontalList,
)
],
);
列(
儿童:[
扩大(
孩子:水平主义者,
)
],
);
错误原因:
列
在主轴方向(垂直轴)上扩展到最大大小,列表视图
解决方案
因此,您需要约束列表视图的高度。有很多方法,你可以选择最适合你需要的
如果要允许列表视图
占用列
中的所有剩余空间,请使用展开
Column(
children: <Widget>[
Expanded(
child: ListView(...),
)
],
)
如果您的列表视图
很小,可以尝试对其使用包覆面提取
属性
Column(
children: <Widget>[
ListView(
shrinkWrap: true, // use it
)
],
)
列(
儿童:[
列表视图(
包络处理:true,//使用它
)
],
)
我有SingleChildScrollView
作为父项,还有一个列
小部件,然后列出视图小部件作为最后一个子项
在列表视图中添加这些属性对我很有用
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.vertical,
实际上,当您阅读文档时,ListView应该在扩展的小部件中,这样它就可以工作了
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Align(
child: PayableWidget(),
),
Expanded(
child: _myListView(context),
)
],
));
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
儿童:[
对齐(
子项:PayableWidget(),
),
扩大(
子项:_myListView(上下文),
)
],
));
}正如上面其他人所提到的,用Expanded包装listview是解决方案
但是在处理嵌套列时,还需要将ListView限制在某个高度(经常遇到这个问题)。
如果有人有其他解决方案,请在评论中提及或添加答案
范例
SingleChildScrollView(
子:列(
儿童:[
Image(Image:),//您可以使用Flex
和Flexible
小部件。例如:
Flex(
direction: Axis.vertical,
children: <Widget>[
... other widgets ...
Flexible(
flex: 1,
child: ListView.builder(
itemCount: ...,
itemBuilder: (context, index) {
...
},
),
),
],
Flex(
方向:轴垂直,
儿童:[
…其他小部件。。。
灵活的(
弹性:1,
子项:ListView.builder(
itemCount:。。。,
itemBuilder:(上下文,索引){
...
},
),
),
],
)扩展小部件使用可用空间尽可能增加其大小,因为ListView实际上具有无限高,这将导致错误
Column(
children: <Widget>[
Flexible(
child: ListView(...),
)
],
)
列(
儿童:[
灵活的(
子项:ListView(…),
)
],
)
在这里,我们应该使用灵活的小部件,因为即使没有足够的小部件在全屏上渲染,它也只会占用扩展全屏所需的空间。尝试使用小部件:
Container(
child: CustomScrollView(
slivers: <Widget>[
SliverList(
delegate: SliverChildListDelegate(
[
HeaderWidget("Header 1"),
HeaderWidget("Header 2"),
HeaderWidget("Header 3"),
HeaderWidget("Header 4"),
],
),
),
SliverList(
delegate: SliverChildListDelegate(
[
BodyWidget(Colors.blue),
BodyWidget(Colors.red),
BodyWidget(Colors.green),
BodyWidget(Colors.orange),
BodyWidget(Colors.blue),
BodyWidget(Colors.red),
],
),
),
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
delegate: SliverChildListDelegate(
[
BodyWidget(Colors.blue),
BodyWidget(Colors.green),
BodyWidget(Colors.yellow),
BodyWidget(Colors.orange),
BodyWidget(Colors.blue),
BodyWidget(Colors.red),
],
),
),
],
),
),
)
容器(
子:自定义滚动视图(
条子:[
银表(
委托:SliverChildListDelegate(
[
标题标题(“标题1”),
标题标题(“标题2”),
海德威杰(“他
Column(
children: <Widget>[
Flexible(
child: ListView(...),
)
],
)
Container(
child: CustomScrollView(
slivers: <Widget>[
SliverList(
delegate: SliverChildListDelegate(
[
HeaderWidget("Header 1"),
HeaderWidget("Header 2"),
HeaderWidget("Header 3"),
HeaderWidget("Header 4"),
],
),
),
SliverList(
delegate: SliverChildListDelegate(
[
BodyWidget(Colors.blue),
BodyWidget(Colors.red),
BodyWidget(Colors.green),
BodyWidget(Colors.orange),
BodyWidget(Colors.blue),
BodyWidget(Colors.red),
],
),
),
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
delegate: SliverChildListDelegate(
[
BodyWidget(Colors.blue),
BodyWidget(Colors.green),
BodyWidget(Colors.yellow),
BodyWidget(Colors.orange),
BodyWidget(Colors.blue),
BodyWidget(Colors.red),
],
),
),
],
),
),
)
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text("Login / Signup"),
),
body: new Container(
child: new Center(
child: ListView(
//mainAxisAlignment: MainAxisAlignment.center,
scrollDirection: Axis.vertical,
children: <Widget>[
new TextField(
decoration: new InputDecoration(
hintText: "E M A I L A D D R E S S"
),
),
new Padding(padding: new EdgeInsets.all(15.00)),
new TextField(obscureText: true,
decoration: new InputDecoration(
hintText: "P A S S W O R D"
),
),
new Padding(padding: new EdgeInsets.all(15.00)),
new TextField(decoration: new InputDecoration(
hintText: "U S E R N A M E"
),),
new RaisedButton(onPressed: null,
child: new Text("SIGNUP"),),
new Padding(padding: new EdgeInsets.all(15.00)),
new RaisedButton(onPressed: null,
child: new Text("LOGIN"),),
new Padding(padding: new EdgeInsets.all(15.00)),
new ListView(scrollDirection: Axis.horizontal,
children: <Widget>[
new RaisedButton(onPressed: null,
child: new Text("Facebook"),),
new Padding(padding: new EdgeInsets.all(5.00)),
new RaisedButton(onPressed: null,
child: new Text("Google"),)
],)
],
),
),
margin: new EdgeInsets.all(15.00),
),
),
);
Expanded(
child: ListView(scrollDirection: Axis.horizontal,
children: <Widget>[
OutlineButton(onPressed: null,
child: Text("Facebook")),
Padding(padding: EdgeInsets.all(5.00)),
OutlineButton(onPressed: null,
child: Text("Google")),
Padding(padding: EdgeInsets.all(5.00)),
OutlineButton(onPressed: null,
child: Text("Twitter"))
]),
),
SizedBox(
height: 100,
child: ListView(scrollDirection: Axis.horizontal,
children: <Widget>[
OutlineButton(
color: Colors.white,
onPressed: null,
child: Text("Amazon")
),
Padding(padding: EdgeInsets.all(5.00)),
OutlineButton(onPressed: null,
child: Text("Instagram")),
Padding(padding: EdgeInsets.all(5.00)),
OutlineButton(onPressed: null,
child: Text("SoundCloud"))
]),
),
Container(
height: 80.0,
child: ListView(scrollDirection: Axis.horizontal,
children: <Widget>[
OutlineButton(onPressed: null,
child: Text("Shopify")),
Padding(padding: EdgeInsets.all(5.00)),
OutlineButton(onPressed: null,
child: Text("Yahoo")),
Padding(padding: EdgeInsets.all(5.00)),
OutlineButton(onPressed: null,
child: Text("LinkedIn"))
]),
),
Column(
children: <Widget>[
Text('Leading text widget'),
ListView(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
children: <Widget>[
ListTile(
leading: Icon(Icons.map),
title: Text('Map'),
),
ListTile(
leading: Icon(Icons.photo_album),
title: Text('Album'),
),
ListTile(
leading: Icon(Icons.phone),
title: Text('Phone'),
),
],
),
Text('More widget'),
],
);
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),