login_view.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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("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. });
  34. })
  35. ),
  36. );
  37. }
  38. }
  39. class LoginScreen extends StatefulWidget {
  40. final Function(String username, String password) onLogin;
  41. final bool isLoading;
  42. final String username;
  43. final String password;
  44. LoginScreen({required this.isLoading, required this.onLogin, required this.username, required this.password});
  45. @override
  46. _LoginScreenState createState() => _LoginScreenState();
  47. }
  48. class _LoginScreenState extends State<LoginScreen> {
  49. final _usernameController = TextEditingController();
  50. final _passwordController = TextEditingController();
  51. @override
  52. Widget build(BuildContext context) {
  53. return Padding(
  54. padding: const EdgeInsets.only(bottom: 30),
  55. child: Center(
  56. child: Padding(
  57. padding: const EdgeInsets.fromLTRB(55, 0, 55, 0),
  58. child: Column(
  59. mainAxisAlignment: MainAxisAlignment.center,
  60. children: [
  61. TextField(
  62. controller: _usernameController,
  63. decoration: const InputDecoration(
  64. labelText: '用户名',
  65. hintText: '请输入用户名',
  66. border: InputBorder.none,
  67. ),
  68. ),
  69. const SizedBox(height: 16),
  70. TextField(
  71. controller: _passwordController,
  72. decoration: const InputDecoration(
  73. labelText: '密码',
  74. hintText: '输入密码',
  75. border: InputBorder.none,
  76. ),
  77. obscureText: true,
  78. ),
  79. const SizedBox(height: 20),
  80. SizedBox(
  81. width: 200,
  82. height: 40,
  83. child: ElevatedButton(
  84. onPressed: () {
  85. if (!widget.isLoading) {
  86. final username = _usernameController.text;
  87. final password = _passwordController.text;
  88. widget.onLogin(username, password);
  89. }
  90. },
  91. child: widget.isLoading ? const CircularProgressIndicator(
  92. color: Colors.white,
  93. ) : const Text('登录'),
  94. ),
  95. ),
  96. ],
  97. ),
  98. ),
  99. ),
  100. );
  101. }
  102. @override
  103. void dispose() {
  104. _usernameController.dispose();
  105. _passwordController.dispose();
  106. super.dispose();
  107. }
  108. }