我正在研究MVP架构。我已经实现了一个。
查看模型:
class AuthenticationViewModel {
String phone;
bool isEmailValidate;
AuthenticationViewModel(){
phone = null;
isEmailValidate = false;
}
}
演示者:
class AuthenticationPresenter{
void onValidatePhone(String value){}
set authenticateView(AuthenticationView value) {}
}
class BasicAuthenticationPresenter implements AuthenticationPresenter {
AuthenticationViewModel _authenticationViewModel;
AuthenticationView _authenticationView;
BasicAuthenticationPresenter() {
this._authenticationViewModel = new AuthenticationViewModel();
}
@override
void onValidatePhone(String value) {
if(PhoneValidator.isPhoneValidate(value))
this._authenticationView.validatePhone(true, value);
else
this._authenticationView.validatePhone(false, null);
}
@override
set authenticateView(AuthenticationView value) {
_authenticationView = value;
this._authenticationView.refreshAuthentication(this._authenticationViewModel);
}
}
查看:
class AuthenticationView {
void refreshAuthentication(AuthenticationViewModel value) {
print("refreshCounter not implemented yet");
}
void validatePhone(bool value, String phone) {
print("validatePhone not implemented yet");
}
}
组件:
class Authentication extends StatefulWidget {
final AuthenticationPresenter presenter;
Authentication(this.presenter);
@override
State<StatefulWidget> createState() {
return _AuthenticationState();
}
}
class _AuthenticationState extends State<Authentication> implements AuthenticationView{
AuthenticationViewModel _viewModel;
@override
void initState() {
super.initState();
this.widget.presenter.counterView = this;
}
@override
void refreshAuthentication(AuthenticationViewModel viewModel) {
setState(() {
this._viewModel = viewModel;
});
}
@override
void validatePhone(bool value, String phone) async {
// TODO: implement validatePhone
setState(() {
_viewModel.phone = phone;
_viewModel.isPhoneValidate = value;
});
}
}
问题是当我尝试通过演示者中的onValidatePhone方法检查电话验证时,没有执行this._authenticationView.validatePhone里面的内容,就像它仍然为null一样。
当我通过Navigator.pushNamed(context, pageNavigate);
打开布局时,就会发生这种麻烦
如果我直接从Main函数打开它,一切都会正常。
MaterialApp(
routes: routes,
title: 'DawiDari',
theme: ThemeData(
primaryColor: AppColors.primaryGreen,
),
home: new Authentication(new BasicAuthenticationPresenter())
)
答案 0 :(得分:0)
经过多次尝试,我找到了解决方案,如果有人有更好的解决方案,他可以发布它,因为我想要构建MVP架构的最佳方法。
我将Presenter更改为:
class AuthenticationPresenter{
void onValidatePhone(String value){}
}
class BasicAuthenticationPresenter implements AuthenticationPresenter {
AuthenticationViewModel _authenticationViewModel;
AuthenticationView _authenticationView;
BasicAuthenticationPresenter(this._authenticationView) {
this._authenticationViewModel = new AuthenticationViewModel();
this._authenticationView.refreshAuthentication(this._authenticationViewModel);
}
@override
void onValidatePhone(String value) {
if(PhoneValidator.isPhoneValidate(value))
this._authenticationView.validatePhone(true, value);
else
this._authenticationView.validatePhone(false, null);
}
}
我也将Component更改为:
class Authentication extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _AuthenticationState();
}
}
class _AuthenticationState extends State<Authentication> implements AuthenticationView{
AuthenticationPresenter presenter;
AuthenticationViewModel _viewModel;
@override
void initState() {
super.initState();
presenter = new BasicAuthenticationPresenter(this);
}
@override
void refreshAuthentication(AuthenticationViewModel viewModel) {
setState(() {
this._viewModel = viewModel;
});
}
@override
void validatePhone(bool value, String phone) async {
// TODO: implement validatePhone
setState(() {
_viewModel.phone = phone;
_viewModel.isPhoneValidate = value;
});
}
}