简体   繁体   English

Flutter 表单小部件未验证表单中的所有字段

[英]Flutter Form widget not validating all fields in the form

I have a IntlPhoneWidget and a textfield widget inside of a stepper widget.我在步进器小部件中有一个 IntlPhoneWidget 和一个文本字段小部件。 When I press the continue button it only validates the intlphonewidget and not the textfield.当我按下继续按钮时,它只验证 intlphonewidget 而不是文本字段。 Also how do I put in a custom validation message for each of the fields inside of the stepper if those fields are empty?另外,如果这些字段为空,我如何为步进器内的每个字段添加自定义验证消息?

Here is my code-这是我的代码-

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Form(
        key: _formKey,
        child: Stepper(
          currentStep: _currentStepper,
          onStepContinue: _continueStep,
          steps: [
            Step(
              title: const Text('Enter your name and phone number'),
              content: Column(
                children: [
                  TextField(
                    controller: _displayNameTextFieldController,
                  ),
                  IntlPhoneField(
                    decoration: const InputDecoration(
                      labelText: 'Phone Number',
                    ),
                    onChanged: (phone) {
                      setState(() {
                        _phoneNumber = (phone.completeNumber).trim();
                      });
                    },
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  void _continueStep() {
    if (_currentStepper == 0) {
      _formKey.currentState?.validate();
    }
  }

This happened because the IntlPhoneField has built-in validator , but TextField doesn't, if you want to have validator for first field too, you need to use TextFormField instead and set the validator like this:发生这种情况是因为IntlPhoneField具有内置validator ,但 TextField 没有,如果您也想为第一个字段设置validator器,则需要改用TextFormField并像这样设置validator

TextFormField(
    controller: _displayNameTextFieldController,
    validator: (value) {
      if (value != null &&  value.isEmpty) {
        return 'fill name field';
      }

      return null;
    },
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM