login_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import 'dart:convert';
  2. import 'package:dio/dio.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
  6. import 'package:lszlgl/base/base_lifecycle_state.dart';
  7. import 'package:lszlgl/config/app_config.dart';
  8. import 'package:lszlgl/main.dart';
  9. import 'package:lszlgl/model/req/login_req.dart';
  10. import 'package:lszlgl/network/my_api.dart';
  11. import 'package:lszlgl/service/dict_service.dart';
  12. import 'package:lszlgl/service/upgrade_service.dart';
  13. import 'package:lszlgl/service/user_service.dart';
  14. import 'package:lszlgl/widget/button.dart';
  15. import '../../config/reresh_config.dart';
  16. import '../../network/base_dio.dart';
  17. import '../../utils/location_utils.dart';
  18. import '../../utils/sp_utils.dart';
  19. import 'package:lszlgl/service/print_service.dart';
  20. /// 登录页面
  21. class LoginPage extends StatefulWidget {
  22. const LoginPage({Key? key}) : super(key: key);
  23. @override
  24. State<LoginPage> createState() => _LoginPageState();
  25. }
  26. class _LoginPageState extends BaseLifecycleState<LoginPage> {
  27. late TextEditingController accountCtrl;
  28. late TextEditingController pwdCtrl;
  29. late ValueNotifier<bool> _showPwd;
  30. late ValueNotifier<Map<String, dynamic>> _account;
  31. void onLogin() async {
  32. var account = accountCtrl.text;
  33. var pwd = pwdCtrl.text;
  34. if (account.isEmpty || pwd.isEmpty) {
  35. MyNavigator.showToast('请输入账号和密码');
  36. return;
  37. }
  38. MyNavigator.showLoading();
  39. try {
  40. // 登录
  41. var login = await MyApi.get().login(LoginReq(username: account, password: pwd));
  42. if (login.data == null) {
  43. MyNavigator.showToast('获取数据失败');
  44. MyNavigator.dismissLoading();
  45. return;
  46. }
  47. await UserService.get().saveLogin(login.data);
  48. // 存储 账号、密码
  49. var actMap = {
  50. 'phone': account,
  51. 'pwd': pwd,
  52. };
  53. SPUtils.getInstance().saveString('accountpwd', json.encode(actMap));
  54. getSystemData();
  55. } on DioException catch (_) {
  56. } catch (e) {
  57. logger.e(e);
  58. MyNavigator.showToast('获取数据失败');
  59. }
  60. MyNavigator.dismissLoading();
  61. }
  62. /// 获取基础数据
  63. void getSystemData() async {
  64. MyNavigator.showLoading(clickDismiss: false);
  65. try {
  66. // 获取用户数据
  67. var user = await MyApi.get().userProfile();
  68. await UserService.get().saveUser(user.data);
  69. // 获取字典
  70. var dictData = await MyApi.get().getAllDict();
  71. DictService.saveDictList(dictData.data);
  72. UpgradeService.checkUpgrade(false);
  73. // 进入主页
  74. MyNavigator.dismissLoading();
  75. startHome();
  76. } on DioException catch (_) {
  77. } on Exception catch (a, _) {
  78. logger.e('$a');
  79. MyNavigator.showToast('获取数据失败');
  80. }
  81. MyNavigator.dismissLoading();
  82. }
  83. /// 进入主页
  84. void startHome() {
  85. var login = UserService.get().getLogin()!;
  86. if (login.defaultPassword ?? false) {
  87. MyNavigator.showToast('密码强度过低,请修改密码');
  88. // 修改密码
  89. MyRouter.startChangePwd(startHome: true);
  90. } else {
  91. // 进入主页
  92. MyRouter.startMain(popAll: true);
  93. }
  94. }
  95. @override
  96. void onInit() {
  97. accountCtrl = TextEditingController();
  98. pwdCtrl = TextEditingController();
  99. _showPwd = ValueNotifier<bool>(true);
  100. _account = ValueNotifier<Map<String, dynamic>>({});
  101. }
  102. @override
  103. void onFirstShow(Duration timeStamp) async {
  104. // MyNavigator.showLoading();
  105. /// 初始化基础库 start
  106. BaseDio.get().init();
  107. await SPUtils.getInstance().init();
  108. RefreshConfig.get().initDefault();
  109. LocationUtils.updatePrivacyShow(true, true);
  110. LocationUtils.updatePrivacyAgree(true);
  111. LocationUtils.setApiKey(
  112. AppConfig.env == AppEnvironment.product
  113. ? '2c783509376e267b24d63b21681686fa'
  114. : '7d0c033909f84adc14a0e60a835f044f',
  115. '');
  116. /// 获取手机设备信息
  117. PrintService.getDeviceInfo();
  118. /// 同步数据库里的蓝牙设备信息到服务器
  119. database.savaBleDataToServer();
  120. /// 初始化基础库 end
  121. MyNavigator.dismissLoading();
  122. // 已登录
  123. if (UserService.get().getLogin() != null) {
  124. getSystemData();
  125. } else {
  126. // 未登录
  127. String? account = SPUtils.getInstance().getString('accountpwd');
  128. if (account != null) {
  129. _account.value = json.decode(account);
  130. }
  131. }
  132. }
  133. @override
  134. void onDestroy() {
  135. accountCtrl.dispose();
  136. pwdCtrl.dispose();
  137. }
  138. @override
  139. Widget build(BuildContext context) {
  140. return Scaffold(
  141. backgroundColor: const Color(0xFF49AAF2),
  142. body: KeyboardDismissOnTap(
  143. child: SingleChildScrollView(
  144. physics: const BouncingScrollPhysics(),
  145. child: Stack(
  146. children: [
  147. Positioned.fill(child: Image.asset(imgLoginBg, fit: BoxFit.fill)),
  148. SizedBox(
  149. height: MediaQuery.of(context).size.height,
  150. child: buildBody(),
  151. ),
  152. ],
  153. ),
  154. ),
  155. ),
  156. );
  157. }
  158. Widget buildBody() {
  159. return Column(
  160. children: [
  161. const Spacer(flex: 3),
  162. Container(
  163. width: double.infinity,
  164. padding: const EdgeInsets.symmetric(horizontal: 48),
  165. child: Image.asset(imgLoginTitle, fit: BoxFit.fill),
  166. ),
  167. const Spacer(),
  168. loginContainer(),
  169. const Spacer(flex: 7),
  170. ],
  171. );
  172. }
  173. Widget loginContainer() {
  174. return SizedBox(
  175. width: double.infinity,
  176. child: Stack(
  177. alignment: Alignment.topCenter,
  178. children: [
  179. buildInputContent(),
  180. userTitle(),
  181. ],
  182. ),
  183. );
  184. }
  185. Widget userTitle() {
  186. return Row(
  187. children: [
  188. const Spacer(),
  189. Expanded(
  190. child: Container(
  191. padding: const EdgeInsets.symmetric(vertical: 12),
  192. alignment: Alignment.center,
  193. decoration: const BoxDecoration(
  194. color: Colors.white,
  195. borderRadius: BorderRadius.all(Radius.circular(100)),
  196. border: Border.fromBorderSide(BorderSide(color: Color(0xFF49AAF2), width: 1)),
  197. boxShadow: [BoxShadow(color: Color(0x941C90FF), offset: Offset(0, 2))],
  198. ),
  199. child: const Text(
  200. '用户登录',
  201. style: TextStyle(color: Color(0xFF1187DE), fontSize: 14),
  202. ),
  203. ),
  204. ),
  205. const Spacer(),
  206. ],
  207. );
  208. }
  209. Widget buildInputContent() {
  210. return Container(
  211. margin: const EdgeInsets.only(top: 18, left: 16, right: 16),
  212. padding: const EdgeInsets.all(6),
  213. decoration: BoxDecoration(
  214. color: Colors.white.withOpacity(0.2),
  215. borderRadius: const BorderRadius.all(Radius.circular(36)),
  216. ),
  217. child: Container(
  218. padding: const EdgeInsets.symmetric(horizontal: 24),
  219. alignment: Alignment.center,
  220. decoration: const BoxDecoration(
  221. color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(32))),
  222. child: Column(
  223. children: [
  224. const SizedBox(height: 56),
  225. buildEdit(
  226. ctrl: accountCtrl,
  227. hint: '请输入登录账号',
  228. icon: imgLoginAccount,
  229. action: TextInputAction.next,
  230. rightIcon: ValueListenableBuilder(
  231. valueListenable: _account,
  232. builder: (ctx, Map<String,dynamic> ac, child){
  233. if(ac.isNotEmpty){
  234. return PopupMenuButton(
  235. icon: const Icon(
  236. Icons.arrow_drop_down_circle_outlined,
  237. size: 18,
  238. color: Color(0xFFBBBBBB),
  239. ),
  240. itemBuilder: (context) => [
  241. PopupMenuItem<String>(
  242. child: Text("账号:${ac['phone']}"),
  243. onTap: (){
  244. accountCtrl.text = ac['phone'];
  245. pwdCtrl.text =ac['pwd'];
  246. },
  247. ),
  248. ]
  249. );
  250. }else{
  251. return const SizedBox.shrink();
  252. }
  253. }
  254. )
  255. ),
  256. const SizedBox(height: 32),
  257. ValueListenableBuilder(
  258. valueListenable: _showPwd,
  259. builder: (BuildContext ctx, bool show, Widget? child) {
  260. return buildEdit(
  261. ctrl: pwdCtrl,
  262. hint: '请输入登录密码',
  263. icon: imgLoginPwd,
  264. obscure: show,
  265. onSubmit: (value) => onLogin(),
  266. rightIcon: child,
  267. );
  268. },
  269. child: IconButton(
  270. icon:
  271. const Icon(Icons.remove_red_eye_outlined, size: 18, color: Color(0xFFBBBBBB)),
  272. onPressed: () {
  273. _showPwd.value = !_showPwd.value;
  274. },
  275. )),
  276. const SizedBox(height: 44),
  277. buildLoginBtn(),
  278. SizedBox(
  279. width: double.infinity,
  280. child: TextButton(
  281. onPressed: () {
  282. MyRouter.forgetPwd();
  283. },
  284. style: const ButtonStyle(alignment: Alignment.centerRight),
  285. child:
  286. const Text('忘记密码', style: TextStyle(color: Color(0xFF25A6EE), fontSize: 14))),
  287. ),
  288. const SizedBox(height: 18),
  289. ],
  290. ),
  291. ),
  292. );
  293. }
  294. Widget buildEdit({
  295. required TextEditingController ctrl,
  296. required String hint,
  297. String? icon,
  298. TextInputAction action = TextInputAction.done,
  299. bool obscure = false,
  300. ValueChanged? onSubmit,
  301. Widget? rightIcon,
  302. }) {
  303. return TextField(
  304. controller: ctrl,
  305. decoration: InputDecoration(
  306. prefixIcon: icon == null
  307. ? null
  308. : Padding(
  309. padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
  310. child: Image.asset(icon, height: 18),
  311. ),
  312. prefixIconConstraints: const BoxConstraints(),
  313. border: const OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(100))),
  314. enabledBorder: const OutlineInputBorder(
  315. borderSide: BorderSide(color: Color(0xFFE6E6E6), width: 1),
  316. borderRadius: BorderRadius.all(Radius.circular(100)),
  317. ),
  318. hintText: hint,
  319. hintStyle: const TextStyle(color: Color(0xFFBBBBBB)),
  320. isDense: true,
  321. contentPadding: EdgeInsets.zero,
  322. suffixIcon: rightIcon,
  323. ),
  324. style: const TextStyle(fontSize: 14),
  325. textInputAction: action,
  326. obscureText: obscure,
  327. onSubmitted: onSubmit,
  328. );
  329. }
  330. Widget buildLoginBtn() {
  331. return MyButton(
  332. '登录',
  333. onTap: onLogin,
  334. gradient: const LinearGradient(colors: [Color(0xFF60B5F4), Color(0xFF62A4D6)]),
  335. );
  336. }
  337. }