login_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:sino_med_cloud/l10n/app_localizations.dart';
  4. class LoginPage extends ConsumerStatefulWidget {
  5. const LoginPage({super.key});
  6. @override
  7. ConsumerState<LoginPage> createState() => _LoginPageState();
  8. }
  9. class _LoginPageState extends ConsumerState<LoginPage>
  10. with SingleTickerProviderStateMixin {
  11. late TabController _tabController;
  12. final _passwordFormKey = GlobalKey<FormState>();
  13. final _smsFormKey = GlobalKey<FormState>();
  14. // 密码登录表单
  15. final _phoneController = TextEditingController();
  16. final _passwordController = TextEditingController();
  17. bool _obscurePassword = true;
  18. // 验证码登录表单
  19. final _phoneSmsController = TextEditingController();
  20. final _smsCodeController = TextEditingController();
  21. int _countdown = 0;
  22. @override
  23. void initState() {
  24. super.initState();
  25. _tabController = TabController(length: 2, vsync: this);
  26. }
  27. @override
  28. void dispose() {
  29. _tabController.dispose();
  30. _phoneController.dispose();
  31. _passwordController.dispose();
  32. _phoneSmsController.dispose();
  33. _smsCodeController.dispose();
  34. super.dispose();
  35. }
  36. // 发送验证码
  37. void _sendSmsCode() {
  38. final l10n = AppLocalizations.of(context)!;
  39. if (_phoneSmsController.text.isEmpty) {
  40. ScaffoldMessenger.of(context).showSnackBar(
  41. SnackBar(content: Text(l10n.phoneNumberRequiredForSms)),
  42. );
  43. return;
  44. }
  45. // TODO: 调用发送验证码接口
  46. setState(() {
  47. _countdown = 60;
  48. });
  49. // 倒计时
  50. Future.doWhile(() async {
  51. await Future.delayed(const Duration(seconds: 1));
  52. if (mounted) {
  53. setState(() {
  54. _countdown--;
  55. });
  56. }
  57. return _countdown > 0;
  58. });
  59. }
  60. // 密码登录
  61. void _handlePasswordLogin() {
  62. final l10n = AppLocalizations.of(context)!;
  63. if (_passwordFormKey.currentState!.validate()) {
  64. // TODO: 调用登录接口
  65. ScaffoldMessenger.of(context).showSnackBar(
  66. SnackBar(content: Text(l10n.loginNotImplemented)),
  67. );
  68. }
  69. }
  70. // 验证码登录
  71. void _handleSmsLogin() {
  72. final l10n = AppLocalizations.of(context)!;
  73. if (_smsFormKey.currentState!.validate()) {
  74. // TODO: 调用登录接口
  75. ScaffoldMessenger.of(context).showSnackBar(
  76. SnackBar(content: Text(l10n.loginNotImplemented)),
  77. );
  78. }
  79. }
  80. @override
  81. Widget build(BuildContext context) {
  82. final l10n = AppLocalizations.of(context)!;
  83. return Scaffold(
  84. body: GestureDetector(
  85. onTap: () {
  86. // 点击空白区域时收起键盘并移除焦点
  87. FocusScope.of(context).unfocus();
  88. },
  89. behavior: HitTestBehavior.opaque,
  90. child: SafeArea(
  91. child: SingleChildScrollView(
  92. padding: const EdgeInsets.all(24),
  93. child: Column(
  94. crossAxisAlignment: CrossAxisAlignment.stretch,
  95. children: [
  96. const SizedBox(height: 40),
  97. // Logo 或标题
  98. Text(
  99. l10n.appName,
  100. style: const TextStyle(
  101. fontSize: 32,
  102. fontWeight: FontWeight.bold,
  103. color: Color(0xFF1F2937),
  104. ),
  105. textAlign: TextAlign.center,
  106. ),
  107. const SizedBox(height: 8),
  108. Text(
  109. l10n.appSubtitle,
  110. style: const TextStyle(
  111. fontSize: 16,
  112. color: Color(0xFF6B7280),
  113. ),
  114. textAlign: TextAlign.center,
  115. ),
  116. const SizedBox(height: 48),
  117. // Tab 切换
  118. Container(
  119. decoration: BoxDecoration(
  120. color: Colors.white,
  121. borderRadius: BorderRadius.circular(12),
  122. ),
  123. child: TabBar(
  124. controller: _tabController,
  125. indicator: BoxDecoration(
  126. borderRadius: BorderRadius.circular(12),
  127. color: const Color(0xFF00BFA5),
  128. ),
  129. indicatorSize: TabBarIndicatorSize.tab,
  130. dividerColor: Colors.transparent,
  131. labelColor: Colors.white,
  132. unselectedLabelColor: const Color(0xFF6B7280),
  133. labelStyle: const TextStyle(
  134. fontSize: 16,
  135. fontWeight: FontWeight.w600,
  136. ),
  137. unselectedLabelStyle: const TextStyle(
  138. fontSize: 16,
  139. fontWeight: FontWeight.w500,
  140. ),
  141. tabs: [
  142. Tab(text: l10n.passwordLogin),
  143. Tab(text: l10n.smsLogin),
  144. ],
  145. ),
  146. ),
  147. const SizedBox(height: 24),
  148. // Tab 内容
  149. SizedBox(
  150. height: 400,
  151. child: TabBarView(
  152. controller: _tabController,
  153. children: [
  154. _buildPasswordLoginForm(),
  155. _buildSmsLoginForm(),
  156. ],
  157. ),
  158. ),
  159. ],
  160. ),
  161. ),
  162. ),
  163. ),
  164. );
  165. }
  166. // 密码登录表单
  167. Widget _buildPasswordLoginForm() {
  168. final l10n = AppLocalizations.of(context)!;
  169. return Form(
  170. key: _passwordFormKey,
  171. child: Column(
  172. crossAxisAlignment: CrossAxisAlignment.stretch,
  173. children: [
  174. const SizedBox(height: 4),
  175. // 手机号输入
  176. TextFormField(
  177. controller: _phoneController,
  178. keyboardType: TextInputType.phone,
  179. decoration: InputDecoration(
  180. labelText: l10n.phoneNumber,
  181. hintText: l10n.phoneNumberHint,
  182. prefixIcon: const Icon(Icons.phone_outlined),
  183. ),
  184. validator: (value) {
  185. if (value == null || value.isEmpty) {
  186. return l10n.phoneNumberRequired;
  187. }
  188. if (!RegExp(r'^1[3-9]\d{9}$').hasMatch(value)) {
  189. return l10n.phoneNumberInvalid;
  190. }
  191. return null;
  192. },
  193. ),
  194. const SizedBox(height: 16),
  195. // 密码输入
  196. TextFormField(
  197. controller: _passwordController,
  198. obscureText: _obscurePassword,
  199. decoration: InputDecoration(
  200. labelText: l10n.password,
  201. hintText: l10n.passwordHint,
  202. prefixIcon: const Icon(Icons.lock_outline),
  203. suffixIcon: IconButton(
  204. icon: Icon(
  205. _obscurePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined,
  206. ),
  207. onPressed: () {
  208. setState(() {
  209. _obscurePassword = !_obscurePassword;
  210. });
  211. },
  212. ),
  213. ),
  214. validator: (value) {
  215. if (value == null || value.isEmpty) {
  216. return l10n.passwordRequired;
  217. }
  218. if (value.length < 6) {
  219. return l10n.passwordMinLength;
  220. }
  221. return null;
  222. },
  223. ),
  224. const SizedBox(height: 8),
  225. // 忘记密码
  226. Align(
  227. alignment: Alignment.centerRight,
  228. child: TextButton(
  229. onPressed: () {
  230. // TODO: 跳转到忘记密码页面
  231. ScaffoldMessenger.of(context).showSnackBar(
  232. SnackBar(content: Text(l10n.forgotPasswordNotImplemented)),
  233. );
  234. },
  235. child: Text(l10n.forgotPassword),
  236. ),
  237. ),
  238. const SizedBox(height: 24),
  239. // 登录按钮
  240. ElevatedButton(
  241. onPressed: _handlePasswordLogin,
  242. style: ElevatedButton.styleFrom(
  243. padding: const EdgeInsets.symmetric(vertical: 16),
  244. ),
  245. child: Text(l10n.login),
  246. ),
  247. ],
  248. ),
  249. );
  250. }
  251. // 验证码登录表单
  252. Widget _buildSmsLoginForm() {
  253. final l10n = AppLocalizations.of(context)!;
  254. return Form(
  255. key: _smsFormKey,
  256. child: Column(
  257. crossAxisAlignment: CrossAxisAlignment.stretch,
  258. children: [
  259. const SizedBox(height: 4),
  260. // 手机号输入
  261. TextFormField(
  262. controller: _phoneSmsController,
  263. keyboardType: TextInputType.phone,
  264. decoration: InputDecoration(
  265. labelText: l10n.phoneNumber,
  266. hintText: l10n.phoneNumberHint,
  267. prefixIcon: const Icon(Icons.phone_outlined),
  268. ),
  269. validator: (value) {
  270. if (value == null || value.isEmpty) {
  271. return l10n.phoneNumberRequired;
  272. }
  273. if (!RegExp(r'^1[3-9]\d{9}$').hasMatch(value)) {
  274. return l10n.phoneNumberInvalid;
  275. }
  276. return null;
  277. },
  278. ),
  279. const SizedBox(height: 16),
  280. // 验证码输入
  281. Row(
  282. children: [
  283. Expanded(
  284. child: TextFormField(
  285. controller: _smsCodeController,
  286. keyboardType: TextInputType.number,
  287. decoration: InputDecoration(
  288. labelText: l10n.smsCode,
  289. hintText: l10n.smsCodeHint,
  290. prefixIcon: const Icon(Icons.sms_outlined),
  291. ),
  292. validator: (value) {
  293. if (value == null || value.isEmpty) {
  294. return l10n.smsCodeRequired;
  295. }
  296. if (value.length != 6) {
  297. return l10n.smsCodeLength;
  298. }
  299. return null;
  300. },
  301. ),
  302. ),
  303. const SizedBox(width: 12),
  304. SizedBox(
  305. width: 100,
  306. child: ElevatedButton(
  307. onPressed: _countdown > 0 ? null : _sendSmsCode,
  308. style: ElevatedButton.styleFrom(
  309. padding: const EdgeInsets.symmetric(vertical: 16),
  310. backgroundColor: _countdown > 0
  311. ? const Color(0xFFE5E7EB)
  312. : const Color(0xFF00BFA5),
  313. ),
  314. child: Text(
  315. _countdown > 0
  316. ? l10n.smsCodeCountdown(_countdown)
  317. : l10n.getSmsCode,
  318. style: TextStyle(
  319. color: _countdown > 0
  320. ? const Color(0xFF6B7280)
  321. : Colors.white,
  322. ),
  323. ),
  324. ),
  325. ),
  326. ],
  327. ),
  328. const SizedBox(height: 24),
  329. // 登录按钮
  330. ElevatedButton(
  331. onPressed: _handleSmsLogin,
  332. style: ElevatedButton.styleFrom(
  333. padding: const EdgeInsets.symmetric(vertical: 16),
  334. ),
  335. child: Text(l10n.login),
  336. ),
  337. ],
  338. ),
  339. );
  340. }
  341. }