0

我有 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),
          ),
        ),
      ],
    );
  }
}

问题是,如何使用验证(Total Premi- Premi Pokok)设置默认值Premi Topup

例如:

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

2 回答 2

1

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

你可以这样做:

  ...
  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);
    ...
  }
  ...
于 2021-01-28T10:53:55.937 回答
1

在 totalpremitopup 的更改中

利用

设置状态((){

var a=int.parse(totalpremipopup.text)

var b=int.parse(Premipokpok.text)

变量 c=ab

premitopup.text=c.toString()

})

同样对于 premiokok 中的未更改

利用

设置状态((){

var a=int.parse(totalpremipopup.text)

var b=int.parse(Premipokpok.text)

变量 c=ab

premitopup.text=c.toString()

})

我希望这有效

于 2021-01-28T10:30:42.833 回答