Flutter:如何将输入从文本字段保存到INT变量?

发布于 2025-01-22 19:21:56 字数 1214 浏览 2 评论 0原文

这是我的文本控制器

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年 2025-01-29 19:21:56

您不应该在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);

吐个泡泡 2025-01-29 19:21:56

我尝试了另一种方法:

    TextEditingController _textFieldController = TextEditingController();
  List<int> transactions = [];
  int Total = 0;
  Future<void> _displayTextInputDialog(BuildContext context) async {
    int Amount = 0;
    String valueText = '';
    return showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('Add income'),
            content: TextField(
              keyboardType: TextInputType.number,
              onChanged: (value) {
                setState(() {
                  valueText = value;
                });
              },
              controller: _textFieldController,
            ),
            actions: <Widget>[
              FlatButton(
                color: Colors.red,
                textColor: Colors.white,
                child: Text('CANCEL'),
                onPressed: () {
                  setState(() {
                    Navigator.pop(context);
                  });
                },
              ),
              FlatButton(
                color: Colors.green,
                textColor: Colors.white,
                child: Text('OK'),
                onPressed: () {
                  setState(() {
                    Amount = int.parse(valueText);
                    transactions.add(Amount);
                    Total = Total + Amount;
                    print(Amount);
                    Navigator.pop(context);
                  });
                },
              ),
            ],
          );
        });
  }

我只是在flatbutton中调用了该功能

I tried another way :

    TextEditingController _textFieldController = TextEditingController();
  List<int> transactions = [];
  int Total = 0;
  Future<void> _displayTextInputDialog(BuildContext context) async {
    int Amount = 0;
    String valueText = '';
    return showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('Add income'),
            content: TextField(
              keyboardType: TextInputType.number,
              onChanged: (value) {
                setState(() {
                  valueText = value;
                });
              },
              controller: _textFieldController,
            ),
            actions: <Widget>[
              FlatButton(
                color: Colors.red,
                textColor: Colors.white,
                child: Text('CANCEL'),
                onPressed: () {
                  setState(() {
                    Navigator.pop(context);
                  });
                },
              ),
              FlatButton(
                color: Colors.green,
                textColor: Colors.white,
                child: Text('OK'),
                onPressed: () {
                  setState(() {
                    Amount = int.parse(valueText);
                    transactions.add(Amount);
                    Total = Total + Amount;
                    print(Amount);
                    Navigator.pop(context);
                  });
                },
              ),
            ],
          );
        });
  }

and i just called the function within a flatbutton

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文