我怎么能添加验证到otp领域时,otp领域是空的Flutter

hzbexzde  于 2023-06-30  发布在  Flutter
关注(0)|答案(2)|浏览(125)

我遇到了一些问题。我如何可以添加验证到明文字段或任何字段,这是用于OTP输入。如果有人有另一个领域的OTP输入请让我知道。希望你能理解。请帮帮我。

_onAlertotp(context) {
    Alert(
        context: context,
        title: "OTP expires in: MM:SS (Counter",
        desc:
            "We have Texted and/or Emailed OTP (One Time Pin) to your registered cell phone and/ or email account. Please check and enter OTP below to activate your TUDO account",
        content: Column(
          children: <Widget>[
            Form(
              autovalidate: true,
              child: PinField(
                  padding: EdgeInsets.all(16.0),
                  length: 6,
                  gap: 6.0,
                  onSubmitted: Validators().otpValidate,
                  keyboardType: TextInputType.numberWithOptions(signed: true),
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly]),
            )
          ],
        ),
        buttons: [
          DialogButton(
            onPressed: () => Navigator.pop(context),
            child: Text(
              "Resend OTP",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
          ),
          DialogButton(
            onPressed: () {
              _onAlertrunningbusiness(context);
            },
            child: Text(
              "Enter Otp",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
          )
        ]).show();
  }
xa9qqrwz

xa9qqrwz1#

为了验证一个空字符串,你只需要检查它的值是否等于“”。
你可以使用getter函数isEmpty()来检查String是否为空。

方案编码:

validator: (String value) {
                        if (value.isEmpty) {
                          return "Please enter phone number";
                        } else if (value.length != 10) {
                          return "Please enter valid Phone number";
                        } else {
                          return null;
                        }
                      },
cbjzeqam

cbjzeqam2#

我正在使用不同的库:https://pub.dev/packages/otp_text_field。但是你的库也有'onChanged'参数。

static const int _otpLength = 6;
    
OTPTextField(
  length: _otpLength,
  onChanged: (code) {
      _onOtpValidated(code.length == _otpLength);
  },
),

void _onOtpValidated(bool isValid) {
    // make your validation flag true
}

相关问题