state.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import 'package:get/get.dart';
  2. ///登出界面状态
  3. class SignUpState {
  4. final Rx<bool> _onSubmit = Rx(false);
  5. final Rx<bool> _isShowCodeRow = Rx(true);
  6. final Rx<bool> _isCodeSend = Rx(false);
  7. final Rx<bool> _isAgreeClause = Rx(false);
  8. final Rx<String> _account = Rx('');
  9. final Rx<String> _password = Rx('');
  10. final Rx<String> _reenterPassword = Rx('');
  11. final Rx<String> _verificationCode = Rx('');
  12. final Rx<int> _codeWaitSecond = Rx(0);
  13. /// 账号(手机/邮箱/用户名)
  14. String get account => _account.value;
  15. set account(String val) => _account(val);
  16. /// 密码
  17. String get password => _password.value;
  18. set password(String val) => _password(val);
  19. /// 确认密码
  20. String get reenterPassword => _reenterPassword.value;
  21. set reenterPassword(String val) => _reenterPassword(val);
  22. /// 验证码
  23. String get verificationCode => _verificationCode.value;
  24. set verificationCode(String val) => _verificationCode(val);
  25. /// 是否显示验证码行
  26. bool get isShowCodeRow => _isShowCodeRow.value;
  27. set isShowCodeRow(bool val) => _isShowCodeRow(val);
  28. /// 验证码是否已发送
  29. bool get isCodeSend => _isCodeSend.value;
  30. set isCodeSend(bool val) => _isCodeSend(val);
  31. /// 验证码等待秒数
  32. int get codeWaitSecond => _codeWaitSecond.value;
  33. set codeWaitSecond(int val) => _codeWaitSecond(val);
  34. /// 是否同意条款和政策
  35. bool get isAgreeClause => _isAgreeClause.value;
  36. set isAgreeClause(bool val) => _isAgreeClause(val);
  37. /// 提交中
  38. bool get onSubmit => _onSubmit.value;
  39. set onSubmit(bool val) => _onSubmit(val);
  40. /// 重置状态
  41. reset() {
  42. account = '';
  43. password = '';
  44. reenterPassword = '';
  45. verificationCode = '';
  46. isShowCodeRow = true;
  47. }
  48. }
  49. class SignUpFormValidateState {
  50. SignUpFormValidateState(this.data);
  51. final SignUpState data;
  52. final Rx<bool> _asyncValidating = Rx(false);
  53. final Rx<bool> _accountBeOccupied = Rx(false);
  54. /// 正在异步验证
  55. bool get asyncValidating => _asyncValidating.value;
  56. set asyncValidating(bool val) => _asyncValidating.value = val;
  57. /// 账号被占用
  58. bool get accountBeOccupied => _accountBeOccupied.value;
  59. set accountBeOccupied(bool val) => _accountBeOccupied.value = val;
  60. /// 账号不为空
  61. bool get accountIsNotEmpty => data.account.isNotEmpty;
  62. /// 账号格式正确
  63. bool get accountFormatCheck {
  64. final account = data.account;
  65. // 判断是否为邮箱
  66. final RegExp emailRegExp = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
  67. if (emailRegExp.hasMatch(account)) {
  68. return true;
  69. }
  70. if (true) {
  71. // 判断是否为手机号
  72. final RegExp phoneRegExp = RegExp(r'^1[0-9]{10}$');
  73. if (phoneRegExp.hasMatch(account)) {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. /// 账号表单验证通过
  80. bool get accountChecked =>
  81. accountIsNotEmpty && accountFormatCheck && !accountBeOccupied;
  82. /// 密码不为空
  83. bool get pwdIsNotEmpty => data.password.isNotEmpty;
  84. /// 密码不包含空格
  85. bool get pwdNotContainBlank => !data.password.contains(' ');
  86. /// 密码长度正确
  87. bool get pwdLengthChecked =>
  88. data.password.length >= 6 && data.password.length <= 20;
  89. /// 密码字符组合正确
  90. bool get pwdCharCombinChecked {
  91. final pwd = data.password;
  92. int pwdMatchNum = 0;
  93. // 验证包含符号 - 非字母数字
  94. if (RegExp(r'[^\da-zA-Z\s]').hasMatch(pwd)) pwdMatchNum++;
  95. // 验证包含字母
  96. if (RegExp(r'[a-zA-Z]').hasMatch(pwd)) pwdMatchNum++;
  97. // 验证包含数字
  98. if (RegExp(r'\d').hasMatch(pwd)) pwdMatchNum++;
  99. return pwdMatchNum > 1;
  100. }
  101. /// 密码表单验证通过
  102. bool get pwdChecked =>
  103. pwdIsNotEmpty &&
  104. pwdNotContainBlank &&
  105. pwdLengthChecked &&
  106. pwdCharCombinChecked;
  107. /// 确认密码不为空
  108. bool get reenterPwdIsNotEmpty => data.reenterPassword.isNotEmpty;
  109. /// 两次密码输入一致
  110. bool get reenterPwdEqualsPwd => data.password == data.reenterPassword;
  111. /// 确认密码表单验证通过
  112. bool get reenterPwdChecked => reenterPwdIsNotEmpty && reenterPwdEqualsPwd;
  113. /// 验证码验证通过
  114. bool get verificationCodeChecked => data.verificationCode.isNotEmpty;
  115. /// 是否可提交
  116. bool get isCanSubmit {
  117. if (asyncValidating) return false;
  118. if (data.isShowCodeRow && data.verificationCode.isEmpty) {
  119. return false;
  120. }
  121. return accountChecked && pwdChecked && reenterPwdChecked;
  122. }
  123. }
  124. class SignUpFormEditState {
  125. final Rx<bool> _accountEdited = Rx(false);
  126. final Rx<bool> _pwdEdited = Rx(false);
  127. final Rx<bool> _reenterPwdEdited = Rx(false);
  128. final Rx<bool> _verificationCodeEdited = Rx(false);
  129. /// 账号已编辑
  130. bool get accountEdited => _accountEdited.value;
  131. set accountEdited(bool val) => _accountEdited.value = val;
  132. /// 密码已编辑
  133. bool get pwdEdited => _pwdEdited.value;
  134. set pwdEdited(bool val) => _pwdEdited.value = val;
  135. /// 确认密码已编辑
  136. bool get reenterPwdEdited => _reenterPwdEdited.value;
  137. set reenterPwdEdited(bool val) => _reenterPwdEdited.value = val;
  138. /// 验证码已编辑
  139. bool get verificationCodeEdited => _verificationCodeEdited.value;
  140. set verificationCodeEdited(bool val) => _verificationCodeEdited.value = val;
  141. /// 清除状态
  142. void clear() {
  143. accountEdited = false;
  144. pwdEdited = false;
  145. reenterPwdEdited = false;
  146. verificationCodeEdited = false;
  147. }
  148. }