login_page.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_tracker/dio/login_dao.dart';
  3. import 'package:flutter_tracker/model/login_entity.dart';
  4. import 'package:flutter_tracker/routes/routes.dart';
  5. import 'package:flutter_tracker/utils/app_util.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. /// Description:
  8. /// Time : 2021年12月03日 Friday
  9. /// Author : liuyuqi.gov@msncn
  10. class LoginPage extends StatefulWidget {
  11. const LoginPage({Key? key}) : super(key: key);
  12. @override
  13. _LoginPageState createState() => _LoginPageState();
  14. }
  15. class _LoginPageState extends State<LoginPage> {
  16. final TextEditingController _usernameController = TextEditingController();
  17. final TextEditingController _passwordController = TextEditingController();
  18. final _formKey = GlobalKey<FormState>();
  19. String? _username = "";
  20. String? _password = "";
  21. @override
  22. void initState() {
  23. super.initState();
  24. loadData();
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. return Scaffold(
  29. appBar: AppBar(title: Text('登录')),
  30. body: Padding(
  31. padding: EdgeInsets.symmetric(vertical: 16, horizontal: 24),
  32. child: Form(
  33. key: _formKey,
  34. child: Column(
  35. children: <Widget>[
  36. TextFormField(
  37. autofocus: true,
  38. controller: _usernameController,
  39. decoration: InputDecoration(
  40. labelText: '用户名',
  41. hintText: '请输入用户名',
  42. icon: Icon(Icons.person)),
  43. validator: (value) {
  44. if (value == null || value.isEmpty) {
  45. return '用户名不能为空';
  46. }
  47. return null;
  48. },
  49. onSaved: (value) {
  50. _username = value;
  51. },
  52. ),
  53. TextFormField(
  54. controller: _passwordController,
  55. decoration: const InputDecoration(
  56. labelText: '密码',
  57. hintText: '请输入密码',
  58. icon: Icon(Icons.lock)),
  59. obscureText: true,
  60. validator: (value) {
  61. if (value == null || value.isEmpty) {
  62. return '密码不能为空';
  63. }
  64. return null;
  65. },
  66. onSaved: (value) {
  67. _password = value;
  68. },
  69. ),
  70. Padding(
  71. padding: EdgeInsets.only(top: 28),
  72. child: Row(
  73. children: [
  74. Expanded(
  75. child: ElevatedButton(
  76. style: ButtonStyle(
  77. padding:
  78. MaterialStateProperty.all(EdgeInsets.all(15)),
  79. iconColor: MaterialStateProperty.all(Colors.green),
  80. foregroundColor:
  81. MaterialStateProperty.all(Colors.white),
  82. ),
  83. child: Text('登录'),
  84. onPressed: () {
  85. login();
  86. },
  87. ),
  88. ),
  89. ],
  90. ),
  91. ),
  92. ],
  93. ),
  94. ),
  95. ));
  96. }
  97. void loadData() async {
  98. SharedPreferences prefs = await SharedPreferences.getInstance();
  99. String? username = prefs.getString('username');
  100. String? password = prefs.getString('password');
  101. if (username != null && password != null) {
  102. _usernameController.text = username;
  103. _passwordController.text = password;
  104. }
  105. }
  106. void login() async {
  107. SharedPreferences prefs = await SharedPreferences.getInstance();
  108. if (_formKey.currentState!.validate() &&
  109. _username != null &&
  110. _password != null) {
  111. _formKey.currentState!.save();
  112. LoginEntity loginEntity = await LoginDao.login(_username!, _password!);
  113. if (loginEntity.msgModel.success) {
  114. prefs.setString('username', _username!);
  115. prefs.setString('password', _password!);
  116. prefs.setBool('isLogin', true);
  117. Navigator.of(context).pushNamed(Routes.indexPage);
  118. }
  119. AppUtil.buildToast(loginEntity.msgModel.msg);
  120. }
  121. }
  122. }