login_view.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../../../component/sys_app_bar.dart';
  4. import '../controllers/login_controller.dart';
  5. class LoginView extends GetView<LoginController> {
  6. const LoginView({Key? key}) : super(key: key);
  7. @override
  8. Widget build(BuildContext context) {
  9. return Container(
  10. decoration: const BoxDecoration(
  11. image: DecorationImage(
  12. image: AssetImage("assets/images/login/login.png"),
  13. fit: BoxFit.fill,
  14. ),
  15. ),
  16. child: Scaffold(
  17. backgroundColor: Colors.transparent,
  18. appBar: const SysAppBar(title: Text("登录"),),
  19. body: Obx(() {
  20. if(controller.errorMsg.isNotEmpty){
  21. WidgetsBinding.instance.addPostFrameCallback((_) {
  22. ScaffoldMessenger.of(context).showSnackBar(
  23. SnackBar(content: Text(controller.errorMsg.value))
  24. );
  25. });
  26. }
  27. return LoginScreen(isLoading: controller.isLoading.value,
  28. username: controller.username(),
  29. password: controller.password(),
  30. onLogin: (username, password) {
  31. // 在这里处理登录逻辑,例如调用API
  32. controller.fetchLogin(username,password);
  33. },onRegin: (username, password) {
  34. controller.fetchRegLogin(username,password);
  35. },);
  36. })
  37. ),
  38. );
  39. }
  40. }
  41. class LoginScreen extends StatefulWidget {
  42. final Function(String username, String password) onLogin;
  43. final Function(String username, String password) onRegin;
  44. final bool isLoading;
  45. final String username;
  46. final String password;
  47. LoginScreen({required this.isLoading, required this.onLogin,required this.onRegin, required this.username, required this.password});
  48. @override
  49. _LoginScreenState createState() => _LoginScreenState();
  50. }
  51. class _LoginScreenState extends State<LoginScreen> {
  52. final _usernameController = TextEditingController();
  53. final _passwordController = TextEditingController();
  54. @override
  55. Widget build(BuildContext context) {
  56. return Padding(
  57. padding: const EdgeInsets.only(bottom: 0),
  58. child: Center(
  59. child: Padding(
  60. padding: const EdgeInsets.fromLTRB(55, 40, 55, 0),
  61. child: Column(
  62. mainAxisAlignment: MainAxisAlignment.center,
  63. children: [
  64. TextField(
  65. controller: _usernameController,
  66. decoration: const InputDecoration(
  67. labelText: '用户名',
  68. hintText: '请输入用户名',
  69. border: InputBorder.none,
  70. ),
  71. ),
  72. const SizedBox(height: 16),
  73. TextField(
  74. controller: _passwordController,
  75. decoration: const InputDecoration(
  76. labelText: '密码',
  77. hintText: '输入密码',
  78. border: InputBorder.none,
  79. ),
  80. obscureText: true,
  81. ),
  82. const SizedBox(height: 20),
  83. SizedBox(
  84. width: 200,
  85. height: 40,
  86. child: ElevatedButton(
  87. onPressed: () {
  88. if (!widget.isLoading) {
  89. final username = _usernameController.text;
  90. final password = _passwordController.text;
  91. widget.onLogin(username, password);
  92. }
  93. },
  94. child: widget.isLoading ? const CircularProgressIndicator(
  95. color: Colors.white,
  96. ) : const Text('登录'),
  97. ),
  98. ),
  99. const SizedBox(height: 20),
  100. SizedBox(
  101. width: 200,
  102. height: 40,
  103. child: ElevatedButton(
  104. onPressed: () {
  105. if (!widget.isLoading) {
  106. final username = _usernameController.text;
  107. final password = _passwordController.text;
  108. widget.onRegin(username, password);
  109. }
  110. },
  111. child: widget.isLoading ? const CircularProgressIndicator(
  112. color: Colors.white,
  113. ) : const Text('注册'),
  114. ),
  115. ),
  116. ],
  117. ),
  118. ),
  119. ),
  120. );
  121. }
  122. @override
  123. void dispose() {
  124. _usernameController.dispose();
  125. _passwordController.dispose();
  126. super.dispose();
  127. }
  128. }