Это мой экран смены пароля. Я использовал flutter_blo c для реализации шаблона mvv c. Эта страница отлично работает с blo c. Но то, что я пытаюсь достичь, это проверить форму при отправке формы. Поскольку я был новичком, я не знаю, как это сделать.
Событие смены пароля
abstract class ChangePasswordEvent extends Equatable {
const ChangePasswordEvent();
}
class SubmitButtonPressed extends ChangePasswordEvent {
final String oldPassword;
final String newPassword;
const SubmitButtonPressed({@required this.oldPassword, this.newPassword});
@override
List<Object> get props => [oldPassword, newPassword];
}
Изменение состояния пароля
abstract class ChangePasswordState extends Equatable {
const ChangePasswordState();
@override
List<Object> get props => [];
}
class ChangePasswordInitial extends ChangePasswordState {}
class ChangePasswordLoading extends ChangePasswordState {}
class ChangePasswordSuccess extends ChangePasswordState {}
class ChangePasswordFailure extends ChangePasswordState {
final String error;
const ChangePasswordFailure({@required this.error});
@override
List<Object> get props => [error];
@override
String toString() => 'ChangePasswordFailure { error: $error }';
}
Изменить пароль Blo c
class ChangePasswordBloc
extends Bloc<ChangePasswordEvent, ChangePasswordState> {
final UserRepository userRepository;
ChangePasswordBloc({
@required this.userRepository,
}) : assert(userRepository != null);
@override
ChangePasswordState get initialState => ChangePasswordInitial();
@override
Stream<ChangePasswordState> mapEventToState(
ChangePasswordEvent event) async* {
if (event is SubmitButtonPressed) {
yield ChangePasswordLoading();
try {
final bool isPasswordChanged = await userRepository.changePassword(
event.oldPassword,
event.newPassword,
);
if (isPasswordChanged) {
yield ChangePasswordSuccess();
}
} catch (error) {
yield ChangePasswordFailure(error: error);
}
}
}
}
Изменить пароль Страница
class ChangePasswordPage extends StatelessWidget {
final UserRepository userRepository;
ChangePasswordPage({Key key, @required this.userRepository})
: assert(userRepository != null),
super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Change Password'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: BlocProvider(
create: (context) {
return ChangePasswordBloc(
userRepository: userRepository,
);
},
child: ChangePasswordForm(),
),
),
);
}
}
Изменить Форма пароля
class ChangePasswordForm extends StatefulWidget {
@override
_ChangePasswordFormState createState() => _ChangePasswordFormState();
}
class _ChangePasswordFormState extends State<ChangePasswordForm> {
final userRepository = UserRepository();
final _formKey = GlobalKey<FormState>();
final _oldPassController = TextEditingController();
final _newPassController = TextEditingController();
final _confirmPassController = TextEditingController();
@override
Widget build(BuildContext context) {
_onSubmitButtonPressed() {
BlocProvider.of<ChangePasswordBloc>(context).add(
SubmitButtonPressed(
oldPassword: _oldPassController.text,
newPassword: _newPassController.text,
),
);
}
return BlocListener<ChangePasswordBloc, ChangePasswordState>(
listener: (context, state) {
if (state is ChangePasswordFailure) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('${state.error}'),
backgroundColor: Colors.red,
),
);
}
if (state is ChangePasswordSuccess) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('Password Changed Successfully'),
backgroundColor: Colors.green,
),
);
}
},
child: BlocBuilder<ChangePasswordBloc, ChangePasswordState>(
builder: (context, state) {
return Form(
key: _formKey,
child: Column(
children: [
TextFormField(
decoration: InputDecoration(labelText: 'Old Password'),
controller: _oldPassController,
),
SizedBox(height: 20.0),
TextFormField(
decoration: InputDecoration(labelText: 'New Password'),
controller: _newPassController,
obscureText: true,
),
SizedBox(height: 20.0),
TextFormField(
decoration: InputDecoration(labelText: 'Confirm Password'),
controller: _confirmPassController,
obscureText: true,
validator: (value) {
final String _newPassword = _newPassController.text;
if (_newPassword != value) {
return "Password Mismatch";
}
return null;
},
),
SizedBox(height: 20.0),
RaisedButton(
onPressed: () {
if (state is! ChangePasswordLoading) {
final form = _formKey.currentState;
if (form.validate()) {
return _onSubmitButtonPressed();
}
return null;
}
},
child: Text('Submit'),
),
],
),
);
},
),
);
}
}