我如何在flutter中使用formz确认密码



当我要确认密码,但是无法比较第一个和最后一个密码时

这是我的密码类。它工作得很好,但是confirmPassword部分不行。

class Password extends FormzInput<String, PasswordValidationError> {
const Password.pure() : super.pure('');
const Password.dirty([String value = '']) : super.dirty(value);
static final _passwordRegExp = RegExp(r'^[A-Za-zd@$!%*?&]{8,}$');
@override
PasswordValidationError? validator(String value) {
if (value.isEmpty) {
return PasswordValidationError.empty;
}
return _passwordRegExp.hasMatch(value)
? null
: PasswordValidationError.invalid;
}
}

你可以问什么是验证器的价值。我示范给你看。

void passwordChanged(String value) {
final password = Password.dirty(value);
emit(state.copyWith(
password: password,
status: Formz.validate(
[state.name, state.email, password, state.confirmPassword]),
));
}

这是我的普通密码字段。现在让我们看看confirm section

class ConfirmPassword
extends FormzInput<String, ConfirmedPasswordValidationError> {
const ConfirmPassword.pure() : super.pure('');
const ConfirmPassword.dirty([String value = '']) : super.dirty(value);
static final _confirmPasswordRegExp = RegExp(r'^[A-Za-zd@$!%*?&]{8,}$');

@override
ConfirmedPasswordValidationError? validator(String value) {
if (value.isEmpty) {
return ConfirmedPasswordValidationError.empty;
} else if (!_confirmPasswordRegExp.hasMatch(value)) {
return ConfirmedPasswordValidationError.invalid;
}
return (password**I mentioned here** == value)
? null
: ConfirmedPasswordValidationError.mismatch;
}
}

和validator中的value:

void confirmPasswordChanged(String value) {
final confirmPassword =
ConfirmPassword.dirty(value);
emit(state.copyWith(
confirmPassword:
state.password.valid ? confirmPassword : const ConfirmPassword.pure(),
status: Formz.validate(
[state.name, state.email, state.password, confirmPassword]),
));
}

我怎么能达到第一个(正常)值字段在confirmPassword节?或者有任何方法与formz的使用?

这是一个验证器

import 'package:formz/formz.dart';
enum ConfirmedPasswordValidationError {
invalid,
mismatch,
}
class ConfirmedPassword extends FormzInput<String, ConfirmedPasswordValidationError> {
final String password;
const ConfirmedPassword.pure({
this.password = ''
}) : super.pure('');
const ConfirmedPassword.dirty({
required this.password,
String value = ''
}) : super.dirty(value);
@override
ConfirmedPasswordValidationError? validator(String value) {
if (value.isEmpty) {
return ConfirmedPasswordValidationError.invalid;
}
return password == value
? null
: ConfirmedPasswordValidationError.mismatch;
}
}
extension Explanation on ConfirmedPasswordValidationError {
String? get name {
switch(this) {
case ConfirmedPasswordValidationError.mismatch:
return 'passwords must match';
default:
return null;
}
}
}

状态管理功能组

void _passwordConfirmationChanged(PasswordConfirmationChangede,Emitter<SignUpFormState> emit){
final confirmedPassword = ConfirmedPassword.dirty(
password: state.password.value,
value: e.passwordConfirmStr,
);
emit(state.copyWith(
confirmedPassword: confirmedPassword,
status: Formz.validate([
state.email,
state.password,
confirmedPassword,
]),
));
}

Widget类

class _ConfirmPasswordInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<SignUpFormBloc, SignUpFormState>(
buildWhen: (previous, current) =>
previous.password != current.password ||
previous.confirmedPassword != current.confirmedPassword,
builder: (context, state) {
return TextField(
key: const Key('signUpForm_confirmedPasswordInput_textField'),
onChanged: (confirmPassword) => context.read<SignUpFormBloc>()
.add(SignUpFormEvent.passwordConfirmationChanged(confirmPassword)),
obscureText: true,
decoration: InputDecoration(
labelText: 'confirm password',
helperText: '',
errorText: state.confirmedPassword.invalid
? 'passwords do not match'
: null,
),
);
},
);
}
}

英文答案是正确的

我在密码更新时添加了确认密码状态的更新:

void _onPasswordChanged(
SignupPasswordChanged event,
Emitter<SignupState> emit,
) {
final password = Password.dirty(event.password);
final confirmPassword = ConfirmedPassword.dirty(password: password.value, value: state.confirmPassword.value);
emit(state.copyWith(
password: password,
confirmPassword: confirmPassword,
status: Formz.validate([password, confirmPassword, state.username]),
));
}

相关内容

  • 没有找到相关文章

最新更新