我添加项目的按钮没有显示。
我要求用户填写玩家姓名。如果用户想添加一个播放器,则单击一个按钮以添加一个新字段
List<Widget> _playerList = new List(); //_playerList is my List name
ListView.builder(
itemBuilder: (context,index){
_playerList.add(new TextField());
Widget widget = _playerList.elementAt(index);
return widget;
}),
new FlatButton.icon(
color: Colors.black,
icon: Icon(
Icons.add,
color: Colors.white,
size: 18,
),
label: Text('Add player',
style: TextStyle(fontSize: 12, color: Colors.white)),
onPressed: () {
_playerList.add(new Text("ggg"));
},
),
添加按钮不显示
最佳答案
在您的代码中,下面的部分产生了问题-
ListView.builder(
itemBuilder: (context,index){
_playerList.add(new TextField());
Widget widget = _playerList.elementAt(index);
return widget;
}),
删除'_playerList.add(new TextField());'线。 它应该喜欢-
class Items extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _ItemsState();
}
}
class _ItemsState extends State<Items> {
List<Widget> _playerList = new List(); //_playerList is my List name
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: Column(
children: <Widget>[
new FlatButton.icon(
color: Colors.black,
icon: Icon(
Icons.add,
color: Colors.white,
size: 18,
),
label: Text('Add player',
style: TextStyle(fontSize: 12, color: Colors.white)),
onPressed: () {
setState(() {
_playerList.add(new Text("ggg"));
print(_playerList);
});
},
),
Expanded(
child: ListView.builder(
itemCount:_playerList.length ,
itemBuilder: (context,index){
Widget widget = _playerList.elementAt(index);
return widget;
})
)
],
));
}
}
关于flutter - 用户单击按钮时如何在列表中添加 TextField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56265314/