Flutter 如何在flatter中将小部件动态添加到列中?
我在Flutter 如何在flatter中将小部件动态添加到列中?,flutter,flutter-layout,Flutter,Flutter Layout,我在列表视图中添加了一些小部件。这样我就可以滚动所有小部件。现在我需要在ListView中再添加一个小部件来加载注释列表。我无法在列表视图中添加列表视图。此外,我的评论不需要单独的卷轴。它应该与列表视图中的小部件一起滚动。因此我计划添加列,而不是列表视图。在列中动态添加我的注释有什么帮助吗 new Expanded( child: new ListView( shrinkWrap: true, childre
列表视图中添加了一些小部件。这样我就可以滚动所有小部件。现在我需要在ListView
中再添加一个小部件来加载注释列表。我无法在列表视图
中添加列表视图
。此外,我的评论不需要单独的卷轴。它应该与列表视图中的小部件一起滚动。因此我计划添加列
,而不是列表视图
。在列中动态添加我的注释有什么帮助吗
new Expanded(
child:
new ListView(
shrinkWrap: true,
children: <Widget>[
// Title
new Padding(padding: const EdgeInsets.only(
top: 10.00, left: 10.00),
child: new Text(
_feed.title, textAlign: TextAlign.start,),
),
// content
new Container(
child: new Text(
_feed.content, textAlign: TextAlign.start,),
),
// Comments List will go here
],
),
),
新扩展(
儿童:
新列表视图(
收缩膜:对,
儿童:[
//头衔
新填充(填充:仅限常量边集)(
顶部:10.00,左侧:10.00),
儿童:新文本(
_feed.title,textAlign:textAlign.start,),
),
//内容
新容器(
儿童:新文本(
_feed.content,textAlign:textAlign.start,),
),
//评论列表将放在这里
],
),
),
如果您已经有注释数据,只需创建一个列表,然后将其传递给列的子属性。比如:
var commentWidgets = List<Widget>();
for (var comment in comments) {
commentWidgets.Add(Text(comment.text)); // TODO: Whatever layout you need for each widget.
}
…
new Expanded(
child:
new ListView(
shrinkWrap: true,
children: <Widget>[
// Title
new Padding(padding: const EdgeInsets.only(
top: 10.00, left: 10.00),
child: new Text(
_feed.title, textAlign: TextAlign.start,),
),
// content
new Container(
child: new Text(
_feed.content, textAlign: TextAlign.start,),
),
// Comments List will go here
Column(children: commentWidgets,),
],
),
),
var commentWidgets=List();
for(注释中的var注释){
commentWidgets.Add(Text(comment.Text));//TODO:每个小部件所需的任何布局。
}
…
新扩展(
儿童:
新列表视图(
收缩膜:对,
儿童:[
//头衔
新填充(填充:仅限常量边集)(
顶部:10.00,左侧:10.00),
儿童:新文本(
_feed.title,textAlign:textAlign.start,),
),
//内容
新容器(
儿童:新文本(
_feed.content,textAlign:textAlign.start,),
),
//评论列表将放在这里
列(子项:commentWidgets,),
],
),
),
如果您还没有注释数据,并且需要获取这些数据,那么在将来完成后,使用a构建UI。import'package:flatter/material.dart';
import 'package:flutter/material.dart';
class LoginRegisterPage extends StatefulWidget {
@override
_LoginRegisterPageState createState() => _LoginRegisterPageState();
}
class _LoginRegisterPageState extends State<LoginRegisterPage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("App"),
),
body: new Container(
margin: EdgeInsets.all(15.0),
child: new Form(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: createInputs() + createButtons(),
),
),
),
);
}
List<Widget> createInputs(){
return{
SizedBox(height: 10.0),
logo(),
SizedBox(height: 20.0),
new TextFormField(
decoration: new InputDecoration(labelText: 'Email'),
),
SizedBox(height: 10.0),
new TextFormField(
decoration: new InputDecoration(labelText: 'Passwors')
),
SizedBox(height: 20.0),
};
}
Widget logo(){
return new Hero(
tag:'hero',
child: new CircleAvatar(
backgroundColor:Colors.transparent,
radius: 110.0,
child: Image.asset("images\logo.PNG"),
),
);
}
List<Widget> createButtons(){
return{
new RaisedButton(
child: new Text("Login", style: new TextStyle(fontSize: 20.0),),
textColor: Colors.pink,
onPressed: () {
},
),
new FlatButton(
child: new Text("Already not have an account?", style: new TextStyle(fontSize: 14.0),),
textColor: Colors.white,
onPressed: () {
},
)
};
}
}
类LoginRegisterPage扩展StatefulWidget{
@凌驾
_LoginRegisterPageState createState()=>\u LoginRegisterPageState();
}
类_LoginRegisterPageState扩展状态{
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:appBar(
标题:文本(“应用程序”),
),
主体:新容器(
边距:所有边缘集(15.0),
儿童:新表格(
子:新列(
crossAxisAlignment:crossAxisAlignment.stretch,
子项:createInputs()+createButtons(),
),
),
),
);
}
列表createInputs(){
返回{
尺寸箱(高度:10.0),
logo(),
尺寸箱(高度:20.0),
新TextFormField(
装饰:新输入装饰(标签文本:“电子邮件”),
),
尺寸箱(高度:10.0),
新TextFormField(
装饰:新输入装饰(标签文本:“Passwors”)
),
尺寸箱(高度:20.0),
};
}
Widget徽标(){
回归新英雄(
标签:'英雄',
孩子:新CircleAvatar(
背景颜色:颜色。透明,
半径:110.0,
子项:Image.asset(“images\logo.PNG”),
),
);
}
列表创建按钮(){
返回{
新升起的按钮(
子项:新文本(“登录”,样式:新文本样式(fontSize:20.0),
textColor:Colors.pink,
onPressed:(){
},
),
新扁平按钮(
子项:新文本(“是否已经没有帐户?”,样式:新文本样式(fontSize:14.0),),
textColor:Colors.white,
已按下:(){
},
)
};
}
}
通过维护对列
对象的引用,可以在声明列
对象后引用.children
字段/属性,如下所示:
Column someColumn = Column(
children: [],
);
someColumn.children.add(Text('Hello 123'));
除了@Derek Lakin的答案对我也有用之外,如果需要更新注释并重新加载,还需要调用setState()
var commentWidgets = List<Widget>();
for (var comment in comments) {
commentWidgets.Add(Text(comment.text)); // TODO: Whatever layout you need foreach widget.
}
setState(() {
});
var commentWidgets=List();
for(注释中的var注释){
commentWidgets.Add(Text(comment.Text));//TODO:每个小部件所需的任何布局。
}
设置状态(){
});
目前(2021-06),颤振2.x实现了空安全。
@Gene Bo的答案应该是可行的,但不需要太多修改
这应该行得通
var pwdWidgets=[];
纵队(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
孩子们:这个.pwdWidgets,
),
升降按钮(
已按下:(){
this.pwdWidgets.add(文本(“Hello World”);
},
子:文本(“单击我”),
),
感谢您提供到FutureBuilder的链接!这节省了我一天的时间。嗨,我也想知道我如何动态添加和删除同一个小部件,例如,如果说我添加了一个小部件,我如何将同一个小部件作为目标来删除它?虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题会真正有助于提高您的帖子的质量,而且可能会得到更多的支持票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请编辑您的答案,添加解释,并说明适用的限制和假设。