login.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'package:dio/dio.dart';
  2. import 'package:e2ee_chat/common/global.dart';
  3. import 'package:e2ee_chat/models/theme_model.dart';
  4. import 'package:e2ee_chat/models/user_model.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:provider/provider.dart';
  7. import 'package:fluttertoast/fluttertoast.dart';
  8. class LoginRoute extends StatefulWidget {
  9. @override
  10. _LoginRouteState createState() => _LoginRouteState();
  11. }
  12. class _LoginRouteState extends State<LoginRoute> {
  13. TextEditingController _unameController = new TextEditingController();
  14. TextEditingController _pwdController = new TextEditingController();
  15. bool pwdShow = false; //密码是否显示明文
  16. GlobalKey _formKey = new GlobalKey<FormState>();
  17. bool _nameAutoFocus = true;
  18. @override
  19. void initState() {
  20. // 自动填充上次登录的用户名,填充后将焦点定位到密码输入框
  21. if (Global.profile.username != null) {
  22. _unameController.text = Global.profile.username!;
  23. _nameAutoFocus = true;
  24. }
  25. super.initState();
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. var gm = GmLocalizations.of(context);
  30. return Scaffold(
  31. appBar: AppBar(title: Text(gm.login)),
  32. body: Padding(
  33. padding: const EdgeInsets.all(16.0),
  34. child: Form(
  35. key: _formKey,
  36. autovalidateMode: AutovalidateMode.always, // TODO: what is AutovalidateMode?
  37. child: Column(
  38. children: <Widget>[
  39. TextFormField(
  40. autofocus: _nameAutoFocus,
  41. controller: _unameController,
  42. decoration: InputDecoration(
  43. labelText: gm.userName,
  44. hintText: gm.userNameOrEmail,
  45. prefixIcon: Icon(Icons.person),
  46. ),
  47. // 校验用户名(不能为空)
  48. validator: (v) {
  49. return v!.trim().isNotEmpty ? null : gm.userNameRequired;
  50. }),
  51. TextFormField(
  52. controller: _pwdController,
  53. autofocus: !_nameAutoFocus,
  54. decoration: InputDecoration(
  55. labelText: gm.password,
  56. hintText: gm.password,
  57. prefixIcon: Icon(Icons.lock),
  58. suffixIcon: IconButton(
  59. icon: Icon(
  60. pwdShow ? Icons.visibility_off : Icons.visibility),
  61. onPressed: () {
  62. setState(() {
  63. pwdShow = !pwdShow;
  64. });
  65. },
  66. )),
  67. obscureText: !pwdShow,
  68. //校验密码(不能为空)
  69. validator: (v) {
  70. return v!.trim().isNotEmpty ? null : gm.passwordRequired;
  71. },
  72. ),
  73. Padding(
  74. padding: const EdgeInsets.only(top: 25),
  75. child: ConstrainedBox(
  76. constraints: BoxConstraints.expand(height: 55.0),
  77. child: ElevatedButton(
  78. style: ButtonStyle(
  79. foregroundColor: MaterialStateProperty.all<Color>(Provider.of<ThemeModel>(context).theme),
  80. ),
  81. onPressed: _onLogin,
  82. child: Text(gm.login),
  83. ),
  84. ),
  85. ),
  86. ],
  87. ),
  88. ),
  89. ),
  90. );
  91. }
  92. void _onLogin() async {
  93. // 提交前,先验证各个表单字段是否合法
  94. if ((_formKey.currentState as FormState).validate()) {
  95. Fluttertoast.showToast(msg: GmLocalizations.of(context).loading);
  96. try {
  97. // user = await Git(context).login(_unameController.text, _pwdController.text);
  98. // 因为登录页返回后,首页会build,所以我们传false,更新user后不触发更新
  99. Provider.of<UserModel>(context).login(username: _unameController.text, password: _pwdController.text);
  100. } on DioError catch(e) {
  101. //登录失败则提示
  102. if (e.response?.statusCode == 401) {
  103. Fluttertoast.showToast(msg: GmLocalizations.of(context).userNameOrPasswordWrong);
  104. } else {
  105. Fluttertoast.showToast(msg: e.toString());
  106. }
  107. }
  108. if (Global.profile.isLogin) {
  109. // 返回
  110. Navigator.of(context).pop();
  111. }
  112. }
  113. }
  114. }