login_page.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import 'package:deus_app/common/local/StoreHelper.dart';
  2. import 'package:deus_app/common/style/gsy_style.dart';
  3. import 'package:deus_app/common/utils/CommonUtils.dart';
  4. import 'package:deus_app/common/utils/DioUtil.dart';
  5. import 'package:deus_app/common/utils/ToastUtils.dart';
  6. import 'package:deus_app/generated/json/user_response_entity_helper.dart';
  7. import 'package:deus_app/model/login_response_entity.dart';
  8. import 'package:deus_app/model/user_response_entity.dart';
  9. import 'package:deus_app/page/home/home_page.dart';
  10. import 'package:deus_app/widget/gsy_flex_button.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:flutter_easyloading/flutter_easyloading.dart';
  13. import '../../common/utils/ConstantString.dart';
  14. import '../../generated/json/login_response_entity_helper.dart';
  15. /// 登录页
  16. /// Created by Ocean
  17. class LoginPage extends StatefulWidget {
  18. static const String sName = "login";
  19. static var routeName = '/LoginPage';
  20. const LoginPage({super.key});
  21. @override
  22. State createState() {
  23. return _LoginPageState();
  24. }
  25. }
  26. class _LoginPageState extends State<LoginPage> {
  27. //TextEditingController可以使用 text 属性指定初始值
  28. final TextEditingController _usernameController = TextEditingController();
  29. final TextEditingController _passwordController = TextEditingController();
  30. String _username = '', _password = '';
  31. @override
  32. Widget build(BuildContext context) {
  33. StoreHelper.init();
  34. return Scaffold(
  35. resizeToAvoidBottomInset: false,
  36. body: Container(
  37. decoration: const BoxDecoration(
  38. image: DecorationImage(
  39. fit: BoxFit.cover,
  40. image: AssetImage("images/login_bg.png"),
  41. ),
  42. ),
  43. child: Column(
  44. crossAxisAlignment: CrossAxisAlignment.start,
  45. mainAxisAlignment: MainAxisAlignment.start,
  46. children: <Widget>[
  47. SizedBox(
  48. height: MediaQuery.of(context).size.height / 6,
  49. ),
  50. Container(
  51. margin: EdgeInsets.only(left: 30),
  52. child: const Text('鑫森科技',
  53. style: TextStyle(
  54. color: Colors.white,
  55. fontSize: GSYConstant.lagerTextSize,
  56. fontWeight: FontWeight.bold,
  57. )),
  58. ),
  59. Container(
  60. padding: EdgeInsets.only(left: 40,right: 40,top: 60),
  61. margin: EdgeInsets.only(top: 40),
  62. decoration: const BoxDecoration(
  63. image: DecorationImage(
  64. image: AssetImage("images/login_bg_write.png"),
  65. fit: BoxFit.fill,
  66. ),
  67. ),
  68. child:Column(
  69. crossAxisAlignment: CrossAxisAlignment.start,
  70. mainAxisAlignment: MainAxisAlignment.start,
  71. children: [
  72. Text('输入手机号',style: TextStyle(
  73. color: Color(0xFF999999),
  74. fontSize: GSYConstant.smallTextSize
  75. )),
  76. SizedBox(
  77. height: 10,
  78. ),
  79. _getUsernameInput(),
  80. SizedBox(
  81. height: 20,
  82. ),
  83. Text('输入密码',style: TextStyle(
  84. color: Color(0xFF999999),
  85. fontSize: GSYConstant.smallTextSize
  86. )),
  87. SizedBox(
  88. height: 10,
  89. ),
  90. _getPasswordInput(),
  91. SizedBox(
  92. height:50,
  93. ),
  94. _getLoginButton(),
  95. SizedBox(
  96. height: 45,
  97. ),
  98. ],
  99. ),
  100. ),
  101. ],
  102. ),
  103. ),
  104. );
  105. }
  106. Widget _getUsernameInput() {
  107. return _getInputTextField(
  108. TextInputType.number,
  109. controller: _usernameController,
  110. decoration: InputDecoration(
  111. border: OutlineInputBorder(
  112. ///设置边框四个角的弧度
  113. borderRadius: BorderRadius.all(Radius.circular(5)),
  114. ///用来配置边框的样式
  115. borderSide: BorderSide(
  116. ///设置边框的颜色
  117. color: Colors.red,
  118. ///设置边框的粗细
  119. width: 1.0,
  120. ),
  121. ),
  122. prefixIcon: Icon(
  123. Icons.mobile_friendly_rounded,
  124. size: 18.0,
  125. ),
  126. contentPadding: EdgeInsets.only(left: 15,right: 15,top: 0,bottom: 0),
  127. //使用 GestureDetector 实现手势识别
  128. suffixIcon: GestureDetector(
  129. child: Offstage(
  130. child: Icon(Icons.clear,size: 18.0),
  131. offstage: _username == '',
  132. ),
  133. //点击清除文本框内容
  134. onTap: () {
  135. this.setState(() {
  136. _username = '';
  137. _usernameController.clear();
  138. });
  139. },
  140. ),
  141. ),
  142. //使用 onChanged 完成双向绑定
  143. onChanged: (value) {
  144. this.setState(() {
  145. _username = value;
  146. });
  147. },
  148. );
  149. }
  150. Widget _getPasswordInput() {
  151. return _getInputTextField(
  152. TextInputType.text,
  153. obscureText: true,
  154. controller: _passwordController,
  155. decoration: InputDecoration(
  156. prefixIcon: Icon(
  157. Icons.lock_open,
  158. size: 18.0,
  159. ),
  160. suffixIcon: GestureDetector(
  161. child: Offstage(
  162. child: Icon(Icons.clear,size: 18.0),
  163. offstage: _password == '',
  164. ),
  165. onTap: () {
  166. this.setState(() {
  167. _password = '';
  168. _passwordController.clear();
  169. });
  170. },
  171. ),
  172. contentPadding: EdgeInsets.only(left: 15,right: 15,top: 0,bottom: 0),
  173. border: OutlineInputBorder(
  174. ///设置边框四个角的弧度
  175. borderRadius: BorderRadius.all(Radius.circular(5)),
  176. ///用来配置边框的样式
  177. borderSide: BorderSide(
  178. ///设置边框的颜色
  179. color: Colors.red,
  180. ///设置边框的粗细
  181. width: 1.0,
  182. ),
  183. ),
  184. ),
  185. onChanged: (value) {
  186. this.setState(() {
  187. _password = value;
  188. });
  189. },
  190. );
  191. }
  192. //省略了上述列举的代码
  193. Widget _getInputTextField(
  194. TextInputType keyboardType, {
  195. FocusNode? focusNode,
  196. controller: TextEditingController,
  197. onChanged: Function,
  198. InputDecoration? decoration,
  199. bool obscureText = false,
  200. height = 52.0,
  201. }) {
  202. return Container(
  203. height: height,
  204. child: Column(
  205. children: [
  206. TextField(
  207. keyboardType: keyboardType,
  208. focusNode: focusNode,
  209. obscureText: obscureText,
  210. controller: controller,
  211. decoration: decoration,
  212. onChanged: onChanged,
  213. style: const TextStyle(fontSize: GSYConstant.TextSize15),
  214. ),
  215. // Divider(
  216. // height: 1.0,
  217. // color: Colors.grey[400],
  218. // ),
  219. ],
  220. ),
  221. );
  222. }
  223. Widget _getLoginButton() {
  224. return SizedBox(
  225. height: 50,
  226. width: double.infinity,
  227. child: GSYFlexButton(
  228. text: ConstantString.loginText,
  229. color: Color(0xFF215CFF),
  230. textColor: GSYColors.textWhite,
  231. fontSize: 16,
  232. onPress: login,
  233. ),
  234. );
  235. }
  236. login() async {
  237. if (!CommonUtils.validationInput(_username)) {
  238. showToast(ConstantString.loginNull);
  239. } else {
  240. // LoadingDialogHelper.showLoading(context);
  241. // EasyLoading.show(status: 'loading...');
  242. var result = await DioUtil().request('auth/login',
  243. method: DioMethod.post,
  244. data: {'username': _username, 'password': _password});
  245. LoginResponseEntity loginResponseEntity =
  246. loginResponseEntityFromJson(LoginResponseEntity(), result);
  247. if (0 == loginResponseEntity.code) {
  248. StoreHelper.putStorage(
  249. ConstantString.token, loginResponseEntity.data.token);
  250. debugPrint(loginResponseEntity.data.token);
  251. getInfo();
  252. } else {
  253. EasyLoading.dismiss();
  254. showToast(loginResponseEntity.msg);
  255. }
  256. }
  257. }
  258. getInfo() async {
  259. var result =
  260. await DioUtil().request('user/current-user', method: DioMethod.post);
  261. UserResponseEntity entity =
  262. userResponseEntityFromJson(UserResponseEntity(), result);
  263. if (entity.code == 0) {
  264. StoreHelper.putStorage(ConstantString.name, entity.data.name);
  265. StoreHelper.putStorage(ConstantString.orgName, entity.data.orgName);
  266. StoreHelper.putStorage(ConstantString.phone, entity.data.phone);
  267. Navigator.pushAndRemoveUntil(
  268. context,
  269. MaterialPageRoute(builder: (context) => HomePage(userResponseEntity: entity.data.menu4AppList)),
  270. (route) => false);
  271. } else {
  272. showToast(entity.msg);
  273. }
  274. }
  275. }