login_view.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:naiyouwl/app/controller/controllers.dart';
  4. import 'package:url_launcher/url_launcher.dart';
  5. import '../../../component/sys_app_bar.dart';
  6. import '../controllers/login_controller.dart';
  7. class LoginView extends GetView<LoginController> {
  8. const LoginView({Key? key}) : super(key: key);
  9. @override
  10. Widget build(BuildContext context) {
  11. return Obx(() {
  12. if(controllers.global.errorMsg.value.isNotEmpty){
  13. String message = controllers.global.errorMsg.value;
  14. controllers.global.errorMsg.value = ''; // Clear the error message immediately
  15. Future.delayed(Duration.zero, () async {
  16. bool? result = await controllers.dialog.showNormalDialog(
  17. title: "提示",
  18. content: message,
  19. cancelText: "取消",
  20. enterText: "确认",
  21. );
  22. if (result != null && result) {
  23. // User pressed "确认"
  24. } else {
  25. // User pressed "取消" or dismissed the dialog
  26. }
  27. });
  28. }
  29. return Container(
  30. decoration: const BoxDecoration(
  31. image: DecorationImage(
  32. image: AssetImage("assets/images/login/login.png"),
  33. fit: BoxFit.fill,
  34. ),
  35. ),
  36. child: Scaffold(
  37. backgroundColor: Colors.transparent,
  38. appBar: SysAppBar(title: Text(controller.isReg.value ? "注册" : "登录"),),
  39. body: LoginScreen(isLoading: controller.isLoading.value,
  40. username: controller.username(),
  41. password: controller.password(),
  42. isReg: controller.isReg.value,
  43. url: controller.sysConfig.value.userReset ?? "",
  44. onLogin: (username, password) {
  45. // 在这里处理登录逻辑,例如调用API
  46. controller.fetchLogin(username, password);
  47. },
  48. onRegin: (username, password) {
  49. controller.fetchRegLogin(username, password);
  50. },
  51. onLoginAndLogin: () {
  52. controller.isReg.value = controller.isReg.value == false;
  53. },)
  54. ),
  55. );
  56. });
  57. }
  58. }
  59. class LoginScreen extends StatefulWidget {
  60. final Function(String username, String password) onLogin;
  61. final Function(String username, String password) onRegin;
  62. final void Function() onLoginAndLogin;
  63. final bool isLoading;
  64. final bool isReg;
  65. final String username;
  66. final String password;
  67. final String url;
  68. LoginScreen(
  69. {required this.isLoading, required this.onLogin, required this.onRegin, required this.onLoginAndLogin, required this.username, required this.password, required this.isReg, required this.url});
  70. @override
  71. _LoginScreenState createState() => _LoginScreenState();
  72. }
  73. class _LoginScreenState extends State<LoginScreen> {
  74. final _usernameController = TextEditingController();
  75. final _passwordController = TextEditingController();
  76. @override
  77. Widget build(BuildContext context) {
  78. return Padding(
  79. padding: const EdgeInsets.only(bottom: 0),
  80. child: Center(
  81. child: Padding(
  82. padding: const EdgeInsets.fromLTRB(55, 70, 55, 0),
  83. child: Column(
  84. mainAxisAlignment: MainAxisAlignment.center,
  85. children: [
  86. TextField(
  87. controller: _usernameController,
  88. decoration: const InputDecoration(
  89. labelText: '邮箱',
  90. hintText: '请输入邮箱',
  91. border: InputBorder.none,
  92. ),
  93. ),
  94. const SizedBox(height: 16),
  95. TextField(
  96. controller: _passwordController,
  97. decoration: const InputDecoration(
  98. labelText: '密码',
  99. hintText: '输入密码',
  100. border: InputBorder.none,
  101. ),
  102. obscureText: true,
  103. ),
  104. const SizedBox(height: 20),
  105. SizedBox(
  106. width: 200,
  107. height: 40,
  108. child: ElevatedButton(
  109. onPressed: () {
  110. if (!widget.isLoading) {
  111. if (widget.isReg) {
  112. final username = _usernameController.text;
  113. final password = _passwordController.text;
  114. widget.onRegin(username, password);
  115. }
  116. else {
  117. final username = _usernameController.text;
  118. final password = _passwordController.text;
  119. widget.onLogin(username, password);
  120. }
  121. }
  122. },
  123. child: widget.isLoading ? const CircularProgressIndicator(
  124. color: Colors.white,
  125. ) : Text(widget.isReg ? '注册新用户' : '登录'),
  126. ),
  127. ),
  128. const SizedBox(height: 20),
  129. SizedBox(
  130. width: 200,
  131. height: 40,
  132. child: ElevatedButton(
  133. onPressed: () {
  134. widget.onLoginAndLogin();
  135. },
  136. child: Text(widget.isReg ? "切换登录" : "注册新用户"),
  137. ),
  138. ),
  139. const SizedBox(height: 20),
  140. Align(
  141. alignment: Alignment.centerRight,
  142. child: TextButton(onPressed: () async {
  143. await launchUrl(Uri.parse(widget.url));
  144. }, child: const Text("忘记密码")),
  145. )
  146. ],
  147. ),
  148. ),
  149. ),
  150. );
  151. }
  152. @override
  153. void dispose() {
  154. _usernameController.dispose();
  155. _passwordController.dispose();
  156. super.dispose();
  157. }
  158. }