You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.5KB

  1. import 'package:farm_tpf/custom_model/password.dart';
  2. import 'package:farm_tpf/data/api/app_exception.dart';
  3. import 'package:farm_tpf/data/repository/user_repository.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  7. import 'package:farm_tpf/utils/const_color.dart';
  8. import 'package:farm_tpf/utils/validators.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  11. class ChangePasswordScreen extends StatefulWidget {
  12. @override
  13. _ChangePasswordScreenState createState() => _ChangePasswordScreenState();
  14. }
  15. class _ChangePasswordScreenState extends State<ChangePasswordScreen> {
  16. final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  17. final _repository = UserRepository();
  18. GlobalKey<FormState> _formKey = GlobalKey();
  19. bool _autoValidate = false;
  20. Password _password = Password();
  21. TextEditingController _currentPasswordController = TextEditingController();
  22. TextEditingController _newPasswordController = TextEditingController();
  23. TextEditingController _confirmPasswordController = TextEditingController();
  24. @override
  25. void initState() {
  26. super.initState();
  27. }
  28. _validateInputs() async {
  29. if (_formKey.currentState!.validate()) {
  30. _formKey.currentState!.save();
  31. LoadingDialog.showLoadingDialog(context);
  32. _repository.changePassword(_password).then((value) {
  33. LoadingDialog.hideLoadingDialog(context);
  34. Navigator.pop(context);
  35. Utils.showSnackBarSuccess(message: "Cập nhật thành công");
  36. }).catchError((onError) {
  37. LoadingDialog.hideLoadingDialog(context);
  38. Utils.showSnackBarError(message: AppException.handleError(onError, customMessageError: "Sai mật khẩu hiện tại"));
  39. });
  40. } else {
  41. _autoValidate = true;
  42. }
  43. }
  44. Widget _currentPasswordField() {
  45. return TextFormField(
  46. keyboardType: TextInputType.text,
  47. obscureText: true,
  48. decoration: const InputDecoration(labelText: "Mật khẩu hiện tại"),
  49. controller: _currentPasswordController,
  50. validator: (String? value) {
  51. return Validators.validateNotNullOrEmpty(value ?? '', "Mật khẩu hiện tại");
  52. },
  53. onSaved: (newValue) {
  54. _password.currentPassword = newValue ?? '';
  55. },
  56. );
  57. }
  58. Widget _newPasswordField() {
  59. return TextFormField(
  60. keyboardType: TextInputType.text,
  61. obscureText: true,
  62. decoration: const InputDecoration(labelText: "Mật khẩu mới"),
  63. controller: _newPasswordController,
  64. validator: (v) {
  65. return validators.validateNewPassword(v ?? '');
  66. },
  67. onSaved: (newValue) {
  68. _password.newPassword = newValue ?? '';
  69. },
  70. );
  71. }
  72. Widget _confirmPasswordField() {
  73. return TextFormField(
  74. keyboardType: TextInputType.text,
  75. obscureText: true,
  76. decoration: const InputDecoration(labelText: "Nhập lại mật khẩu mới"),
  77. controller: _confirmPasswordController,
  78. validator: (String? value) {
  79. return validators.validateConfirmPassword(_newPasswordController.text, value ?? '');
  80. },
  81. onSaved: (newValue) {},
  82. );
  83. }
  84. Widget _btnSubmit() {
  85. return SizedBox(
  86. width: double.infinity,
  87. height: 55,
  88. child: FlatButton(
  89. onPressed: () {
  90. var currentFocus = FocusScope.of(context);
  91. if (!currentFocus.hasPrimaryFocus) {
  92. currentFocus.unfocus();
  93. }
  94. _validateInputs();
  95. },
  96. color: AppColors.DEFAULT,
  97. shape: RoundedRectangleBorder(
  98. borderRadius: BorderRadius.circular(7.0),
  99. ),
  100. child: Text(
  101. 'Cập nhật'.toUpperCase(),
  102. style: TextStyle(fontWeight: FontWeight.bold, color: AppColors.WHITE),
  103. ),
  104. ),
  105. );
  106. }
  107. @override
  108. Widget build(BuildContext context) => KeyboardDismisser(
  109. child: Scaffold(
  110. backgroundColor: Colors.white,
  111. key: _scaffoldKey,
  112. appBar: AppBarWidget(
  113. isBack: true,
  114. ),
  115. body: KeyboardDismisser(
  116. child: Form(
  117. key: _formKey,
  118. // autovalidate: _autoValidate,
  119. child: SingleChildScrollView(
  120. padding: const EdgeInsets.all(8.0),
  121. child: Column(
  122. crossAxisAlignment: CrossAxisAlignment.start,
  123. children: <Widget>[
  124. const Text(
  125. 'Bảo mật',
  126. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
  127. ),
  128. const Text('Cập nhật mật khẩu cho tài khoản của bạn. Mật khẩu bao gồm cả chữ và số, độ dài tối thiểu 8 ký tự',
  129. style: TextStyle(fontSize: 15, fontWeight: FontWeight.w200, color: Colors.black54)),
  130. _currentPasswordField(),
  131. const SizedBox(
  132. height: 8.0,
  133. ),
  134. _newPasswordField(),
  135. const SizedBox(
  136. height: 8.0,
  137. ),
  138. _confirmPasswordField(),
  139. const SizedBox(
  140. height: 20.0,
  141. ),
  142. _btnSubmit()
  143. ],
  144. ),
  145. )))));
  146. }