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