login.dart 12KB

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