register_page.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: ElevatedButton(
  82. child: Text("注册"),
  83. style: ButtonStyle(
  84. padding: MaterialStateProperty.all(
  85. EdgeInsets.all(15.0),
  86. ),
  87. iconColor: MaterialStateProperty.all(
  88. Theme.of(context).primaryColor),
  89. foregroundColor:
  90. MaterialStateProperty.all(Colors.white),
  91. ),
  92. onPressed: () {
  93. register();
  94. },
  95. ),
  96. ),
  97. ],
  98. ),
  99. )
  100. ],
  101. ),
  102. ),
  103. ),
  104. );
  105. }
  106. //注册
  107. void register() async {
  108. MessageModel messageModel = await LoginDao.register(_userName, _pwd);
  109. if (messageModel != null) {
  110. if (messageModel.success) {
  111. Navigator.of(context).pushNamed(Routes.loginPage);
  112. }
  113. AppUtil.buildToast(messageModel.msg);
  114. } else {
  115. AppUtil.buildToast("注册失败,请检查网络");
  116. }
  117. }
  118. // // //保存用户信息
  119. // void saveUserInfo(UserModel userModel) async {
  120. // SharedPreferences prefs = await SharedPreferences.getInstance();
  121. // prefs.setString("token", userModel.token);
  122. // AppConfig.isUser = false;
  123. // AppConfig.token = userModel.token;
  124. // loadUserInfo(userModel.token);
  125. // }
  126. //
  127. // void loadUserInfo(String token) async {
  128. // UserEntity entity = await UserDao.fetch(token);
  129. // if (entity?.userInfoModel != null) {
  130. // DialogUtil.buildToast("登录成功~");
  131. // Navigator.pop(context);
  132. // // eventBus.fire(UserLoggedInEvent("sucuss"));
  133. // } else {
  134. // DialogUtil.buildToast(entity.msgModel.msg);
  135. // }
  136. // }
  137. }