jaumard / sms_autofill

Flutter plugin to provide SMS code autofill support
MIT License
289 stars 184 forks source link

PhoneFormFieldHint validator is not returning value #178

Closed danishkhawaja98 closed 1 year ago

danishkhawaja98 commented 1 year ago

Hi @jaumard While using your package, everything is working for getting mobile device phone number, but when leaving the field blank it is not calling the validator function. I had debugged it and attached a breakpoint on the validator function, but the debugger is not going to that validator function breakpoint. However, the flutter provided TextFormField is returning me the value from validator but your package is not.

Here is my code: `Form( key: _formKey, autovalidateMode: _autoValidateMode, child: Column(children: [ TextFormField( textInputAction: TextInputAction.next, inputFormatters: [ FilteringTextInputFormatter.digitsOnly, ], keyboardType: TextInputType.number, validator: (value) => Validator.generalValidation(value!, "Phone Number"), textAlign: TextAlign.left, decoration: InputDecoration( prefixIcon: Icon(Icons.phone), errorText: null, labelText: "Phone Number", hintText: "(555) 555-5555", labelStyle: TextStyle(color: Colors.black), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide(), ), ), onSaved: (value) { _userServiceViewModel.phoneNumber = value; }, ), PhoneFormFieldHint( validator: (value) => Validator.generalValidation(value!, "Phone Number"), child: TextField( focusNode: focusNode, controller: phoneController, textInputAction: TextInputAction.next, inputFormatters: [ FilteringTextInputFormatter.digitsOnly, ], keyboardType: TextInputType.number, textAlign: TextAlign.left, decoration: InputDecoration( prefixIcon: Icon(Icons.phone), errorText: null, labelText: "Phone Number", hintText: "(555) 555-5555", labelStyle: TextStyle(color: Colors.black), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide(), ), ), ), ), Row( children: [ Expanded( child: TextButton( onPressed: () async { if (_validateInputs() == false) return;

                            ServerResponse _serverResponse;

                            _formKey.currentState!.save();
                            alertDialog(context, 'Getting things ready');
                            _serverResponse =
                                await _userService.registerUser(
                                    context,
                                    _userServiceViewModel,
                                    _termOfServiceCheckBox);
                          },
                          style: TextButton.styleFrom(
                            primary: Colors.white,
                            backgroundColor: UnbankColors.secondaryColor,
                            textStyle: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.all(12.0),
                            child: Text("Register"),
                          )),
                    ),
                  ],
                ),
              ]),
            ),`

Here is my _validateInputs function:

bool _validateInputs() { final FormState form = _formKey.currentState!; if (!form.validate()) { setState(() { _autoValidateMode = AutovalidateMode.always; // Start validating on every change. }); return false; } else { form.save(); // Encrypt and send send payment details to payment gateway // showSnackBar(context, 'Payment card is valid'); return true; } }

Kindly have a look into this

Thanks

danishkhawaja98 commented 1 year ago

@jaumard When I am placing child -> TextField in PhoneFormFieldHint, the validator is not working.

Can you look into this?

Thanks

danishkhawaja98 commented 1 year ago

I got the solution