login.dart 10KB

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