Flutter:如何将输入从文本字段保存到INT变量?
这是我的文本控制器
final amountController = TextEditingController();
@override
void dispose() {
amountController.dispose();
super.dispose();
}
,当我想将项目添加到列表中时,一个iConbutton显示了一个带有文本字段的对话框,即IM应该在其中获取输入。
IconButton(
icon: Icon(Icons.add),
onPressed: () {
// //this is the part where i add a new income to my list
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Add income'),
content: TextField(
keyboardType: TextInputType.number,
controller: amountController,
onChanged: (s) {
int s = int.parse(amountController.text);
amount = s;
transactions.add(s);
},
),
));
}),
问题是我不知道如何在这里保存数据
List<int> transactions = [];
int amount = 0;
This is my text controller
final amountController = TextEditingController();
@override
void dispose() {
amountController.dispose();
super.dispose();
}
and when i want to add an item to a list a got an iconbutton that shows a dialog box with a text field where im supposed to get inputs.
IconButton(
icon: Icon(Icons.add),
onPressed: () {
// //this is the part where i add a new income to my list
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Add income'),
content: TextField(
keyboardType: TextInputType.number,
controller: amountController,
onChanged: (s) {
int s = int.parse(amountController.text);
amount = s;
transactions.add(s);
},
),
));
}),
the probleme is that i dont know how to save the data here
List<int> transactions = [];
int amount = 0;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该在on Changed方法中编写此行
transactions.ADD(s);
,因为在每个数字中,您输入的每个数字都将值添加到列表中。因此,您必须将Onchanged值分配给金额值,并在按钮的OnPressed方法中向对话框添加一个按钮添加此行
transactions.add(s);
you should not write this line
transactions.add(s);
in onChanged method, because in this way in each number you enter you add a value to the list.so you have to assign onChanged value to amount value and add a button to your dialog in button's onPressed method add this line
transactions.add(s);
我尝试了另一种方法:
我只是在flatbutton中调用了该功能
I tried another way :
and i just called the function within a flatbutton