【发布时间】:2020-08-01 23:49:33
【问题描述】:
我想从这些动态 TextFormField 中获取文本值
class MyPets extends StatefulWidget {
@override
_MyPetsState createState() => _MyPetsState();
}
class _MyPetsState extends State<MyPets> {
List<Widget> _children = [];
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Title"),
actions: <Widget>[IconButton(icon: Icon(Icons.add), onPressed: _add)],
),
body: ListView(children: _children),
);
}
void _add() {
TextEditingController controller = TextEditingController();
controllers.add(controller); //adding the current controller to the list
for (int i = 0; i < controllers.length; i++) {
print(
controllers[i].text); //printing the values to show that it's working
}
_children = List.from(_children)
..add(
TextFormField(
controller: controller,
decoration: InputDecoration(
hintText: "This is TextField $_count",
icon: IconButton(icon: Icon(Icons.remove_circle), onPressed: rem),
),
),
);
setState(() => ++_count);
}
rem() {
setState(() {
_children.removeAt(_count);
controllers.removeAt(_count);
});
}
@override
void dispose() {
controllers.clear();
super.dispose();
}
}
问题是我不知道如何在颤振中将 TextEditingController 传递给这个表单域 我从this stack overflow answer得到了上述解决方案
基本上我想要文本字段,当我点击按钮时,文本字段必须递增 我想要每个文本字段的值,而且我应该能够删除相应的字段
请帮帮我 提前感谢
【问题讨论】:
标签: flutter dart flutter-layout