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.

163 lines
5.6KB

  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(
  39. message: AppException.handleError(onError,
  40. customMessageError: "Sai mật khẩu hiện tại"));
  41. });
  42. } else {
  43. _autoValidate = true;
  44. }
  45. }
  46. Widget _currentPasswordField() {
  47. return TextFormField(
  48. keyboardType: TextInputType.text,
  49. obscureText: true,
  50. decoration: InputDecoration(labelText: "Mật khẩu hiện tại"),
  51. controller: _currentPasswordController,
  52. validator: (String value) {
  53. return Validators.validateNotNullOrEmpty(value, "Mật khẩu hiện tại");
  54. },
  55. onSaved: (newValue) {
  56. _password.currentPassword = newValue;
  57. },
  58. );
  59. }
  60. Widget _newPasswordField() {
  61. return TextFormField(
  62. keyboardType: TextInputType.text,
  63. obscureText: true,
  64. decoration: InputDecoration(labelText: "Mật khẩu mới"),
  65. controller: _newPasswordController,
  66. validator: validators.validateNewPassword,
  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: InputDecoration(labelText: "Nhập lại mật khẩu mới"),
  77. controller: _confirmPasswordController,
  78. validator: (String value) {
  79. return validators.validateConfirmPassword(
  80. _newPasswordController.text, value);
  81. },
  82. onSaved: (newValue) {},
  83. );
  84. }
  85. Widget _btnSubmit() {
  86. return SizedBox(
  87. width: double.infinity,
  88. height: 55,
  89. child: FlatButton(
  90. onPressed: () {
  91. FocusScopeNode currentFocus = FocusScope.of(context);
  92. if (!currentFocus.hasPrimaryFocus) {
  93. currentFocus.unfocus();
  94. }
  95. _validateInputs();
  96. },
  97. color: AppColors.DEFAULT,
  98. shape: RoundedRectangleBorder(
  99. borderRadius: new BorderRadius.circular(7.0),
  100. ),
  101. child: Text(
  102. 'Cập nhật'.toUpperCase(),
  103. style: TextStyle(fontWeight: FontWeight.bold, color: AppColors.WHITE),
  104. ),
  105. ),
  106. );
  107. }
  108. @override
  109. Widget build(BuildContext context) => KeyboardDismisser(
  110. child: Scaffold(
  111. backgroundColor: Colors.white,
  112. key: _scaffoldKey,
  113. appBar: AppBarWidget(
  114. isBack: true,
  115. ),
  116. body: KeyboardDismisser(
  117. child: Form(
  118. key: _formKey,
  119. autovalidate: _autoValidate,
  120. child: SingleChildScrollView(
  121. padding: EdgeInsets.all(8.0),
  122. child: Column(
  123. crossAxisAlignment: CrossAxisAlignment.start,
  124. children: <Widget>[
  125. Text(
  126. 'Bảo mật',
  127. style: TextStyle(
  128. fontWeight: FontWeight.w500, fontSize: 22),
  129. ),
  130. Text(
  131. '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ự',
  132. style: TextStyle(
  133. fontSize: 15,
  134. fontWeight: FontWeight.w200,
  135. color: Colors.black54)),
  136. _currentPasswordField(),
  137. SizedBox(
  138. height: 8.0,
  139. ),
  140. _newPasswordField(),
  141. SizedBox(
  142. height: 8.0,
  143. ),
  144. _confirmPasswordField(),
  145. SizedBox(
  146. height: 20.0,
  147. ),
  148. _btnSubmit()
  149. ],
  150. ),
  151. )))));
  152. }