我希望在flutter中更改以上4个字段的值时,第5个字段中的4个文本字段的总和

时间:2019-07-03 06:41:14

标签: flutter

这是代码。对于4个文本字段和一个用于添加这些值的字段,还使用了切换用例,用于从文本字段的标签中获取值,但未获得任何输出。我的切换案例也可以,但是在totalSum变量中没有任何输出。

这是代码。对于4个文本字段和一个用于添加这些值的字段,还使用了切换用例,用于从文本字段的标签中获取值,但未获得任何输出。我的切换案例也可以,但是在totalSum变量中没有任何输出。

  @override
  State<StatefulWidget> createState() => _AmountEdit();
}
class _AmountEdit extends State<AddAmount> {
  final amount = new TextEditingController();
  final amount1 = new TextEditingController();
  final amount2 = new TextEditingController();
  final amount3 = new TextEditingController();
  final amount4 = new TextEditingController();
  final total = new TextEditingController();

  final FocusNode _focusNode = FocusNode();
  final FocusNode _focusNode1 = FocusNode();
  final FocusNode _focusNode2 = FocusNode();
  final FocusNode _focusNode3 = FocusNode();
  final FocusNode _focusNode4 = FocusNode();
  final FocusNode _focusNode5 = FocusNode();

  void initState() {
    super.initState();
    _focusNode.addListener(() {
      print("Has focus: ${_focusNode.hasFocus}");
      if (_focusNode.hasFocus == false) {
        print(test);
      }
      if (test.contains(",00")) {
        print("perfect amount");
      } else {
        String finalAmount = test + append;
        print("the final amount ${finalAmount}");
        amount.text = finalAmount;
      }
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  String append = ",00";
  String test;

  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: AppBar(
        title: Text('Retrieve Text Input'),
      ),
      body: Column(
        children: <Widget>[
          TextBox1(
            controllerText: amount,
            focusNode: _focusNode,
            label: "amount",
          ),
          TextBox1(
            controllerText: amount1,
            focusNode: _focusNode1,
            label: "amount1",
          ),
          TextBox1(
            controllerText: amount2,
            focusNode: _focusNode2,
            label: "amount2",
          ),
          TextBox1(
            controllerText: amount3,
            focusNode: _focusNode3,
            label: "amount3",
          ),
          TextBox1(
            controllerText: amount4,
            focusNode: _focusNode4,
            label: "amount4",
          ),
          TextBox1(
            controllerText: total,
            focusNode: _focusNode5,
          ),
        ],
      ),
    );
  }
}

class TextBox1 extends StatefulWidget {
  var controllerText;
  var focusNode;
  String label;

  TextBox1({this.controllerText, this.focusNode, this.label});

  @override
  State<StatefulWidget> createState() => _TextBox();
}

class _TextBox extends State<TextBox1> {
  String test;
  int test1;
  String append = ",00";
  String finalAmount;

  @override
  void initState() {
    super.initState();
    widget.focusNode.addListener(() {
      if (widget.focusNode.hasFocus == false) {
        print(test);
        if (test == "") {
          finalAmount = "";
        } else {
          if (test.contains(",")) {
          } else {
            finalAmount = test + append;
            widget.controllerText.text = finalAmount;
          }
        }
      }
    });
  }

  @override
  void dispose() {
    widget.focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    double screenWidth = MediaQuery.of(context).size.width;
    return TextField(
        onChanged: (value) {

          print("widget label ${widget.label}");

          print("onchangecallback");
          test = value;
          print("printng value ${value}");

          test1 = int.parse(value);
          sum(value, widget.label);
//          print("printing test1 ${test1}");
        },
//        inputFormatters: [
//          WhitelistingTextInputFormatter.digitsOnly,
//          CurrencyInputFormatter(),
//        ],
        controller: widget.controllerText,
        focusNode: widget.focusNode,
        keyboardType: TextInputType.number,
        decoration: InputDecoration(
          disabledBorder:
              OutlineInputBorder(borderSide: BorderSide(color: Colors.white)),
          border: OutlineInputBorder(),
        ));
  }

  dynamic a;
  dynamic b;
  dynamic c;
  dynamic d;
  dynamic e;
  dynamic totalSum;

  sum(value, type) {
//    print("print value ${widget.controllerText}");
//    print("print amount text field ${value}");
    switch (type) {
      case "amount":
        {
          setState(() {});

          a = value;
          print("value of A ${a}");
        }
        break;

      case "amount1":
        {setState(() {});
          b = value;
          print("value of B ${b}");
        }
        break;

      case "amount2":
        {setState(() {});
          c = value;
          print("value of C ${c}");
        }
        break;

      case "amount3":
        {setState(() {});
          d = value;
          print("value of D ${d}");
        }
        break;

      case "amount4":
        {setState(() {});
          e = value;
          print("value of E ${e}");
        }
        break;

    }
    print("value of A outside the switch ${a}");
    print("value of B outside the switch ${b}");
  totalSum = a + b + c + d + e;
    print("this is total ${totalSum}");
  }
}```

1 个答案:

答案 0 :(得分:0)

为什么不使用TextEditingController.fromValue控制器?

    new TextField(
      controller: new TextEditingController.fromValue(
          new TextEditingValue(
              text: field1,
              selection:
      onChanged: (content) {
        setState(() {
        sum = field1+field2+field3+field4;
      });
      },
      onTap: (() {}),
      onSubmitted: ((s) {
        setState(() {
        });
      }),
    ),