auth_login_page.dart 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import 'package:cpt_auth/modules/auth_login/auth_login_state.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:cs_resources/generated/l10n.dart';
  4. import 'package:cs_resources/theme/app_colors_theme.dart';
  5. import 'package:flutter/gestures.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:auto_route/auto_route.dart';
  8. import 'package:hooks_riverpod/hooks_riverpod.dart';
  9. import 'package:router/ext/auto_router_extensions.dart';
  10. import 'package:widgets/ext/ex_widget.dart';
  11. import 'package:widgets/my_appbar.dart';
  12. import 'package:widgets/my_button.dart';
  13. import 'package:widgets/my_load_image.dart';
  14. import 'package:widgets/my_text_field.dart';
  15. import 'package:widgets/my_text_view.dart';
  16. import 'package:widgets/shatter/custom_check_box.dart';
  17. import 'package:widgets/widget_export.dart';
  18. import '../../router/page/auth_page_router.dart';
  19. import 'auth_login_view_model.dart';
  20. /*
  21. * 用户的登录页面
  22. */
  23. @RoutePage()
  24. class AuthLoginPage extends HookConsumerWidget {
  25. const AuthLoginPage({Key? key}) : super(key: key);
  26. //启动当前页面
  27. static void startInstance({BuildContext? context}) {
  28. if (context != null) {
  29. context.router.navigate(const AuthLoginPageRoute());
  30. } else {
  31. appRouter.navigate(const AuthLoginPageRoute());
  32. }
  33. }
  34. //替换之前的页面
  35. static void startAndPopAll({BuildContext? context}) {
  36. if (context != null) {
  37. context.router.popUntilRoot();
  38. context.router.replace(const AuthLoginPageRoute());
  39. } else {
  40. appRouter.popUntilRoot();
  41. appRouter.replace(const AuthLoginPageRoute());
  42. }
  43. }
  44. @override
  45. Widget build(BuildContext context, WidgetRef ref) {
  46. final viewModel = ref.read(authLoginViewModelProvider.notifier);
  47. final state = ref.watch(authLoginViewModelProvider);
  48. return Scaffold(
  49. appBar: MyAppBar.appBar(context, "", showBackButton: false),
  50. backgroundColor: context.appColors.backgroundDefault,
  51. body: SingleChildScrollView(
  52. scrollDirection: Axis.vertical,
  53. physics: const BouncingScrollPhysics(),
  54. child: Container(
  55. margin: const EdgeInsets.symmetric(horizontal: 38),
  56. width: double.infinity,
  57. child: Column(
  58. mainAxisSize: MainAxisSize.max,
  59. crossAxisAlignment: CrossAxisAlignment.center,
  60. children: [
  61. //顶部Logo
  62. const MyAssetImage(
  63. Assets.assetsYyHomeLogo,
  64. width: 85.5,
  65. height: 85.5,
  66. ).marginOnly(top: 45, bottom: 45),
  67. // 登录表单 - 账号
  68. _buildInputLayout(
  69. context,
  70. state,
  71. "account",
  72. textInputAction: TextInputAction.next,
  73. errorText: state.accountErrorText,
  74. onSubmit: (formKey, value) {
  75. state.formData[formKey]!['focusNode'].unfocus();
  76. FocusScope.of(context).requestFocus(state.formData['password']!['focusNode']);
  77. },
  78. ),
  79. // 登录表单 - 密码
  80. _buildInputLayout(
  81. context,
  82. state,
  83. "password",
  84. marginTop: 15,
  85. obscureText: !state.pwdVisibility,
  86. errorText: state.passwordErrorText,
  87. showRightIcon: true,
  88. rightWidget: IconButton(
  89. highlightColor: Colors.transparent,
  90. splashColor: Colors.transparent,
  91. icon: state.pwdVisibility
  92. ? const MyAssetImage(
  93. Assets.authPasswordHide,
  94. width: 22.5,
  95. height: 16.5,
  96. )
  97. : const MyAssetImage(
  98. Assets.authPasswordShow,
  99. width: 22.5,
  100. height: 16.5,
  101. ),
  102. onPressed: () {
  103. viewModel.switchPwdVisibility();
  104. },
  105. ),
  106. onSubmit: (formKey, value) {
  107. state.formData[formKey]!['focusNode'].unfocus();
  108. viewModel.doLogin();
  109. },
  110. ),
  111. //登录按钮
  112. MyButton(
  113. onPressed: viewModel.doLogin,
  114. text: S.current.login,
  115. enable: state.isLoginBtnEnable,
  116. textColor: Colors.white,
  117. disabledTextColor: Colors.white,
  118. backgroundColor: context.appColors.btnBgDefault,
  119. disabledBackgroundColor: context.appColors.btnBgDefault.withOpacity(0.2),
  120. fontWeight: FontWeight.w500,
  121. type: ClickType.throttle,
  122. minHeight: 50,
  123. radius: 5,
  124. ).marginOnly(top: 20, bottom: 23),
  125. //忘记密码
  126. MyTextView(
  127. S.current.forgot_password,
  128. isFontMedium: true,
  129. fontSize: 16,
  130. textColor: context.appColors.textPrimary,
  131. onClick: viewModel.gotoForgotPage,
  132. ),
  133. //创建账户-注册
  134. MyButton(
  135. onPressed: viewModel.gotoSignUpPage,
  136. text: S.current.create_new_yy_home_account,
  137. textColor: Colors.white,
  138. backgroundColor: context.appColors.btnBgDefault,
  139. fontWeight: FontWeight.w500,
  140. type: ClickType.throttle,
  141. minHeight: 50,
  142. radius: 5,
  143. ).marginOnly(top: 28),
  144. //同意协议
  145. Row(
  146. mainAxisSize: MainAxisSize.min,
  147. children: [
  148. MyAssetImage(
  149. state.isAgreeTerms ? Assets.baseServiceCheckBoxChecked : Assets.baseServiceCheckBoxUncheck,
  150. width: 15.5,
  151. height: 15.5,
  152. ).onTap(viewModel.switchAgreeTerms, padding: 10),
  153. RichText(
  154. text: TextSpan(
  155. children: [
  156. TextSpan(
  157. text: S.current.agree_to,
  158. style: TextStyle(color: context.appColors.textDarkGray, fontWeight: FontWeight.w500, fontSize: 15), // 灰色文本
  159. ),
  160. const TextSpan(
  161. text: " ",
  162. ),
  163. TextSpan(
  164. text: S.current.terms_of_service,
  165. style: TextStyle(color: context.appColors.textPrimary, fontWeight: FontWeight.w500, fontSize: 15), // 蓝色文本
  166. recognizer: TapGestureRecognizer()..onTap = viewModel.gotoTermsPage,
  167. ),
  168. ],
  169. ),
  170. ),
  171. ],
  172. ).marginOnly(top: 30, bottom: 30),
  173. //结束
  174. ],
  175. ),
  176. ),
  177. ),
  178. );
  179. }
  180. /// 输入框 账号与密码
  181. Widget _buildInputLayout(
  182. BuildContext context,
  183. LoginState state,
  184. String key, {
  185. double marginTop = 0,
  186. bool? showRightIcon = false, //是否展示右侧的布局
  187. Widget? rightWidget, //右侧的布局
  188. TextInputType textInputType = TextInputType.text,
  189. String? errorText,
  190. bool obscureText = false,
  191. TextInputAction textInputAction = TextInputAction.done,
  192. Function? onSubmit,
  193. }) {
  194. return IgnoreKeyboardDismiss(
  195. child: MyTextField(
  196. key,
  197. fillBackgroundColor: context.appColors.authFiledBG,
  198. state.formData[key]!['value'],
  199. hintText: state.formData[key]!['hintText'],
  200. hintStyle: TextStyle(
  201. color: context.appColors.authFiledHint,
  202. fontSize: 16.0,
  203. fontWeight: FontWeight.w500,
  204. ),
  205. controller: state.formData[key]!['controller'],
  206. focusNode: state.formData[key]!['focusNode'],
  207. margin: EdgeInsets.only(top: marginTop),
  208. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 3),
  209. showDivider: false,
  210. height: 44,
  211. style: TextStyle(
  212. color: context.appColors.authFiledText,
  213. fontSize: 16.0,
  214. fontWeight: FontWeight.w500,
  215. ),
  216. inputType: textInputType,
  217. textInputAction: textInputAction,
  218. onSubmit: onSubmit,
  219. cursorColor: context.appColors.authFiledText,
  220. obscureText: obscureText,
  221. errorText: errorText,
  222. showLeftIcon: true,
  223. showRightIcon: showRightIcon,
  224. rightWidget: rightWidget,
  225. ),
  226. );
  227. }
  228. }