login.dart 12KB

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