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