login_page.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_tracker/dio/login_dao.dart';
  3. import 'package:flutter_tracker/model/config.dart';
  4. import 'package:flutter_tracker/model/user_model.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6. class LoginPage extends StatefulWidget {
  7. const LoginPage({Key key}) : super(key: key);
  8. @override
  9. _LoginPageState createState() => _LoginPageState();
  10. }
  11. class _LoginPageState extends State<LoginPage> {
  12. final TextEditingController _usernameController = TextEditingController();
  13. final TextEditingController _passwordController = TextEditingController();
  14. final _formKey = GlobalKey<FormState>();
  15. String _username;
  16. String _password;
  17. @override
  18. void initState() {
  19. super.initState();
  20. loadData();
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. appBar: AppBar(title: Text('登录')),
  26. body: Padding(
  27. padding: EdgeInsets.symmetric(vertical: 16, horizontal: 24),
  28. child: Form(
  29. key: _formKey,
  30. child: Column(
  31. children: <Widget>[
  32. TextFormField(
  33. autofocus: true,
  34. controller: _usernameController,
  35. decoration: InputDecoration(
  36. labelText: '用户名',
  37. hintText: '请输入用户名',
  38. icon: Icon(Icons.person)),
  39. validator: (value) {
  40. if (value.isEmpty) {
  41. return '用户名不能为空';
  42. }
  43. return null;
  44. },
  45. onSaved: (value) {
  46. _username = value;
  47. },
  48. ),
  49. TextFormField(
  50. controller: _passwordController,
  51. decoration: InputDecoration(
  52. labelText: '密码',
  53. hintText: '请输入密码',
  54. icon: Icon(Icons.lock)),
  55. obscureText: true,
  56. validator: (value) {
  57. if (value.isEmpty) {
  58. return '密码不能为空';
  59. }
  60. return null;
  61. },
  62. onSaved: (value) {
  63. _password = value;
  64. },
  65. ),
  66. Padding(
  67. padding: EdgeInsets.only(top: 28),
  68. child: Row(
  69. children: [
  70. Expanded(
  71. child: RaisedButton(
  72. padding: EdgeInsets.all(15),
  73. color: Theme.of(context).primaryColor,
  74. textColor: Colors.white,
  75. child: Text('登录'),
  76. onPressed: () {
  77. login();
  78. },
  79. ),
  80. ),
  81. ],
  82. ),
  83. ),
  84. ],
  85. ),
  86. ),
  87. ));
  88. }
  89. void loadData() async {
  90. SharedPreferences prefs = await SharedPreferences.getInstance();
  91. String username = prefs.getString('username');
  92. String password = prefs.getString('password');
  93. if (username != null && password != null) {
  94. _usernameController.text = username;
  95. _passwordController.text = password;
  96. }
  97. }
  98. void login() async {
  99. if (_formKey.currentState.validate()) {
  100. _formKey.currentState.save();
  101. UserModel userModel = await LoginDao.login(_username, _password);
  102. if (userModel.username == "") {
  103. Config.pref.setString('username', _username);
  104. Config.pref.setString('password', _password);
  105. Config.pref.setBool('isLogin', true);
  106. Navigator.of(context).pushReplacementNamed('/home');
  107. } else {}
  108. }
  109. }
  110. }