login_page.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.isEmpty) {
  45. return '用户名不能为空';
  46. }
  47. return null;
  48. },
  49. onSaved: (value) {
  50. _username = value;
  51. },
  52. ),
  53. TextFormField(
  54. controller: _passwordController,
  55. decoration: InputDecoration(
  56. labelText: '密码',
  57. hintText: '请输入密码',
  58. icon: Icon(Icons.lock)),
  59. obscureText: true,
  60. validator: (value) {
  61. if (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: RaisedButton(
  76. padding: EdgeInsets.all(15),
  77. color: Colors.green,
  78. textColor: Colors.white,
  79. child: Text('登录'),
  80. onPressed: () {
  81. login();
  82. },
  83. ),
  84. ),
  85. ],
  86. ),
  87. ),
  88. ],
  89. ),
  90. ),
  91. ));
  92. }
  93. void loadData() async {
  94. SharedPreferences prefs = await SharedPreferences.getInstance();
  95. String username = prefs.getString('username');
  96. String password = prefs.getString('password');
  97. if (username != null && password != null) {
  98. _usernameController.text = username;
  99. _passwordController.text = password;
  100. }
  101. }
  102. void login() async {
  103. SharedPreferences prefs = await SharedPreferences.getInstance();
  104. if (_formKey.currentState.validate()) {
  105. _formKey.currentState.save();
  106. LoginEntity loginEntity = await LoginDao.login(_username, _password);
  107. if (loginEntity.msgModel.success) {
  108. prefs.setString('username', _username);
  109. prefs.setString('password', _password);
  110. prefs.setBool('isLogin', true);
  111. Navigator.of(context).pushNamed(Routes.indexPage);
  112. }
  113. AppUtil.buildToast(loginEntity.msgModel.msg);
  114. }
  115. }
  116. }