login.dart 9.9KB

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