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.

150 lines
5.2KB

  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 = new 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: 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: InputDecoration(labelText: "Mật khẩu mới"),
  63. controller: _newPasswordController,
  64. validator: validators.validateNewPassword,
  65. onSaved: (newValue) {
  66. _password.newPassword = newValue;
  67. },
  68. );
  69. }
  70. Widget _confirmPasswordField() {
  71. return TextFormField(
  72. keyboardType: TextInputType.text,
  73. obscureText: true,
  74. decoration: InputDecoration(labelText: "Nhập lại mật khẩu mới"),
  75. controller: _confirmPasswordController,
  76. validator: (String? value) {
  77. return validators.validateConfirmPassword(_newPasswordController.text, value ?? '');
  78. },
  79. onSaved: (newValue) {},
  80. );
  81. }
  82. Widget _btnSubmit() {
  83. return SizedBox(
  84. width: double.infinity,
  85. height: 55,
  86. child: TextButton(
  87. onPressed: () {
  88. FocusScopeNode currentFocus = FocusScope.of(context);
  89. if (!currentFocus.hasPrimaryFocus) {
  90. currentFocus.unfocus();
  91. }
  92. _validateInputs();
  93. },
  94. child: Text(
  95. 'Cập nhật'.toUpperCase(),
  96. style: TextStyle(fontWeight: FontWeight.bold, color: AppColors.WHITE),
  97. ),
  98. ),
  99. );
  100. }
  101. @override
  102. Widget build(BuildContext context) => KeyboardDismisser(
  103. child: Scaffold(
  104. backgroundColor: Colors.white,
  105. key: _scaffoldKey,
  106. appBar: AppBarWidget(
  107. isBack: true,
  108. ),
  109. body: KeyboardDismisser(
  110. child: Form(
  111. key: _formKey,
  112. child: SingleChildScrollView(
  113. padding: EdgeInsets.all(8.0),
  114. child: Column(
  115. crossAxisAlignment: CrossAxisAlignment.start,
  116. children: <Widget>[
  117. Text(
  118. 'Bảo mật',
  119. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
  120. ),
  121. 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ự',
  122. style: TextStyle(fontSize: 15, fontWeight: FontWeight.w200, color: Colors.black54)),
  123. _currentPasswordField(),
  124. SizedBox(
  125. height: 8.0,
  126. ),
  127. _newPasswordField(),
  128. SizedBox(
  129. height: 8.0,
  130. ),
  131. _confirmPasswordField(),
  132. SizedBox(
  133. height: 20.0,
  134. ),
  135. _btnSubmit()
  136. ],
  137. ),
  138. )))));
  139. }