login.dart 8.2KB

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