login_page.dart 11 KB

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