login.dart 10KB

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