login.dart 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import 'dart:async';
  2. import 'package:farmer_client/models/app.dart';
  3. import 'package:fluttertoast/fluttertoast.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. import 'package:get/get.dart';
  7. import 'package:get_storage/get_storage.dart';
  8. import '../../models/entities/person.dart';
  9. import '../../services/user.dart';
  10. import '../../widgets/Cell.dart';
  11. class MyRouteLogin extends StatefulWidget {
  12. // 从其他页面跳转过来的
  13. bool isBack = false;
  14. @protected
  15. @mustCallSuper
  16. void beforeShow() {
  17. assert(() {
  18. print("===========beforeShow===========");
  19. return true;
  20. }());
  21. if (Get.arguments != null) {
  22. isBack = Get.arguments['isBack'];
  23. }
  24. }
  25. @override
  26. State<MyRouteLogin> createState() => _RouteLogin();
  27. }
  28. class _RouteLogin extends State<MyRouteLogin> {
  29. var userInfo = AppController.t.user;
  30. bool isButtonEnable = true; //按钮状态 是否可点击
  31. String buttonText = '发送验证码'; //初始文本
  32. int count = 60; //初始倒计时时间
  33. var timer; //倒计时的计时器
  34. TextEditingController mController = TextEditingController();
  35. GetStorage box = GetStorage();
  36. //获取验证码
  37. void _initTimer() {
  38. timer = Timer.periodic(Duration(seconds: 1), (Timer timer) {
  39. count--;
  40. setState(() {
  41. if (count == 0) {
  42. timer.cancel(); //倒计时结束取消定时器
  43. isButtonEnable = true; //按钮可点击
  44. count = 60; //重置时间
  45. buttonText = '发送验证码'; //重置按钮文本
  46. } else {
  47. buttonText = '($count)s重发'; //更新文本内容
  48. }
  49. });
  50. });
  51. }
  52. @override
  53. void initState() {
  54. super.initState();
  55. //注册协议的手势
  56. var location = AppController.t.testInt.value;
  57. print('--------------');
  58. print(location);
  59. }
  60. @override
  61. void dispose() {
  62. timer?.cancel(); //销毁计时器
  63. timer = null;
  64. mController.dispose();
  65. super.dispose();
  66. ///销毁
  67. }
  68. // -------------逻辑分割-------------------------------------------
  69. var handlePhones = '';
  70. var handleCodes = '';
  71. var userContent = {'phones': '', 'code': ''};
  72. bool _newValue = false;
  73. void _handlePhone(e) => {
  74. setState(() => {handlePhones = e})
  75. };
  76. void _handleCode(e) => {
  77. setState(() => {handleCodes = e})
  78. };
  79. // 获取验证码
  80. void _buttonClickListen() {
  81. setState(() {
  82. if (isButtonEnable) {
  83. if (handlePhones != '') {
  84. isButtonEnable = false; //按钮状态标记
  85. _initTimer();
  86. getSMSCaptch(handlePhones);
  87. } else {
  88. Fluttertoast.showToast(msg: '请正确输入手机号!');
  89. }
  90. }
  91. });
  92. }
  93. //登录按钮
  94. void _handelSubmit() {
  95. if (handleCodes == '' || handlePhones == '') {
  96. Fluttertoast.showToast(msg: '请输入验证码或手机号!');
  97. } else {
  98. if (_newValue) {
  99. userLogin(handlePhones, handleCodes).then((value) {
  100. box.write("token", value['token']);
  101. box.write("tokenTimeout", value['tokenTimeout']);
  102. userInfo(Person.fromJson(value['person']));
  103. if (widget.isBack) {
  104. Get.back();
  105. } else {
  106. Get.offNamed("/home");
  107. }
  108. });
  109. } else {
  110. Fluttertoast.showToast(msg: '请阅读并同意相关隐私政策!');
  111. }
  112. }
  113. }
  114. @override
  115. Widget build(BuildContext context) {
  116. return Scaffold(
  117. backgroundColor: Colors.transparent,
  118. resizeToAvoidBottomInset: false,
  119. body: GestureDetector(
  120. behavior: HitTestBehavior.translucent,
  121. onTap: () {
  122. FocusScope.of(context).requestFocus(FocusNode());
  123. },
  124. child: Container(
  125. decoration: const BoxDecoration(
  126. image: DecorationImage(
  127. image: AssetImage("images/icon_login.png"),
  128. fit: BoxFit.cover,
  129. ),
  130. ),
  131. child: ListView(
  132. children: [
  133. SizedBox(
  134. height: 44.w,
  135. child: Center(
  136. child: Text(
  137. '登录',
  138. style: TextStyle(
  139. letterSpacing: 3,
  140. fontWeight: FontWeight.bold,
  141. color: const Color(0xFF333333),
  142. fontSize: 17.sp,
  143. ),
  144. ),
  145. ),
  146. ),
  147. Padding(
  148. padding: EdgeInsets.fromLTRB(15, 0, 0, 5),
  149. child: const Text(
  150. '您好!',
  151. style: TextStyle(
  152. fontWeight: FontWeight.bold,
  153. fontSize: 33,
  154. ),
  155. ),
  156. ),
  157. Padding(
  158. padding: EdgeInsets.fromLTRB(15, 0, 0, 0),
  159. child: const Text(
  160. '欢迎进入农户端应用!',
  161. style: TextStyle(
  162. fontWeight: FontWeight.bold,
  163. fontSize: 27,
  164. ),
  165. ),
  166. ),
  167. Cell(
  168. // margin: EdgeInsets.symmetric(horizontal: 13.w),
  169. margin: const EdgeInsets.fromLTRB(13, 43, 10, 0),
  170. header: Text(
  171. "+86",
  172. style: TextStyle(
  173. fontSize: 19.sp,
  174. ),
  175. ),
  176. child: TextField(
  177. maxLength: 11,
  178. keyboardType: TextInputType.number,
  179. style: TextStyle(
  180. fontSize: 17.sp,
  181. ),
  182. decoration: const InputDecoration(
  183. isCollapsed: true,
  184. contentPadding:
  185. EdgeInsets.symmetric(vertical: 8, horizontal: 16),
  186. labelText: "请输入手机号码",
  187. counterText: '', //去掉计数
  188. border: InputBorder.none,
  189. floatingLabelBehavior: FloatingLabelBehavior.never,
  190. ),
  191. onChanged: (e) {
  192. _handlePhone(e);
  193. },
  194. ),
  195. footer: SizedBox(
  196. child: ElevatedButton(
  197. onPressed: () => {
  198. setState(() {
  199. _buttonClickListen();
  200. })
  201. },
  202. child: Text(
  203. '$buttonText',
  204. style: TextStyle(
  205. fontSize: 15.sp,
  206. ),
  207. ),
  208. style: ButtonStyle(
  209. backgroundColor: isButtonEnable
  210. ? MaterialStateProperty.all(const Color(0xFFFF703B))
  211. : MaterialStateProperty.all(
  212. const Color(0xFFCBCBCB)),
  213. shape: MaterialStateProperty.all(
  214. const RoundedRectangleBorder(
  215. borderRadius:
  216. BorderRadius.all(Radius.circular(10)))),
  217. ),
  218. ),
  219. )),
  220. Cell(
  221. // margin: EdgeInsets.symmetric(horizontal: 13.w),
  222. margin: const EdgeInsets.fromLTRB(13, 16, 10, 0),
  223. header: Align(
  224. alignment: FractionalOffset(0.1, 0.5),
  225. child: Image.asset(
  226. 'images/phoneCode.png',
  227. width: 20,
  228. height: 20,
  229. ),
  230. ),
  231. child: TextField(
  232. keyboardType: TextInputType.number,
  233. style: TextStyle(
  234. fontSize: 17.sp,
  235. ),
  236. decoration: const InputDecoration(
  237. isCollapsed: true,
  238. contentPadding:
  239. EdgeInsets.symmetric(vertical: 8, horizontal: 16),
  240. labelText: "请输入验证码",
  241. border: InputBorder.none,
  242. floatingLabelBehavior: FloatingLabelBehavior.never,
  243. ),
  244. onChanged: (e) {
  245. _handleCode(e);
  246. },
  247. ),
  248. ),
  249. Container(
  250. height: 350.h,
  251. alignment: Alignment.bottomCenter,
  252. child: SizedBox(
  253. width: 315.w,
  254. height: 49.h,
  255. child: ElevatedButton(
  256. onPressed: () {
  257. _handelSubmit();
  258. },
  259. child: const Text(
  260. "登录",
  261. style: TextStyle(
  262. fontSize: 18,
  263. color: Colors.white,
  264. fontWeight: FontWeight.bold),
  265. ),
  266. style: ButtonStyle(
  267. elevation: MaterialStateProperty.all(0),
  268. backgroundColor:
  269. MaterialStateProperty.all(const Color(0xFFFF703B)),
  270. shape: MaterialStateProperty.all(
  271. const RoundedRectangleBorder(
  272. borderRadius:
  273. BorderRadius.all(Radius.circular(24.4)))),
  274. ),
  275. ),
  276. ),
  277. ),
  278. Container(
  279. padding: EdgeInsets.fromLTRB(10.0, 10, 0, 10),
  280. child: Row(
  281. children: <Widget>[
  282. Radio<bool>(
  283. value: true,
  284. activeColor: Color(0xFFFF703B),
  285. groupValue: _newValue,
  286. onChanged: (value) {
  287. setState(() {
  288. _newValue = value!;
  289. });
  290. }),
  291. // SizedBox(
  292. // child:Text('文本协议/隐私政策',style: TextStyle(fontSize:15.sp,color: Color(0xffce3800))),
  293. // ),
  294. Text('请认真查看',
  295. style: TextStyle(
  296. fontSize: 14.sp, color: Color(0xff2a2a2a))),
  297. GestureDetector(
  298. onTap: () {
  299. Get.toNamed('/agreement');
  300. },
  301. child: Text('文本协议/隐私政策',
  302. style: TextStyle(
  303. fontSize: 14.sp, color: Color(0xffce3800))),
  304. ),
  305. Text(',确认之后选择此项',
  306. style: TextStyle(
  307. fontSize: 14.sp, color: Color(0xff2a2a2a))),
  308. // RichText(
  309. // text: TextSpan(children: <InlineSpan>[
  310. // const TextSpan(
  311. // text: '请认真查看',
  312. // style: TextStyle(color: Color(0xff2a2a2a))),
  313. // TextSpan(
  314. // text: '文本协议/隐私政策,',
  315. // style: TextStyle(color: Color(0xffce3800)),
  316. // recognizer:
  317. // TapGestureRecognizer() //踩坑。。。recognizer 是手势交互 除了我现在些的是 点击交互,其他一般都是抽象类。
  318. // ..onTap = () {
  319. // Get.toNamed('/agreement');
  320. // },
  321. // ),
  322. // const TextSpan(
  323. // text: '确认之后选择此项',
  324. // style: TextStyle(color: Color(0xff2a2a2a))),
  325. // ]),
  326. // ),
  327. ],
  328. )),
  329. ],
  330. ),
  331. ),
  332. ),
  333. );
  334. }
  335. }