register_page.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_tracker/dio/login_dao.dart';
  3. import 'package:flutter_tracker/model/message_model.dart';
  4. import 'package:flutter_tracker/routes/routes.dart';
  5. import 'package:flutter_tracker/utils/app_util.dart';
  6. /// Description:
  7. /// Time : 2021年12月03日 Friday
  8. /// Author : liuyuqi.gov@msncn
  9. class RegisterPage extends StatefulWidget {
  10. const RegisterPage({Key key}) : super(key: key);
  11. @override
  12. _RegisterPageState createState() => _RegisterPageState();
  13. }
  14. class _RegisterPageState extends State<RegisterPage> {
  15. //用户名密码控制器
  16. final TextEditingController _controllerUsn = TextEditingController();
  17. final TextEditingController _controllerPwd = TextEditingController();
  18. final TextEditingController _controllerRepwd = TextEditingController();
  19. final GlobalKey _formKey = GlobalKey<FormState>();
  20. String _userName = "";
  21. String _pwd = "";
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. appBar: AppBar(
  26. title: Text("注册账号"),
  27. ),
  28. body: Padding(
  29. padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
  30. child: Form(
  31. autovalidateMode: AutovalidateMode.always, key: _formKey, //开启自动校验
  32. child: ListView(
  33. children: [
  34. TextFormField(
  35. autofocus: true,
  36. controller: _controllerUsn,
  37. decoration: const InputDecoration(
  38. labelText: "用户名",
  39. hintText: "请输入用户名",
  40. icon: Icon(Icons.person)),
  41. // 校验用户名
  42. validator: (v) {
  43. return v.trim().isNotEmpty ? null : "用户名不能为空";
  44. },
  45. onChanged: (inputStr) {
  46. _userName = inputStr;
  47. },
  48. ),
  49. TextFormField(
  50. controller: _controllerPwd,
  51. decoration: const InputDecoration(
  52. labelText: "密码",
  53. hintText: "请输入密码",
  54. icon: Icon(Icons.lock)),
  55. obscureText: true,
  56. //校验密码
  57. validator: (v) {
  58. return v.trim().length > 5 ? null : "密码不能少于6位";
  59. }),
  60. TextFormField(
  61. controller: _controllerRepwd,
  62. decoration: const InputDecoration(
  63. labelText: "确认密码",
  64. hintText: "确认登录密码",
  65. icon: Icon(Icons.lock)),
  66. obscureText: true,
  67. //校验密码
  68. validator: (v) {
  69. return v.trim().length > 5 ? null : "密码不能少于6位";
  70. },
  71. onChanged: (inputStr) {
  72. _pwd = inputStr;
  73. },
  74. ),
  75. // 登录按钮
  76. Padding(
  77. padding: const EdgeInsets.only(top: 28.0),
  78. child: Row(
  79. children: <Widget>[
  80. Expanded(
  81. child: RaisedButton(
  82. padding: EdgeInsets.all(15.0),
  83. child: Text("注册"),
  84. color: Theme.of(context).primaryColor,
  85. textColor: Colors.white,
  86. onPressed: () {
  87. register();
  88. },
  89. ),
  90. ),
  91. ],
  92. ),
  93. )
  94. ],
  95. ),
  96. ),
  97. ),
  98. );
  99. }
  100. //注册
  101. void register() async {
  102. MessageModel messageModel = await LoginDao.register(_userName, _pwd);
  103. if (messageModel != null) {
  104. if (messageModel.success) {
  105. Navigator.of(context).pushNamed(Routes.loginPage);
  106. }
  107. AppUtil.buildToast(messageModel.msg);
  108. } else {
  109. AppUtil.buildToast("注册失败,请检查网络");
  110. }
  111. }
  112. // // //保存用户信息
  113. // void saveUserInfo(UserModel userModel) async {
  114. // SharedPreferences prefs = await SharedPreferences.getInstance();
  115. // prefs.setString("token", userModel.token);
  116. // AppConfig.isUser = false;
  117. // AppConfig.token = userModel.token;
  118. // loadUserInfo(userModel.token);
  119. // }
  120. //
  121. // void loadUserInfo(String token) async {
  122. // UserEntity entity = await UserDao.fetch(token);
  123. // if (entity?.userInfoModel != null) {
  124. // DialogUtil.buildToast("登录成功~");
  125. // Navigator.pop(context);
  126. // // eventBus.fire(UserLoggedInEvent("sucuss"));
  127. // } else {
  128. // DialogUtil.buildToast(entity.msgModel.msg);
  129. // }
  130. // }
  131. }