login_page.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import 'package:flutter/material.dart';
  2. import 'package:gobang/service/user_dao.dart';
  3. /// Description: login page
  4. /// Time : 02/20/2024 Tuesday
  5. /// Author : liuyuqi.gov@msn.cn
  6. class LoginPage extends StatefulWidget {
  7. const LoginPage({Key? key}) : super(key: key);
  8. @override
  9. State<LoginPage> createState() => _LoginPageState();
  10. }
  11. class _LoginPageState extends State<LoginPage> {
  12. TextEditingController userNameController = TextEditingController();
  13. TextEditingController passwordController = TextEditingController();
  14. @override
  15. Widget build(BuildContext context) {
  16. return Scaffold(
  17. body: Column(children: [
  18. TextFormField(
  19. controller: userNameController,
  20. decoration: const InputDecoration(
  21. hintText: "请输入用户名",
  22. ),
  23. validator: (v) {
  24. if (v == null) {
  25. return "用户名不能为空";
  26. } else {
  27. return v.trim().isNotEmpty ? null : "用户名不能为空";
  28. }
  29. },
  30. ),
  31. TextFormField(
  32. controller: passwordController,
  33. decoration: const InputDecoration(
  34. hintText: "请输入密码",
  35. ),
  36. obscureText: true,
  37. validator: (v) {
  38. if (v == null) {
  39. return "密码不能为空";
  40. } else {
  41. return v.trim().length > 6 ? null : "密码不能少于6位";
  42. }
  43. },
  44. ),
  45. ElevatedButton(
  46. onPressed: () {
  47. Navigator.of(context).pushNamedAndRemoveUntil(
  48. "/",
  49. (Route<dynamic> route) => false,
  50. );
  51. },
  52. child: const Text("登录"),
  53. ),
  54. ]),
  55. );
  56. }
  57. /// login
  58. void login() async {
  59. if (Form.of(context).validate()) {
  60. await UserDao.login(userNameController.text, passwordController.text);
  61. } else {
  62. ScaffoldMessenger.of(context).showSnackBar(
  63. const SnackBar(
  64. content: Text("用户名或密码错误"),
  65. ),
  66. );
  67. }
  68. }
  69. }