login.dart 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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)'; //更新文本内容
  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: Container(
  102. decoration: const BoxDecoration(
  103. image: DecorationImage(
  104. image: AssetImage("images/icon_login.png"),
  105. fit: BoxFit.cover,
  106. ),
  107. ),
  108. child: Container(
  109. child: Column(
  110. mainAxisAlignment: MainAxisAlignment.end,
  111. crossAxisAlignment: CrossAxisAlignment.start,
  112. children: [
  113. Padding(
  114. padding: EdgeInsets.fromLTRB(15, 0, 0, 5),
  115. child: const Text(
  116. '您好!',
  117. style: TextStyle(
  118. fontWeight: FontWeight.bold,
  119. fontSize: 33,
  120. ),
  121. ),
  122. ),
  123. Padding(
  124. padding: EdgeInsets.fromLTRB(15, 0, 0, 0),
  125. child: const Text(
  126. '欢迎进入农户端应用!',
  127. style: TextStyle(
  128. fontWeight: FontWeight.bold,
  129. fontSize: 27,
  130. ),
  131. ),
  132. ),
  133. Cell(
  134. // margin: EdgeInsets.symmetric(horizontal: 13.w),
  135. margin: const EdgeInsets.fromLTRB(13, 43, 10, 0),
  136. header: Text(
  137. "+86",
  138. style: TextStyle(
  139. fontSize: 19.sp,
  140. ),
  141. ),
  142. child: TextField(
  143. maxLength: 11,
  144. keyboardType: TextInputType.number,
  145. style: TextStyle(
  146. fontSize: 17.sp,
  147. ),
  148. decoration: const InputDecoration(
  149. isCollapsed: true,
  150. contentPadding:
  151. EdgeInsets.symmetric(vertical: 8, horizontal: 16),
  152. labelText: "请输入手机号码",
  153. counterText: '', //去掉计数
  154. border: InputBorder.none,
  155. floatingLabelBehavior: FloatingLabelBehavior.never,
  156. ),
  157. onChanged: (e) {
  158. _handlePhone(e);
  159. },
  160. ),
  161. footer: SizedBox(
  162. child: ElevatedButton(
  163. onPressed: () => {
  164. setState(() {
  165. _buttonClickListen();
  166. })
  167. },
  168. child: Text(
  169. '$buttonText',
  170. style: TextStyle(
  171. fontSize: 15.sp,
  172. ),
  173. ),
  174. style: ButtonStyle(
  175. backgroundColor: isButtonEnable
  176. ? MaterialStateProperty.all(const Color(0xFFFF703B))
  177. : MaterialStateProperty.all(
  178. const Color(0xFFCBCBCB)),
  179. shape: MaterialStateProperty.all(
  180. const RoundedRectangleBorder(
  181. borderRadius:
  182. BorderRadius.all(Radius.circular(10)))),
  183. ),
  184. ),
  185. )),
  186. Cell(
  187. // margin: EdgeInsets.symmetric(horizontal: 13.w),
  188. margin: const EdgeInsets.fromLTRB(13, 16, 10, 0),
  189. header: Align(
  190. alignment: FractionalOffset(0.1, 0.5),
  191. child: Image.asset(
  192. 'images/phoneCode.png',
  193. width: 20,
  194. height: 20,
  195. ),
  196. ),
  197. child: TextField(
  198. keyboardType: TextInputType.number,
  199. style: TextStyle(
  200. fontSize: 17.sp,
  201. ),
  202. decoration: const InputDecoration(
  203. isCollapsed: true,
  204. contentPadding:
  205. EdgeInsets.symmetric(vertical: 8, horizontal: 16),
  206. labelText: "请输入验证码",
  207. border: InputBorder.none,
  208. floatingLabelBehavior: FloatingLabelBehavior.never,
  209. ),
  210. onChanged: (e) {
  211. _handleCode(e);
  212. },
  213. ),
  214. ),
  215. Container(
  216. height: 350.h,
  217. alignment: Alignment.bottomCenter,
  218. child: SizedBox(
  219. width: 315.w,
  220. height: 49.h,
  221. child: ElevatedButton(
  222. onPressed: () {
  223. _handelSubmit();
  224. },
  225. child: const Text(
  226. "登陆",
  227. style: TextStyle(
  228. fontSize: 18,
  229. color: Colors.white,
  230. fontWeight: FontWeight.bold),
  231. ),
  232. style: ButtonStyle(
  233. elevation: MaterialStateProperty.all(0),
  234. backgroundColor:
  235. MaterialStateProperty.all(const Color(0xFFFF703B)),
  236. shape: MaterialStateProperty.all(
  237. const RoundedRectangleBorder(
  238. borderRadius:
  239. BorderRadius.all(Radius.circular(24.4)))),
  240. ),
  241. ),
  242. ),
  243. ),
  244. Container(
  245. padding: EdgeInsets.fromLTRB(10.0, 10, 10.0, 10),
  246. child: Row(
  247. children: <Widget>[
  248. Radio<bool>(
  249. value: true,
  250. activeColor: Color(0xFFFF703B),
  251. groupValue: _newValue,
  252. onChanged: (value) {
  253. setState(() {
  254. _newValue = value!;
  255. });
  256. }),
  257. RichText(
  258. text: TextSpan(children: <InlineSpan>[
  259. const TextSpan(
  260. text: '请认真查看',
  261. style: TextStyle(color: Color(0xff2a2a2a))),
  262. TextSpan(
  263. text: '文本协议/隐私政策,',
  264. style: TextStyle(color: Color(0xffce3800)),
  265. recognizer:
  266. TapGestureRecognizer() //踩坑。。。recognizer 是手势交互 除了我现在些的是 点击交互,其他一般都是抽象类。
  267. ..onTap = () {
  268. Get.toNamed('/agreement');
  269. },
  270. ),
  271. const TextSpan(
  272. text: '确认之后选择此项',
  273. style: TextStyle(color: Color(0xff2a2a2a))),
  274. ]),
  275. ),
  276. ],
  277. )),
  278. ],
  279. ),
  280. ),
  281. ),
  282. );
  283. }
  284. }