flutter - 从 ListView 中的所有 TextFeild 中获取值
问题描述
我制作了一个小部件,其中ListView
每个项目包含三个 TextField,就像这样。
小部件可以包含联系人拥有的N个地址。当联系人完成输入他们的地址时,我想单击 appBar 中的保存按钮以从每个项目中获取所有输入值ListView
并放入 JSON。像这样的东西:
[
{"Place": "Office", "Street": "Street Name", "PostalCode": "56789"},
{"Place": "Home", "Street": "Street Home", "PostalCode": "57689"},
{"Second Home": "Office", "Street": "Street 2th", "PostalCode": "45342"},
]
这是小部件的代码:
class TestPage2 extends StatefulWidget {
@override
_TestPage2State createState() => _TestPage2State();
}
class _TestPage2State extends State<TestPage2> {
List<Widget> lwidgets = [];
@override
void initState() {
lwidgets.insert(
lwidgets.length,
_Contacts(
indice: 0, onDeleteContact: (int i) => {deleteItemFromList(i)}));
lwidgets.insert(lwidgets.length, _AddElement(() => {addItemToList()}));
super.initState();
}
void addItemToList() {
setState(() {
lwidgets.insert(
lwidgets.length - 1,
_Contacts(
indice: lwidgets.length - 1,
onDeleteContact: (int i) => {deleteItemFromList(i)}));
});
}
void deleteItemFromList(int i) {
setState(() {
lwidgets.removeAt(i);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('CONTACTS'),
leading: Icon(Icons.arrow_left),
actions: <Widget>[
FlatButton(
onPressed:
() {}, // I want get all values contained of lwidgets. What do i need to do???
child: Text(
'SAVE',
style: TextStyle(color: Colors.white),
),
)
],
),
backgroundColor: Color(0xFFE5E5EA),
body: Column(
children: <Widget>[
Text('ADRESSES'),
Container(
height: 200,
child: ListView.builder(
itemCount: lwidgets.length,
itemBuilder: (BuildContext context, int index) {
return lwidgets[index];
},
physics: NeverScrollableScrollPhysics(),
),
),
],
));
}
}
这是联系信息小部件的代码:
class _Contacts extends StatelessWidget {
final int indice;
final Function(int) onDeleteContact;
const _Contacts({this.indice = 0, this.onDeleteContact});
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Container(
height: 40,
color: Colors.white,
child: Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.remove_circle),
iconSize: 24,
color: Color(0xFFFF3B30),
onPressed: () {
onDeleteContact(indice);
},
),
Container(
width: 100,
child: TextField(
decoration: InputDecoration(
hintText: 'Place',
border: InputBorder.none,
),
style: TextStyle(fontFamily: 'Lekton'),
),
),
_VerticalDivider(),
Container(
width: 100,
child: TextField(
decoration: InputDecoration(
hintText: 'Street',
border: InputBorder.none,
),
style: TextStyle(fontFamily: 'Lekton'),
),
),
_VerticalDivider(),
Container(
width: 100,
child: TextField(
decoration: InputDecoration(
hintText: 'Postal Code',
border: InputBorder.none,
),
style: TextStyle(fontFamily: 'Lekton'),
),
),
],
),
),
_HorizontalDivider(),
],
);
}
我将不胜感激任何帮助
解决方案
推荐阅读
- python - 检查数字是否步进的函数返回false
- config - 如何为包编写新的 .nix 配置文件?
- .net - 将 Dotnet 框架从 4.5.1 迁移到 Dotnet Core 2 常见问题
- mysql - 我的mysql存储过程row_count()在另一个存储过程中总是返回零
- python - py2exe.mf3._scan_opcodes 中的“元组索引超出范围”是什么意思?
- javascript - 使用控制比例线保存地图
- java - 如何从应用程序颜色主题以编程方式设置背景颜色
- reactjs - 如何在反应列表中隐藏特定元素?
- algorithm - 查找总和为特定值的所有子集,然后选择这些子集的最有价值的组合
- c# - 用户控件依赖属性在 x64 中为空