login.dart 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import 'dart:async';
  2. import 'dart:ffi';
  3. import 'package:fluttertoast/fluttertoast.dart';
  4. import 'package:flutter/gestures.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_screenutil/flutter_screenutil.dart';
  7. import '../../services/user.dart';
  8. import '../../widgets/Cell.dart';
  9. class MyRouteLogin extends StatefulWidget {
  10. @override
  11. State<MyRouteLogin> createState() => _RouteLogin();
  12. }
  13. class _RouteLogin extends State<MyRouteLogin> {
  14. @override
  15. void initState() {
  16. super.initState();
  17. //注册协议的手势
  18. }
  19. bool isButtonEnable = true; //按钮状态 是否可点击
  20. String buttonText = '发送验证码'; //初始文本
  21. int count = 60; //初始倒计时时间
  22. var timer; //倒计时的计时器
  23. TextEditingController mController = TextEditingController();
  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 dispose() {
  41. timer?.cancel(); //销毁计时器
  42. timer = null;
  43. mController.dispose();
  44. super.dispose();
  45. ///销毁
  46. }
  47. // -------------逻辑分割-------------------------------------------
  48. var handlePhones = '';
  49. var handleCodes = '';
  50. void _handlePhone(e) => {
  51. setState(() => {handlePhones = e})
  52. };
  53. void _handleCode(e) => {
  54. setState(() => {handleCodes = e})
  55. };
  56. var userContent = {'phones': '', 'code': ''};
  57. void phoneUser() => {
  58. setState(() {
  59. userContent['phones'] = handlePhones;
  60. userContent['code'] = handleCodes;
  61. }),
  62. print('我输入的信息:${userContent},手机号:${userContent['phones']}')
  63. };
  64. bool _checkValue = false;
  65. bool _newValue = false;
  66. void _buttonClickListen() {
  67. setState(() {
  68. if (isButtonEnable) {
  69. if (handlePhones != '') {
  70. isButtonEnable = false; //按钮状态标记
  71. _initTimer();
  72. getSMSCaptch(handlePhones);
  73. } else {
  74. Fluttertoast.showToast(msg: '请正确输入手机号!');
  75. }
  76. print('获取验证码按钮222222');
  77. }
  78. });
  79. }
  80. @override
  81. Widget build(BuildContext context) {
  82. return Scaffold(
  83. backgroundColor: Colors.transparent,
  84. resizeToAvoidBottomInset: false,
  85. body: Container(
  86. decoration: const BoxDecoration(
  87. image: DecorationImage(
  88. image: AssetImage("images/icon_login.png"),
  89. fit: BoxFit.cover,
  90. ),
  91. ),
  92. child: Container(
  93. child: Column(
  94. mainAxisAlignment: MainAxisAlignment.end,
  95. crossAxisAlignment: CrossAxisAlignment.start,
  96. children:[
  97. const Text(
  98. '您好!',
  99. style: TextStyle(
  100. fontWeight: FontWeight.bold,
  101. fontSize: 50,
  102. ),
  103. ),
  104. const Text(
  105. '欢迎进入农户端应用!',
  106. style: TextStyle(
  107. fontWeight: FontWeight.bold,
  108. fontSize: 35,
  109. ),
  110. ),
  111. Cell(
  112. // margin: EdgeInsets.symmetric(horizontal: 13.w),
  113. margin: const EdgeInsets.fromLTRB(13, 43, 10, 0),
  114. header: Text(
  115. "+86",
  116. style: TextStyle(
  117. fontSize: 19.sp,
  118. ),
  119. ),
  120. child: TextField(
  121. maxLength: 11,
  122. keyboardType: TextInputType.number,
  123. style: TextStyle(
  124. fontSize: 17.sp,
  125. ),
  126. decoration: const InputDecoration(
  127. isCollapsed: true,
  128. contentPadding:
  129. EdgeInsets.symmetric(vertical: 8, horizontal: 16),
  130. labelText: "请输入手机号码",
  131. counterText: '', //去掉计数
  132. border: InputBorder.none,
  133. floatingLabelBehavior: FloatingLabelBehavior.never,
  134. ),
  135. onChanged: (e) {
  136. _handlePhone(e);
  137. },
  138. ),
  139. footer: SizedBox(
  140. width: 300.w,
  141. child: ElevatedButton(
  142. onPressed: () => {
  143. setState(() {
  144. // _buttonClickListen();
  145. setState(() {
  146. if (isButtonEnable) {
  147. if (handlePhones != '') {
  148. isButtonEnable = false; //按钮状态标记
  149. _initTimer();
  150. getSMSCaptch(handlePhones);
  151. } else {
  152. Fluttertoast.showToast(msg: '请正确输入手机号!');
  153. }
  154. print('获取验证码按钮222222');
  155. }
  156. });
  157. })
  158. },
  159. child: Text(
  160. '$buttonText',
  161. style: TextStyle(
  162. fontSize: 15.sp,
  163. ),
  164. ),
  165. style: ButtonStyle(
  166. backgroundColor: isButtonEnable
  167. ? MaterialStateProperty.all(const Color(0xFFFF703B))
  168. : MaterialStateProperty.all(
  169. const Color(0xFFCBCBCB)),
  170. shape: MaterialStateProperty.all(
  171. const RoundedRectangleBorder(
  172. borderRadius:
  173. BorderRadius.all(Radius.circular(10)))),
  174. ),
  175. ),
  176. )),
  177. // --------------FractionalOffset API--------------
  178. Cell(
  179. // margin: EdgeInsets.symmetric(horizontal: 13.w),
  180. margin: const EdgeInsets.fromLTRB(13, 16, 10, 0),
  181. header: Align(
  182. alignment: FractionalOffset(0.1, 0.5),
  183. child: Image.asset(
  184. 'images/phoneCode.png',
  185. width: 20,
  186. height: 20,
  187. ),
  188. ),
  189. child: TextField(
  190. keyboardType: TextInputType.number,
  191. style: TextStyle(
  192. fontSize: 17.sp,
  193. ),
  194. decoration: const InputDecoration(
  195. isCollapsed: true,
  196. contentPadding:
  197. EdgeInsets.symmetric(vertical: 8, horizontal: 16),
  198. labelText: "请输入验证码",
  199. border: InputBorder.none,
  200. floatingLabelBehavior: FloatingLabelBehavior.never,
  201. ),
  202. onChanged: (e) {
  203. _handleCode(e);
  204. },
  205. ),
  206. ),
  207. Container(
  208. height: 350.h,
  209. alignment: Alignment.bottomCenter,
  210. child: SizedBox(
  211. width: 315.w,
  212. height: 49.h,
  213. child: ElevatedButton(
  214. onPressed: () {
  215. if(handleCodes==''||handlePhones==''){
  216. Fluttertoast.showToast(msg: '请输入验证码或手机号!');
  217. }else{
  218. if(_newValue){
  219. print('已同意协议');
  220. userLogin(handlePhones,handleCodes);
  221. }else{
  222. Fluttertoast.showToast(msg: '请阅读并同意相关隐私政策!');
  223. }
  224. }
  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. 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. print('阅读已同意!!!');
  270. },
  271. ),
  272. TextSpan(
  273. text: '确认之后选择此项',
  274. style: TextStyle(color: Color(0xff2a2a2a))),
  275. ]),
  276. ),
  277. ],
  278. )),
  279. ],
  280. ),
  281. ),
  282. ),
  283. );
  284. }
  285. }