login.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:bot_toast/bot_toast.dart';
  4. import 'package:twong/api/index.dart';
  5. import 'package:twong/config/protocols.dart';
  6. import 'package:twong/config/style.dart';
  7. import 'package:twong/router/base.dart';
  8. import 'package:twong/utils/index.dart';
  9. import 'package:twong/widgets/counter_botton.dart';
  10. class LoginPage extends StatefulWidget {
  11. @override
  12. State<StatefulWidget> createState() {
  13. return _LoginPageState();
  14. }
  15. }
  16. class _LoginPageState extends State<LoginPage> {
  17. var _enable = true;
  18. final _max = 60;
  19. var _counter;
  20. Timer _timer;
  21. var _codeTxt = "获取验证码";
  22. FocusNode _codeNode = FocusNode();
  23. FocusNode _accountNode = FocusNode();
  24. FocusNode _passwordNode = FocusNode();
  25. FocusNode _rePasswordNode = FocusNode();
  26. PageController _pageController = PageController();
  27. TextEditingController _codeCtl = TextEditingController();
  28. TextEditingController _accountCtl = TextEditingController();
  29. TextEditingController _passwordCtl = TextEditingController();
  30. TextEditingController _rePasswordCtl = TextEditingController();
  31. _onLogin(String value) {
  32. var account = _accountCtl.text;
  33. var password = _passwordCtl.text;
  34. if(account.trim() == "" || password.trim() == "") {
  35. return;
  36. }
  37. Network.inst.login(account: account, password: password).then((value) {
  38. Cache.updateAccount(account, password);
  39. Navigator.pop(context);
  40. BotToast.showText(text: "登陆成功!");
  41. }).catchError((err) {
  42. Log.e(err);
  43. BotToast.showText(text: err.toString());
  44. });
  45. }
  46. bool _onGetCode() {
  47. if(_accountCtl.text.trim() == "") {
  48. BotToast.showText(text: "手机号不能为空");
  49. return false;
  50. }
  51. return true;
  52. }
  53. _onSmsLogin(String value) {
  54. }
  55. _onRegister(String value) {
  56. }
  57. _onNextInput(String value) {
  58. FocusScope.of(context).requestFocus(_passwordNode);
  59. }
  60. wechatLogin () {
  61. Utils.notOpen();
  62. }
  63. Widget _buildInput(String hit, FocusNode node, TextEditingController controller,
  64. TextInputAction action, Function(String) onSubmit, IconData icon,
  65. {bool password = false, TextInputType type}) {
  66. return TextField(
  67. focusNode: node,
  68. obscureText: password,
  69. onSubmitted: onSubmit,
  70. controller: controller,
  71. keyboardType: type,
  72. style: TextStyle(color: Colors.white),
  73. textInputAction: action,
  74. cursorColor: Colors.white,
  75. decoration: InputDecoration(
  76. filled: true,
  77. hintText: hit,
  78. border: InputBorder.none,
  79. hintStyle: TextStyle(color: Colors.white70),
  80. enabledBorder: OutlineInputBorder(
  81. borderSide: BorderSide(color: Color(0x00FF00a0)),
  82. borderRadius: BorderRadius.all(
  83. Radius.circular(100),
  84. ),
  85. ),
  86. focusedBorder: OutlineInputBorder(
  87. borderSide: BorderSide(color: Color(0x000000a0)),
  88. borderRadius: BorderRadius.all(
  89. Radius.circular(100),
  90. ),
  91. ),
  92. contentPadding: EdgeInsets.all(10.px),
  93. prefixIcon: Icon(icon, color: Colors.white),
  94. ),
  95. );
  96. }
  97. @override
  98. void dispose() {
  99. super.dispose();
  100. if(_timer != null) {
  101. _timer.cancel();
  102. }
  103. }
  104. @override
  105. Widget build(BuildContext context) {
  106. return Scaffold(
  107. body: Container(
  108. decoration: BoxDecoration(
  109. image: DecorationImage(
  110. fit: BoxFit.cover,
  111. image: AssetImage('assets/images/launcher.png'),
  112. )
  113. ),
  114. child: SafeArea(
  115. child: Column(
  116. children: <Widget>[
  117. Container(
  118. alignment: Alignment.topRight,
  119. child: FlatButton(
  120. highlightColor: Colors.transparent,
  121. child: Text('跳过,看好货 >',
  122. style: TextStyle(color: Colors.white)
  123. ),
  124. onPressed: () {
  125. Navigator.pop(context);
  126. }
  127. ),
  128. ),
  129. Container(
  130. height: 100.px,
  131. margin: EdgeInsets.only(bottom: 50.px, top: 10.px),
  132. child: Center(
  133. child: Image.asset("assets/images/logo.png"),
  134. ),
  135. ),
  136. Container(
  137. height: 368.px,
  138. margin: EdgeInsets.only(left: 12.px, right: 12.px),
  139. child: PageView(
  140. controller: _pageController,
  141. physics: NeverScrollableScrollPhysics(),
  142. children: [
  143. _buildLogin(),
  144. _buildRegister(),
  145. _buildSMSLogin()
  146. ],
  147. ),
  148. ),
  149. Expanded(child: _buildOtherLogin()),
  150. _buildBottom()
  151. ],
  152. ),
  153. ),
  154. ),
  155. );
  156. }
  157. Widget _buildLogin() {
  158. return Column(
  159. children: [
  160. Container(
  161. margin: EdgeInsets.only(bottom: 12.px),
  162. child: _buildInput("请输入您的手机号", _accountNode, _accountCtl,
  163. TextInputAction.next,_onNextInput, Icons.account_circle,
  164. type: TextInputType.phone),
  165. ),
  166. _buildInput("请输入您的密码", _passwordNode, _passwordCtl,
  167. TextInputAction.done, _onLogin, Icons.lock, password: true,
  168. type: TextInputType.text),
  169. Container(
  170. height: 50.px,
  171. width: double.infinity,
  172. margin: EdgeInsets.only(left: 56.px, right: 56.px),
  173. padding: EdgeInsets.only(top: 12.px),
  174. child: FlatButton(
  175. color: Colors.white,
  176. shape: StadiumBorder(),
  177. onPressed: () { _onLogin(_accountCtl.text); },
  178. child: Text('登 陆', style: TextStyle(fontSize: 16.px)),
  179. ),
  180. ),
  181. Container(
  182. margin: EdgeInsets.only(bottom: 22.px),
  183. child: Row(
  184. children: [
  185. Container(
  186. child: FlatButton(
  187. highlightColor: Colors.transparent,
  188. child: Text('验证码登陆', style: TextStyle(color:
  189. Colors.white, decoration: TextDecoration.underline)),
  190. onPressed: () { _jumpTo(2); }),
  191. ),
  192. Spacer(),
  193. Container(
  194. child: FlatButton(
  195. highlightColor: Colors.transparent,
  196. child: Text('没有账号?去注册', style: TextStyle(color:
  197. Colors.white, decoration: TextDecoration.underline)),
  198. onPressed: () { _jumpTo(1); }),
  199. ),
  200. ],
  201. ),
  202. ),
  203. ],
  204. );
  205. }
  206. Widget _buildRegister() {
  207. return Column(
  208. children: [
  209. Container(
  210. margin: EdgeInsets.only(bottom: 12.px),
  211. child: _buildInput("请输入您的手机号", _accountNode, _accountCtl,
  212. TextInputAction.next,_onNextInput, Icons.account_circle,
  213. type: TextInputType.phone),
  214. ),
  215. Row(
  216. children: [
  217. Expanded(child: Container(
  218. margin: EdgeInsets.only(right: 12.px, bottom: 12.px),
  219. child: _buildInput("请输入短信验证码", _codeNode, _codeCtl,
  220. TextInputAction.done, _onNextInput, Icons.sms,
  221. type: TextInputType.number),
  222. )),
  223. CounterButton(onClick: _onGetCode)
  224. ],
  225. ),
  226. Container(
  227. margin: EdgeInsets.only(bottom: 12.px),
  228. child: _buildInput("请输入您的密码", _passwordNode, _passwordCtl,
  229. TextInputAction.done, _onLogin, Icons.lock, password: true,
  230. type: TextInputType.text),
  231. ),
  232. _buildInput("请再次输入您的密码", _rePasswordNode, _rePasswordCtl,
  233. TextInputAction.done, _onLogin, Icons.lock_outline, password: true,
  234. type: TextInputType.text),
  235. Container(
  236. height: 50.px,
  237. width: double.infinity,
  238. margin: EdgeInsets.only(left: 56.px, right: 56.px),
  239. padding: EdgeInsets.only(top: 12.px),
  240. child: FlatButton(
  241. color: Colors.white,
  242. shape: StadiumBorder(),
  243. onPressed: () { _onRegister(_accountCtl.text); },
  244. child: Text('注 册', style: TextStyle(fontSize: 16.px)),
  245. ),
  246. ),
  247. Container(
  248. margin: EdgeInsets.only(bottom: 22.px),
  249. child: Row(
  250. children: [
  251. Container(
  252. child: FlatButton(
  253. highlightColor: Colors.transparent,
  254. child: Text('验证码登陆', style: TextStyle(color:
  255. Colors.white, decoration: TextDecoration.underline)),
  256. onPressed: () { _jumpTo(2); }),
  257. ),
  258. Spacer(),
  259. Container(
  260. child: FlatButton(
  261. highlightColor: Colors.transparent,
  262. child: Text('已有账号?去登陆', style: TextStyle(color:
  263. Colors.white, decoration: TextDecoration.underline)),
  264. onPressed: () { _jumpTo(0); }),
  265. ),
  266. ],
  267. ),
  268. ),
  269. ],
  270. );
  271. }
  272. Widget _buildSMSLogin() {
  273. return Column(
  274. children: [
  275. Container(
  276. margin: EdgeInsets.only(bottom: 12.px),
  277. child: _buildInput("请输入您的手机号", _accountNode, _accountCtl,
  278. TextInputAction.next,_onNextInput, Icons.account_circle,
  279. type: TextInputType.phone),
  280. ),
  281. Row(
  282. children: [
  283. Expanded(child: Container(
  284. margin: EdgeInsets.only(right: 12.px, bottom: 12.px),
  285. child: _buildInput("请输入短信验证码", _codeNode, _codeCtl,
  286. TextInputAction.done, _onLogin, Icons.sms,
  287. type: TextInputType.number),
  288. )),
  289. CounterButton(onClick: _onGetCode)
  290. ],
  291. ),
  292. Container(
  293. height: 50.px,
  294. width: double.infinity,
  295. margin: EdgeInsets.only(left: 56.px, right: 56.px),
  296. padding: EdgeInsets.only(top: 12.px),
  297. child: FlatButton(
  298. color: Colors.white,
  299. shape: StadiumBorder(),
  300. onPressed: () { _onSmsLogin(_accountCtl.text); },
  301. child: Text('登 陆', style: TextStyle(fontSize: 16.px)),
  302. ),
  303. ),
  304. Container(
  305. margin: EdgeInsets.only(bottom: 22.px),
  306. child: Row(
  307. children: [
  308. Container(
  309. child: FlatButton(
  310. highlightColor: Colors.transparent,
  311. child: Text('账号密码登陆', style: TextStyle(color:
  312. Colors.white, decoration: TextDecoration.underline)),
  313. onPressed: () { _jumpTo(0); }),
  314. ),
  315. Spacer(),
  316. Container(
  317. child: FlatButton(
  318. highlightColor: Colors.transparent,
  319. child: Text('没有账号?去注册', style: TextStyle(color:
  320. Colors.white, decoration: TextDecoration.underline)),
  321. onPressed: () { _jumpTo(1); }),
  322. ),
  323. ],
  324. ),
  325. ),
  326. ],
  327. );
  328. }
  329. void _jumpTo(int index) {
  330. _pageController.animateToPage(index, curve: Curves.easeIn,
  331. duration: Duration(milliseconds: 260));
  332. }
  333. Widget _buildOtherLogin() {
  334. return Column(
  335. children: [
  336. Container(
  337. child: Text('其他登录方式', style: TextStyle(color: Colors.white)),
  338. ),
  339. Container(
  340. width: double.infinity,
  341. padding: EdgeInsets.only(top: 30),
  342. child: Row(
  343. children: <Widget>[
  344. Expanded(child: Container(
  345. width: 46,
  346. height: 46,
  347. child: InkWell(
  348. onTap: () => wechatLogin(),
  349. child: Image.asset("assets/images/wechat.png"),
  350. )
  351. )),
  352. ],
  353. ),
  354. )
  355. ],
  356. );
  357. }
  358. Widget _buildBottom() {
  359. return
  360. Container(
  361. margin: EdgeInsets.only(bottom: 12.px),
  362. alignment: Alignment.center,
  363. child: Wrap(
  364. children: [
  365. Text("登陆即视为您以阅读并同意",
  366. style: TextStyle(fontSize: 11.px, color: Colors.black54)),
  367. InkWell(
  368. onTap: () {
  369. Navigator.pushNamed(context, RouteNames.protocol,
  370. arguments: ProtocolType.USER);
  371. },
  372. child: Text("《用户注册协议》",
  373. style: TextStyle(fontSize: 11.px, color: Colors.white)),
  374. ),
  375. Text("和",
  376. style: TextStyle(fontSize: 11.px, color: Colors.black54)),
  377. InkWell(
  378. onTap: () {
  379. Navigator.pushNamed(context, RouteNames.protocol,
  380. arguments: ProtocolType.PRIVATE);
  381. },
  382. child: Text("《美天旺会员隐私协议》",
  383. style: TextStyle(fontSize: 11.px, color: Colors.white)),
  384. ),
  385. ],
  386. ),
  387. );
  388. }
  389. }