[英]onPressed create multiple instances of CheckboxListTile in Flutter
I'm new to flutter so I'm not sure how to go about this.我是 flutter 的新手,所以我不确定 go 如何解决这个问题。 I want to be able to add another instance of the same CheckboxListTile when a user presses a button right below the most recent CheckboxListTile.当用户按下最近的 CheckboxListTile 正下方的按钮时,我希望能够添加相同 CheckboxListTile 的另一个实例。
The code for how it currently is is below.目前的代码如下。
Widget build(BuildContext context) {
return ListView(
children: <Widget>[
CheckboxListTile(
title: TextField(
autocorrect: true,
),
value: checkBoxValue,
secondary: Icon(Icons.assignment),
onChanged: (bool newValue) {
setState(() {
checkBoxValue = newValue;
});
}
),
],
The code of an example of how I would want the app to appear after a user presses is below.下面是我希望应用程序在用户按下后出现的示例代码。
Widget build(BuildContext context) {
return ListView(
children: <Widget>[
CheckboxListTile(
title: TextField(
autocorrect: true,
),
value: checkBoxValue,
secondary: Icon(Icons.assignment),
onChanged: (bool newValue) {
setState(() {
checkBoxValue = newValue;
});
}
),
CheckboxListTile(
title: TextField(
autocorrect: true,
),
value: checkBoxValue,
secondary: Icon(Icons.assignment),
onChanged: (bool newValue) {
setState(() {
checkBoxValue = newValue;
});
}
),
],
Thanks in advance!提前致谢!
Generating widgets in a ListView is by adding List<Widget>
in its children.在ListView中生成小部件是通过在其子项中添加List<Widget>
来实现的。 Simply put, it roughly looks similar to.简单地说,它看起来大致相似。
ListView(
children: <Widget>[widgetA, widgetA, widgetC]);
What you need to do is to manually add widgets in List<Widget>
.您需要做的是在List<Widget>
中手动添加小部件。 You can create a List<Widget> _checkboxList
and create a function that returns a CheckboxListTile widget.您可以创建一个List<Widget> _checkboxList
并创建一个返回 CheckboxListTile 小部件的 function。
CheckboxListTile _checkboxListTile(){
return CheckboxListTile(
title: TextField(
autocorrect: true,
),
value: checkBoxValue,
secondary: Icon(Icons.assignment),
onChanged: (bool newValue) {
setState(() {
checkBoxValue = newValue;
});
}
);
}
and on your button, call _checkboxList.add(_checkboxListTile());
并在您的按钮上调用_checkboxList.add(_checkboxListTile());
to add it on List<Widget> _checkboxList
将其添加到List<Widget> _checkboxList
RaisedButton(
onPressed: () {
setState(() {
_checkboxList.add(_checkboxListTile());
});
},
child: Text('Add checkbox'),
),
To display List _checkboxList on your screen:要在屏幕上显示 List _checkboxList:
Widget build(BuildContext context) {
return ListView(children: _checkboxList);
}
Let me know if this helps.让我知道这是否有帮助。
One way you could do it is by using a ListView.builder
like this..您可以这样做的一种方法是使用这样的ListView.builder
..
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
List<bool> checkBoxesCheckedStates = [false];
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: checkBoxesCheckedStates.length,
itemBuilder: (BuildContext context, int index){
return CheckboxListTile(
title: TextField(
autocorrect: true,
),
value: checkBoxesCheckedStates[index],
secondary: Icon(Icons.assignment),
onChanged: (bool newValue) {
setState(() {
checkBoxesCheckedStates[index] = newValue;
});
},
);
},
),
),
RaisedButton(
child: Text('Add'),
onPressed: (){
setState(() {
checkBoxesCheckedStates.add(false);
});
},
),
],
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.