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