Flutter 颤振:将另一个textformfield的默认值设置为textformfield

Flutter 颤振:将另一个textformfield的默认值设置为textformfield,flutter,textformfield,Flutter,Textformfield,我有3个TextFormField,如下所示: 这是上面3个TextFormField的代码: class _ProposalDataInsuranceState extends State<ProposalDataInsurance> { final _totalPremiController = TextEditingController(); final _premiPokokController = TextEditingController(); fin

我有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),
          ),
        ),
      ],
    );
  }
}
class\u建议数据保险状态扩展状态{
最终_totalPremiController=TextEditingController();
最终_premiPokokController=TextEditingController();
final _premitopucontroller=TextEditingController();
@凌驾
无效处置(){
_totalPremiController.dispose();
_premiPokokController.dispose();
_premiTopUpController.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
TextFormField(
textInputAction:textInputAction.next,
风格:
textMediumColor(Modular.get().black),
控制器:_totalPremiController,
maxLines:1,
键盘类型:TextInputType.number,
输入格式化程序:[
FilteringPutFormatter.digitsOnly,
CurrencyInputFormatter(最大位数:16),
],
装饰:输入装饰(
边框:InputBorder.none,
FocusedOrder:InputBorder.none,
enabledBorder:InputBorder.none,
errorBorder:InputBorder.none,
disabledBorder:InputBorder.none,
内容填充:
仅限边集(底部:11,顶部:11,右侧:15),
hintText:'0',
辛茨风格:
textMediumColor(Modular.get().black),
),
),
TextFormField(
控制器:_premiPokokController,
textInputAction:textInputAction.next,
样式:textMediumColor(Modular.get().black),
maxLines:1,
键盘类型:TextInputType.number,
输入格式化程序:[
FilteringPutFormatter.digitsOnly,
CurrencyInputFormatter(最大位数:16),
],
装饰:输入装饰(
边框:InputBorder.none,
FocusedOrder:InputBorder.none,
enabledBorder:InputBorder.none,
errorBorder:InputBorder.none,
disabledBorder:InputBorder.none,
内容填充:
仅限边集(底部:11,顶部:11,右侧:15),
hintText:'0',
辛茨风格:
textMediumColor(Modular.get().black),
),
),
TextFormField(
控制器:_premito控制器,
textInputAction:textInputAction.done,
样式:textMediumColor(Modular.get().black),
maxLines:1,
键盘类型:TextInputType.number,
输入格式化程序:[
FilteringPutFormatter.digitsOnly,
CurrencyInputFormatter(最大位数:16),
],
装饰:输入装饰(
边框:InputBorder.none,
FocusedOrder:InputBorder.none,
enabledBorder:InputBorder.none,
errorBorder:InputBorder.none,
disabledBorder:InputBorder.none,
内容填充:
仅限边集(底部:11,顶部:11,右侧:15),
hintText:'0',
辛茨风格:
textMediumColor(Modular.get().black),
),
),
],
);
}
}
问题是,如何设置
Premi-Total-Premi
-
Premi-Pokok
)的验证默认值

例如:

  • 如果我在
    Total Premi
    中添加
    50000
    ,那么
    Premi Pokok
    的值仍然是
    0
    ,因为
    Total Premi
    -
    Premi Pokok
    50000
    -
    0
    =
    0
  • 如果我在
    Total Premi
    中添加
    50000
    ,在
    Premi Pokok
    中添加
    40000
    ,那么
    Premi Pokok
    的值是
    10000
    ,因为
    总Premi
    -
    Premi Pokok
    50000
    -
    40000
    =
    10000
  • 并且,
    Premi-toup
    仍然可以由用户编辑该值,尽管它具有默认值,并通过
    Total-Premi
    -
    Premi-Pokok
    进行验证

  • 在totalpremitopup的打开状态下

    使用

    设置状态(){

    var a=int.parse(totalpremipopup.text)

    var b=int.parse(Premipokpok.text)

    var c=a-b

    premitopup.text=c.toString()

    })

    普雷米科克的情况也一样

    使用

    设置状态(){

    var a=int.parse(totalpremipopup.text)

    var b=int.parse(Premipokpok.text)

    var c=a-b

    premitopup.text=c.toString()

    })


    我希望这在totalpremitopup的更改中起作用

    使用

    设置状态(){

    var a=int.parse(totalpremipopup.text)

    var b=int.parse(Premipokpok.text)

    var c=a-b

    premitopup.text=c.toString()

    })

    普雷米科克的情况也一样

    使用

    设置状态(){

    var a=int.parse(totalpremipopup.text)

    var b=int.parse(Premipokpok.text)

    var c=a-b

    premitopup.text=c.toString()

    })


    我希望这能起作用

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

    您可以按以下方式执行此操作:

    。。。
    void setPremitoppupDefault(){
    最终totalPremi=int.tryParse(_totalPremiController.text);
    最终premiPokok=int.tryParse(_premiPokokController.text);
    if(totalPremi!=null&&premiPokok!=null){
    _premiTopUpController.text=(totalPremi-premiPokok.toString();
    }
    }
    @凌驾
    void initState(){
    super.initState();
    _addListener(setPremiTopUpDefault);
    _addListener(setPremiTopUpDefault);
    }
    @凌驾
    无效处置(){
    _totalPremiController.removeListener(setPremiTopUpDefault);
    _premiPokokController.removeListener(setPremiTopUpDefault);
    ...
    }
    ...
    
    您可以将侦听器设置为文本字段控制器

    你可以像下面那样做