我正在使用flutter_bloc库来创建验证码页面。这是我试图做的。
class PhonePage extends StatelessWidget {
static Route route() {
return MaterialPageRoute<void>(builder: (_) => PhonePage());
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocProvider(
create: (_) =>
ValidationCubit(context.repository<AuthenticationRepository>()),
child: PhoneForm(),
),
);
}
}
class PhoneForm extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocConsumer<ValidationCubit, ValidationState>(
listener: (context, state) {
print('Listener has been called');
if (state.status.isSubmissionFailure) {
_showVerificationError(context);
} else if (state.status.isSubmissionSuccess) {
_showVerificationSuccess(context);
}
},
builder: (context, state) {
return Container(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_HeaderAndTitle(),
_VerificationInput(),
_VerifyButton()
],
),
),
),
);
},
);
}
void _showVerificationError(context) {
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(const SnackBar(content: Text('Validation error')));
}
void _showVerificationSuccess(context) {
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(const SnackBar(
content: Text('Validation Success'),
backgroundColor: Colors.green,
));
}
}
...
class _VerifyButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<ValidationCubit, ValidationState>(
builder: (context, state) {
return RaisedButton.icon(
color: Colors.blue,
padding: EdgeInsets.symmetric(horizontal: 38.0, vertical: 12.0),
textColor: Colors.white,
icon: state.status.isSubmissionInProgress
? Icon(FontAwesomeIcons.ellipsisH)
: Icon(null),
label: Text(state.status.isSubmissionInProgress ? '' : 'Verify',
style: TextStyle(fontSize: 16.0)),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
onPressed: state.status.isValid
? () => context.bloc<ValidationCubit>().verifyCode()
: null);
});
}
}
现在verifyCode()函数是在ValidationCubit内部定义的异步函数。它发出状态设置为加载,成功和失败的状态。但是,听众不会选择这些更改并显示小吃店。我不知道为什么?我还按照flutter_bloc文档中的建议使用Formz库。这是verifyCode部分。
Future<void> verifyCode() async {
if (!state.status.isValidated) return;
emit(state.copyWith(status: FormzStatus.submissionInProgress));
try {
// send validation code to server here
await _authenticationRepository.loginWithEmailAndPassword(
email: 'email@email.com', password: '12');
emit(state.copyWith(status: FormzStatus.submissionSuccess));
} on Exception {
emit(state.copyWith(status: FormzStatus.submissionFailure));
}
}
验证码模型如下:
class ValidationState extends Equatable {
final VerificationCode verificationCode;
final FormzStatus status;
const ValidationState({this.verificationCode, this.status});
@override
List<Object> get props => [verificationCode];
ValidationState copyWith(
{VerificationCode verificationCode, FormzStatus status}) {
return ValidationState(
verificationCode: verificationCode ?? this.verificationCode,
status: status ?? this.status);
}
}
验证状态类为:
class ValidationState extends Equatable {
final VerificationCode verificationCode;
final FormzStatus status;
const ValidationState({this.verificationCode, this.status});
@override
List<Object> get props => [verificationCode];
ValidationState copyWith(
{VerificationCode verificationCode, FormzStatus status}) {
return ValidationState(
verificationCode: verificationCode ?? this.verificationCode,
status: status ?? this.status);
}
}
答案 0 :(得分:1)
我认为问题在于您的州立课程。
与BlocBuilder中的builder不同,每次状态更改(不包括初始状态)都仅调用一次监听器,并且它是一个void函数。
Cubit每次发出新状态时,都会将其与前一个状态进行比较,如果状态为“ DIFFERENT”,则会触发侦听器功能。
在您所处的情况下,您仅使用verificationCode
作为props
的Equatable,这意味着当比较两个状态时,仅测试verificationCode
。这样,BLoC消费者认为这两个状态相等,不会再次触发侦听器功能。
如果您检查verifyCode()
函数,则唯一更改的参数是status
。
为了解决此问题,将status
属性添加到状态类的props
列表中。
@override
List<Object> get props => [this.verificationCode, this.status];
答案 1 :(得分:0)
如果你想更新相同的状态,只需在调用更新状态之前添加一个状态 像这样 如果您想再次更新“加载”状态,请在此之前调用“加载”状态,然后调用“加载”状态,以便 BlocListener 和 BlocBuilder 将侦听它
已编辑 为此,我已将 bloc 更改为 cubit,并且 cubit 可以连续发出相同的状态,并且 bloclistener 和 blocbuilder 可以收听它