login.dart 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import 'dart:async';
  2. import 'package:farmer_client/components/UI/DefaultButton.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import '../../widgets/Cell.dart';
  6. class MyRouteLogin extends StatefulWidget {
  7. @override
  8. State<MyRouteLogin> createState() => _RouteLogin();
  9. }
  10. class _RouteLogin extends State<MyRouteLogin> {
  11. bool isButtonEnable = true; //按钮状态 是否可点击
  12. String buttonText = '发送验证码'; //初始文本
  13. int count = 5; //初始倒计时时间
  14. var timer; //倒计时的计时器
  15. TextEditingController mController = TextEditingController();
  16. void _buttonClickListen() {
  17. print('获取验证码按钮');
  18. setState(() {
  19. if (isButtonEnable) {
  20. //当按钮可点击时
  21. isButtonEnable = false; //按钮状态标记
  22. _initTimer();
  23. return null; //返回null按钮禁止点击
  24. } else {
  25. //当按钮不可点击时
  26. // debugPrint('false');
  27. return null; //返回null按钮禁止点击
  28. }
  29. });
  30. }
  31. void _initTimer() {
  32. timer = new Timer.periodic(Duration(seconds: 1), (Timer timer) {
  33. count--;
  34. setState(() {
  35. if (count == 0) {
  36. timer.cancel(); //倒计时结束取消定时器
  37. isButtonEnable = true; //按钮可点击
  38. count = 5; //重置时间
  39. buttonText = '发送验证码'; //重置按钮文本
  40. } else {
  41. buttonText = '重新发送($count)'; //更新文本内容
  42. }
  43. });
  44. });
  45. }
  46. @override
  47. void dispose() {
  48. timer?.cancel(); //销毁计时器
  49. timer = null;
  50. mController.dispose();
  51. super.dispose();
  52. }
  53. // -------------逻辑分割-------------------------------------------
  54. var handlePhones = '';
  55. var handleCodes = '';
  56. void _handlePhone(e) => {
  57. setState(() => {handlePhones = e})
  58. };
  59. void _handleCode(e) => {
  60. setState(() => {handleCodes = e})
  61. };
  62. var userContent = {'phones': '', 'code': ''};
  63. void phoneUser() => {
  64. setState(() {
  65. userContent['phones'] = handlePhones;
  66. userContent['code'] = handleCodes;
  67. }),
  68. print('我输入的信息:${userContent},手机号:${userContent['phones']}')
  69. };
  70. @override
  71. Widget build(BuildContext context) {
  72. return Container(
  73. width: 12.w,
  74. decoration: const BoxDecoration(
  75. image: DecorationImage(
  76. image: AssetImage("images/icon_login.png"),
  77. fit: BoxFit.cover,
  78. ),
  79. ),
  80. child: Scaffold(
  81. backgroundColor: Colors.transparent, //把scaffold的背景色改成透明
  82. appBar: AppBar(
  83. backgroundColor: Colors.transparent, //把appbar的背景色改成透明
  84. // elevation: 0,//appbar的阴影
  85. title: const Text('登陆'),
  86. ),
  87. body: Center(
  88. child: Column(
  89. crossAxisAlignment: CrossAxisAlignment.start,
  90. children: <Widget>[
  91. Text(
  92. '您好!',
  93. style: TextStyle(
  94. fontWeight: FontWeight.bold,
  95. fontSize: 50,
  96. ),
  97. ),
  98. Text(
  99. '欢迎进入农户端应用!',
  100. style: TextStyle(
  101. fontWeight: FontWeight.bold,
  102. fontSize: 35,
  103. ),
  104. ),
  105. // Container(
  106. // // margin: EdgeInsets.fromLTRB(5,0,0,0),
  107. // decoration: BoxDecoration(
  108. // border: Border(
  109. // bottom: BorderSide(
  110. // width: 1, color: Color(0xffe5e5e5)))),
  111. // child: Flex(
  112. // direction: Axis.horizontal,
  113. // children: <Widget>[
  114. // Expanded(
  115. // child: TextField(
  116. // autofocus: true,
  117. // maxLength: 11,
  118. // keyboardType: TextInputType.number,
  119. // cursorColor: Color(0xD4D4D4FF),
  120. // decoration: const InputDecoration(
  121. // contentPadding:EdgeInsets.fromLTRB(30.0, 0, 0, 0),
  122. // hintText: "请输入手机号",
  123. // prefixIcon: Icon(Icons.phone_iphone_outlined),
  124. // counterText: '',
  125. // border: OutlineInputBorder(
  126. // borderSide: BorderSide.none),
  127. // ),
  128. // onChanged: (e) {
  129. // _handlePhone(e);
  130. // // phoneUser(e);
  131. // },
  132. // ),
  133. // ),
  134. // Container(
  135. // width: 100.0,
  136. // child: SizedBox(
  137. // child: FlatButton(
  138. // disabledColor:
  139. // Colors.grey.withOpacity(0.1), //按钮禁用时的颜色
  140. // disabledTextColor: Colors.white, //按钮禁用时的文本颜色
  141. // textColor: isButtonEnable
  142. // ? Colors.white
  143. // : Colors.black.withOpacity(0.2), //文本颜色
  144. // color: isButtonEnable
  145. // ? Color(0xffff703b)
  146. // : Colors.grey.withOpacity(0.1), //按钮的颜色
  147. // splashColor: isButtonEnable
  148. // ? Colors.white.withOpacity(0.1)
  149. // : Colors.transparent,
  150. // shape: StadiumBorder(side: BorderSide.none),
  151. // onPressed: () {
  152. // setState(() {
  153. // if (isButtonEnable) {
  154. // _buttonClickListen();
  155. // } else {
  156. // return;
  157. // }
  158. // });
  159. // },
  160. // child: Text(
  161. // '$buttonText',
  162. // style: TextStyle(
  163. // fontSize: 13,
  164. //
  165. // ),
  166. // ),
  167. // ),
  168. // ),
  169. // ),
  170. // ],
  171. // )),
  172. Cell(
  173. margin: EdgeInsets.symmetric(horizontal: 13.w),
  174. header: Text(
  175. "+86",
  176. style: TextStyle(fontSize: 19.sp),
  177. ),
  178. child: TextField(
  179. style: TextStyle(
  180. fontSize: 17.sp,
  181. ),
  182. decoration: const InputDecoration(
  183. isCollapsed: true,
  184. contentPadding:
  185. EdgeInsets.symmetric(vertical: 8, horizontal: 16),
  186. labelText: "请输入手机号码",
  187. border: InputBorder.none,
  188. // border: OutlineInputBorder(
  189. // // borderSide: BorderSide.none,
  190. // // gapPadding: 0,
  191. // ),
  192. floatingLabelBehavior: FloatingLabelBehavior.never,
  193. )),
  194. footer: ElevatedButton(
  195. onPressed: () => {
  196. setState(() {
  197. if (isButtonEnable) {
  198. _buttonClickListen();
  199. } else {
  200. return;
  201. }
  202. })
  203. },
  204. child: Text(
  205. '$buttonText',
  206. style: TextStyle(fontSize: 15.sp),
  207. ),
  208. style: ElevatedButton.styleFrom(
  209. primary: const Color(0xFFFF703B),
  210. elevation: 0,
  211. shape: const RoundedRectangleBorder(
  212. borderRadius:
  213. BorderRadius.all(Radius.circular(10)))),
  214. ),
  215. ),
  216. TextField(
  217. keyboardType: TextInputType.number,
  218. cursorColor: Color(0xD4D4D4FF),
  219. decoration: const InputDecoration(
  220. hintText: "请输入验证码", prefixIcon: Icon(Icons.beenhere)),
  221. onChanged: (e) {
  222. _handleCode(e);
  223. },
  224. ),
  225. // 登录按钮
  226. Container(
  227. child: DefaultButton(
  228. margin:
  229. EdgeInsets.fromLTRB(0, 30.0, 0, 0), //可选配置,自定义控件中有默认配置
  230. text: "登陆",
  231. width: 200.0,
  232. height: 50.0,
  233. fontSize: 20.0,
  234. backColor: Color(0xffff703b),
  235. color: Colors.white,
  236. onPressed: () {
  237. Navigator.push(
  238. context,
  239. MaterialPageRoute(builder: (context) {
  240. return MyRouteLogin();
  241. }),
  242. );
  243. },
  244. ),
  245. )
  246. ],
  247. ))));
  248. }
  249. }