这是CaiYouHui前端,一个关于flutter的安卓app,前端使用flutter实现

login_screen.dart 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import '../../providers/auth_provider.dart';
  4. import '../../../core/constants/route_constants.dart';
  5. import '../../widgets/common/app_button.dart';
  6. import '../../widgets/common/app_text_field.dart';
  7. import '../../widgets/common/loading_indicator.dart';
  8. class LoginScreen extends StatefulWidget {
  9. final VoidCallback? onSuccess;
  10. const LoginScreen({
  11. super.key,
  12. this.onSuccess,
  13. });
  14. @override
  15. State<LoginScreen> createState() => _LoginScreenState();
  16. }
  17. class _LoginScreenState extends State<LoginScreen> {
  18. final _formKey = GlobalKey<FormState>();
  19. final _emailController = TextEditingController(text: 'aaa');
  20. final _passwordController = TextEditingController(text: 'Heweidabangzi77!');
  21. @override
  22. void dispose() {
  23. _emailController.dispose();
  24. _passwordController.dispose();
  25. super.dispose();
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. final authProvider = Provider.of<AuthProvider>(context);
  30. return Scaffold(
  31. appBar: AppBar(
  32. title: const Text('登录'),
  33. ),
  34. body: SingleChildScrollView(
  35. child: Padding(
  36. padding: const EdgeInsets.all(24.0),
  37. child: Form(
  38. key: _formKey,
  39. child: Column(
  40. crossAxisAlignment: CrossAxisAlignment.start,
  41. children: [
  42. const SizedBox(height: 40),
  43. const Center(
  44. child: Text(
  45. '欢迎回来',
  46. style: TextStyle(
  47. fontSize: 28,
  48. fontWeight: FontWeight.bold,
  49. ),
  50. ),
  51. ),
  52. const SizedBox(height: 8),
  53. const Center(
  54. child: Text(
  55. '请输入您的账号信息',
  56. style: TextStyle(
  57. fontSize: 16,
  58. color: Colors.grey,
  59. ),
  60. ),
  61. ),
  62. const SizedBox(height: 40),
  63. AppTextField(
  64. controller: _emailController, // 改为username
  65. labelText: '用户名',
  66. hintText: '请输入用户名',
  67. prefixIcon: const Icon(Icons.person_outline),
  68. validator: (value) {
  69. if (value == null || value.isEmpty) {
  70. return '请输入用户名';
  71. }
  72. return null;
  73. },
  74. ),
  75. const SizedBox(height: 20),
  76. AppTextField(
  77. controller: _passwordController,
  78. labelText: '密码',
  79. hintText: '请输入密码',
  80. obscureText: true,
  81. prefixIcon: const Icon(Icons.lock_outline),
  82. validator: (value) {
  83. if (value == null || value.isEmpty) {
  84. return '请输入密码';
  85. }
  86. if (value.length < 6) {
  87. return '密码至少需要6位字符';
  88. }
  89. return null;
  90. },
  91. ),
  92. const SizedBox(height: 30),
  93. if (authProvider.error != null)
  94. Padding(
  95. padding: const EdgeInsets.only(bottom: 16),
  96. child: Text(
  97. authProvider.error!,
  98. style: const TextStyle(
  99. color: Colors.red,
  100. fontSize: 14,
  101. ),
  102. ),
  103. ),
  104. if (authProvider.isLoading)
  105. const LoadingIndicator()
  106. else
  107. AppButton(
  108. text: '登录',
  109. onPressed: () async {
  110. if (_formKey.currentState!.validate()) {
  111. await authProvider.login(
  112. _emailController.text.trim(),
  113. _passwordController.text,
  114. );
  115. if (authProvider.isAuthenticated) {
  116. // widget.onSuccess?.call();
  117. Navigator.of(context).pushReplacementNamed(RouteConstants.home);
  118. // Navigator.of(context).pushNamedAndRemoveUntil(RouteConstants.home);
  119. }
  120. }
  121. },
  122. ),
  123. const SizedBox(height: 20),
  124. Center(
  125. child: TextButton(
  126. onPressed: () {
  127. Navigator.of(context).pushNamed(RouteConstants.register);
  128. },
  129. child: const Text(
  130. '还没有账号?立即注册',
  131. style: TextStyle(
  132. fontSize: 14,
  133. color: Colors.blue,
  134. ),
  135. ),
  136. ),
  137. ),
  138. const SizedBox(height: 30),
  139. const Row(
  140. children: [
  141. Expanded(child: Divider()),
  142. Padding(
  143. padding: EdgeInsets.symmetric(horizontal: 16),
  144. child: Text('或'),
  145. ),
  146. Expanded(child: Divider()),
  147. ],
  148. ),
  149. const SizedBox(height: 20),
  150. Row(
  151. mainAxisAlignment: MainAxisAlignment.center,
  152. children: [
  153. IconButton(
  154. onPressed: () {},
  155. icon: Container(
  156. padding: const EdgeInsets.all(10),
  157. decoration: BoxDecoration(
  158. color: Colors.grey[100],
  159. borderRadius: BorderRadius.circular(50),
  160. ),
  161. child: const Icon(Icons.wechat, color: Colors.green),
  162. ),
  163. ),
  164. IconButton(
  165. onPressed: () {},
  166. icon: Container(
  167. padding: const EdgeInsets.all(10),
  168. decoration: BoxDecoration(
  169. color: Colors.grey[100],
  170. borderRadius: BorderRadius.circular(50),
  171. ),
  172. child: const Icon(Icons.wechat, color: Colors.blue),
  173. ),
  174. ),
  175. ],
  176. ),
  177. ],
  178. ),
  179. ),
  180. ),
  181. ),
  182. );
  183. }
  184. }