Flutter:将默认值从另一个 textformfield 设置为 textformfield

Flutter: Set default value from another textformfield to textformfield

我有 3 个 TextFormField,如下所示:

这是上面 3 个 TextFormField 的代码:

class _ProposalDataInsuranceState extends State<ProposalDataInsurance> {
  
  final _totalPremiController = TextEditingController();
  final _premiPokokController = TextEditingController();
  final _premiTopUpController = TextEditingController();

  @override
  void dispose() {
    _totalPremiController.dispose();
    _premiPokokController.dispose();
    _premiTopUpController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextFormField(
          textInputAction: TextInputAction.next,
          style:
          textMediumColor(Modular.get<ColorPalettes>().black),
          controller: _totalPremiController,
          maxLines: 1,
          keyboardType: TextInputType.number,
          inputFormatters: [
            FilteringTextInputFormatter.digitsOnly,
            CurrencyInputFormatter(maxDigits: 16),
          ],
          decoration: InputDecoration(
            border: InputBorder.none,
            focusedBorder: InputBorder.none,
            enabledBorder: InputBorder.none,
            errorBorder: InputBorder.none,
            disabledBorder: InputBorder.none,
            contentPadding:
            EdgeInsets.only(bottom: 11, top: 11, right: 15),
            hintText: '0',
            hintStyle:
            textMediumColor(Modular.get<ColorPalettes>().black),
          ),
        ),
        TextFormField(
          controller: _premiPokokController,
          textInputAction: TextInputAction.next,
          style: textMediumColor(Modular.get<ColorPalettes>().black),
          maxLines: 1,
          keyboardType: TextInputType.number,
          inputFormatters: [
            FilteringTextInputFormatter.digitsOnly,
            CurrencyInputFormatter(maxDigits: 16),
          ],
          decoration: InputDecoration(
            border: InputBorder.none,
            focusedBorder: InputBorder.none,
            enabledBorder: InputBorder.none,
            errorBorder: InputBorder.none,
            disabledBorder: InputBorder.none,
            contentPadding:
            EdgeInsets.only(bottom: 11, top: 11, right: 15),
            hintText: '0',
            hintStyle:
            textMediumColor(Modular.get<ColorPalettes>().black),
          ),
        ),
        TextFormField(
          controller: _premiTopUpController,
          textInputAction: TextInputAction.done,
          style: textMediumColor(Modular.get<ColorPalettes>().black),
          maxLines: 1,
          keyboardType: TextInputType.number,
          inputFormatters: [
            FilteringTextInputFormatter.digitsOnly,
            CurrencyInputFormatter(maxDigits: 16),
          ],
          decoration: InputDecoration(
            border: InputBorder.none,
            focusedBorder: InputBorder.none,
            enabledBorder: InputBorder.none,
            errorBorder: InputBorder.none,
            disabledBorder: InputBorder.none,
            contentPadding:
            EdgeInsets.only(bottom: 11, top: 11, right: 15),
            hintText: '0',
            hintStyle:
            textMediumColor(Modular.get<ColorPalettes>().black),
          ),
        ),
      ],
    );
  }
}

问题是,如何为 Premi Topup 设置带验证的默认值 (Total Premi - Premi Pokok)?

例如:

  1. 如果我在Total Premi中添加50000,那么Premi Pokok的值仍然是0,因为Total Premi - Premi Pokok50000 - 0 = 0.
  2. 如果我在Total Premi中添加50000,在Premi Pokok中添加40000,那么Premi Pokok的值就是10000,因为Total Premi - Premi Pokok50000 - 40000 = 10000.
  3. 并且 Premi Topup 仍然可以由用户编辑值,尽管具有来自 Total Premi - Premi Pokok.
  4. 验证的默认值

在totalpremitopup的on changed

使用

setState((){

var a=int.parse(totalpremipopup.text)

var b=int.parse(Premipokpok.text)

var c=a-b

premitopup.text=c.toString()

})

premiokok 中同样不变

使用

setState((){

var a=int.parse(totalpremipopup.text)

var b=int.parse(Premipokpok.text)

var c=a-b

premitopup.text=c.toString()

})

我希望这有效

您可以为您的文本字段控制器设置侦听器。

您可以按照以下方式进行操作:

  ...
  void setPremiTopUpDefault() {
    final totalPremi = int.tryParse(_totalPremiController.text);
    final premiPokok = int.tryParse(_premiPokokController.text);
    if (totalPremi != null && premiPokok != null) {
      _premiTopUpController.text = (totalPremi - premiPokok).toString();
    }
  }

  @override
  void initState() {
    super.initState();
    _totalPremiController.addListener(setPremiTopUpDefault);
    _premiPokokController.addListener(setPremiTopUpDefault);
  }

  @override
  void dispose() {
    _totalPremiController.removeListener(setPremiTopUpDefault);
    _premiPokokController.removeListener(setPremiTopUpDefault);
    ...
  }
  ...