Flutter-Bloc-比较两个TextFields密码和passwordConfirm的问题



我是一个颤振的工程师。我使用的是Bloc,我能够验证单个的Texfields,但在比较两个TextFields(password和passwordConfirm(时我有一些疑问。我使用BehaviorSubject((作为控制器和Rx.dart,但我找不到链接它们的方法。我也尝试过使用StreamTransformer,但无法输入多个值。

final validatePasswordConfirm = StreamTransformer<String,String>
.fromHandlers(handleData: (passwordConfirm, sink) {});

以下是其余代码(register_page.dart、register_bloc.dart和validators.dart的一部分(:

// register_page.dart

Widget create_password(RegisterBloc bloc){
return StreamBuilder(
stream: bloc.passwordStream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: TextField(
obscureText: true,
decoration: InputDecoration(
icon: Icon(Icons.lock, color: Colors.deepPurple),
labelText: 'password',
counterText: snapshot.data,
errorText: snapshot.error
),
onChanged: (value) => bloc.changePassword(value),
),
);
},
);
}

Widget create_password_confirm(RegisterBloc bloc){
return StreamBuilder(
stream: bloc.validatePasswordConfirmStream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: TextField(
obscureText: true,
decoration: InputDecoration(
icon: Icon(Icons.lock, color: Colors.deepPurple),
labelText: 'password Confirm',
counterText: snapshot.data,
errorText: snapshot.error
),
onChanged: (value) => bloc.changePasswordConfirm(value),
),
);
},
);
}
// register_bloc.dart
class RegisterBloc with Validators{

final _usernameController = BehaviorSubject<String>();
final _emailController = BehaviorSubject<String>();
final _passwordController = BehaviorSubject<String>();
final _passwordConfirmController = BehaviorSubject<String>();


Stream<String> get usernameStream => _emailController.stream.transform(validatePassword);
Stream<String> get emailStream => _emailController.stream.transform(validateEmail);
Stream<String> get passwordStream => _passwordController.stream.transform(validatePassword);
Stream<String> get passwordConfirmStream => _passwordConfirmController.stream.transform(validatePasswordConfirm);


Stream<String> get validatePasswordConfirmStream => Rx.combineLatest2(passwordStream, passwordConfirmStream,
(p, pc){
return p.compareTo(pc)? p.passwordStream:'passwords does not match';
});

Stream<bool> get formValidStream =>
Rx.combineLatest4(usernameStream, emailStream, passwordStream, passwordConfirmStream, (u,e,p, pc) => true);

Function(String) get changeUsername => _usernameController.sink.add;
Function(String) get changeEmail => _emailController.sink.add;
Function(String) get changePassword => _passwordController.sink.add;
Function(String) get changePasswordConfirm => _passwordConfirmController.sink.add;


String get username => _usernameController.value;
String get email => _emailController.value;
String get password => _passwordController.value;
String get passwordConfirm => _passwordController.value;

dispose() {
_usernameController?.close();
_emailController?.close();
_passwordController?.close();
_passwordConfirmController?.close();
}
}
// validators.dart
class Validators{
final validateEmail = StreamTransformer<String,String>.fromHandlers(
handleData: (email, sink) {
Pattern pattern = r'^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$';
RegExp regExp = new RegExp(pattern);
if (regExp.hasMatch(email)) {
sink.add(email);
} else {
sink.addError('Email no es correcto');
}
}
);

final validatePassword = StreamTransformer<String,String>.fromHandlers(
handleData: (p, sink){
if(p.length >= 6) {
sink.add(p);
} else {
sink.addError('Más de 6 caracteres por favor');
}
}
);

final validatePasswordConfirm = StreamTransformer<String,String>.fromHandlers(
handleData: (passwordConfirm, sink) {});
}

提前表示感谢,并为我糟糕的英语感到抱歉。问候,Mariano

几个小时后,我终于找到了问题的答案。现在我可以比较两个textField,如果它们不匹配,我会向passwordConfirm textField发送一条错误消息。

Stream<String> get passwordConfirmStream => _passwordConfirmController.stream
.transform(validatePassword).doOnData((String c){
if (0 != _passwordController.value.compareTo(c)){
_passwordConfirmController.addError("No Match");
}
}); 

问候,Mariano

相关内容

  • 没有找到相关文章

最新更新