flutter 找到了一些很好的代码来做一个加两个数字的计算,但我需要加20个数字在一起,我会显示代码,并把没有真实的的错误

doinxwow  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(126)

第三个目标:% 3不添加到目标% 1和% 2。目标1和目标2正确相加
请注意“TextEditingController num 3controller = new TextEditingController();“已添加由我尝试添加3字段添加我需要使用不同的类或语法添加多个字段谢谢大家
输出outputenter image description here的图片
测试结果

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appTitle = 'Twin City Bowmen Score Sheet';

    return MaterialApp(
      title: appTitle,
      home: Scaffold(
        appBar: AppBar(
          title: Text(appTitle),
        ),
        body: AddTwoNumbers(),
      ),
    );
  }
}

class AddTwoNumbers extends StatefulWidget {
  @override
  _AddTwoNumbersState createState() => _AddTwoNumbersState();
}

class _AddTwoNumbersState extends State<AddTwoNumbers> {
  TextEditingController num1controller = new TextEditingController();
  TextEditingController num2controller = new TextEditingController();
  TextEditingController num3controller = new TextEditingController();
  String result = "0";
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Text("Target 1:"),
              new Flexible(
                child: new TextField(
                  keyboardType: TextInputType.number,
                  controller: num1controller,
                ),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Text("Target 2:"),
              new Flexible(
                child: new TextField(
                  keyboardType: TextInputType.number,
                  controller: num2controller,
                ),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Text("Target 3:"),
              new Flexible(
                child: new TextField(
                  keyboardType: TextInputType.number,
                  controller: num3controller,
                ),
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                child: Text("Add"),
                onPressed: () {
                  setState(() {
                    int sum = int.parse(num1controller.text) +
                        int.parse(num2controller.text);
                    result = sum.toString();
                  });
                },
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                "Result:",
                style: TextStyle(
                  fontSize: 30,
                ),
              ),
              Text(
                result,
                style: TextStyle(
                  fontSize: 30,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

字符串

p3rjfoxz

p3rjfoxz1#

如果我正确理解你的问题,这应该是解决方案:

setState(() {
                int sum = int.parse(num1controller.text) +
                    int.parse(num2controller.text) +
                    int.parse(num3controller.text);
                result = sum.toString();
              });

字符串

相关问题