login.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import 'package:flutter/material.dart';
  2. import 'package:bot_toast/bot_toast.dart';
  3. import 'package:twong/api/index.dart';
  4. import 'package:twong/config/style.dart';
  5. import 'package:twong/providers/user_model.dart';
  6. import 'package:twong/router/index.dart';
  7. import 'package:twong/utils/index.dart';
  8. import 'package:twong/config/protocols.dart';
  9. import 'package:provider/provider.dart';
  10. class LoginPage extends StatefulWidget {
  11. @override
  12. State<StatefulWidget> createState() {
  13. return _LoginPageState();
  14. }
  15. }
  16. class _LoginPageState extends State<LoginPage> {
  17. bool read = false;
  18. FocusNode _accountNode = new FocusNode();
  19. FocusNode _passwordNode = new FocusNode();
  20. TextEditingController _accountCtl = TextEditingController();
  21. TextEditingController _passwordCtl = TextEditingController();
  22. _onLogin(String value) {
  23. var account = _accountCtl.text;
  24. var password = _passwordCtl.text;
  25. if(account.trim() == "" || password.trim() == "") {
  26. return;
  27. }
  28. Network.inst.login(account: account, password: password).then((value) {
  29. Cache.updateAccount(account, password);
  30. Navigator.pop(context);
  31. BotToast.showText(text: "登陆成功!");
  32. }).catchError((err) {
  33. Log.e(err);
  34. BotToast.showText(text: err.toString());
  35. });
  36. }
  37. _onNextInput(String value) {
  38. FocusScope.of(context).requestFocus(_passwordNode);
  39. }
  40. wechatLogin () {
  41. Utils.notOpen();
  42. }
  43. Widget _buildInput(String hit, FocusNode node, TextEditingController controller,
  44. TextInputAction action, Function(String) onSubmit, IconData icon,
  45. {bool password = false, TextInputType type}) {
  46. return Container(
  47. padding: EdgeInsets.only(left: 30, right: 30, top: 10.px),
  48. child: TextField(
  49. focusNode: node,
  50. obscureText: password,
  51. onSubmitted: onSubmit,
  52. controller: controller,
  53. keyboardType: type,
  54. style: TextStyle(color: Colors.white),
  55. textInputAction: action,
  56. decoration: InputDecoration(
  57. filled: true,
  58. hintText: hit,
  59. border: InputBorder.none,
  60. hintStyle: TextStyle(color: Colors.white70),
  61. enabledBorder: OutlineInputBorder(
  62. borderSide: BorderSide(color: Color(0x00FF00a0)),
  63. borderRadius: BorderRadius.all(
  64. Radius.circular(100),
  65. ),
  66. ),
  67. focusedBorder: OutlineInputBorder(
  68. borderSide: BorderSide(color: Color(0x000000a0)),
  69. borderRadius: BorderRadius.all(
  70. Radius.circular(100),
  71. ),
  72. ),
  73. contentPadding: EdgeInsets.all(10.px),
  74. prefixIcon: Icon(icon, color: Colors.white),
  75. ),
  76. )
  77. );
  78. }
  79. @override
  80. Widget build(BuildContext context) {
  81. return Scaffold(
  82. body: Container(
  83. decoration: BoxDecoration(
  84. image: DecorationImage(
  85. fit: BoxFit.cover,
  86. image: AssetImage('assets/images/launcher.png'),
  87. )
  88. ),
  89. child: ListView(
  90. physics: ClampingScrollPhysics(),
  91. children: <Widget>[
  92. Container(
  93. alignment: Alignment.topRight,
  94. child: FlatButton(
  95. highlightColor: Colors.transparent,
  96. child: Text('跳过,看好货 >',
  97. style: TextStyle(color: Colors.white)
  98. ),
  99. onPressed: () { Navigator.pop(context); }
  100. ),
  101. ),
  102. Container(
  103. height: 100.px,
  104. margin: EdgeInsets.only(bottom: 50.px, top: 10.px),
  105. child: Center(
  106. child: Image.asset("assets/images/logo.png"),
  107. ),
  108. ),
  109. _buildInput("请输入您的账号", _accountNode, _accountCtl,
  110. TextInputAction.next,_onNextInput, Icons.account_circle,
  111. type: TextInputType.phone),
  112. _buildInput("请输入您的密码", _passwordNode, _passwordCtl,
  113. TextInputAction.done, _onLogin, Icons.lock, password: true,
  114. type: TextInputType.text),
  115. Container(
  116. height: 50.px,
  117. margin: EdgeInsets.only(left: 56.px, right: 56.px),
  118. padding: EdgeInsets.only(top: 12.px),
  119. child: FlatButton(
  120. color: Colors.white,
  121. shape: StadiumBorder(),
  122. onPressed: () { _onLogin(_accountCtl.text); },
  123. child: Text('登 陆', style: TextStyle(fontSize: 16.px)),
  124. ),
  125. ),
  126. Container(
  127. padding: EdgeInsets.only(right: 20.px, bottom: 20.px),
  128. alignment: Alignment.topRight,
  129. child: FlatButton(
  130. highlightColor: Colors.transparent,
  131. child: Text('遇到问题?', style: TextStyle(color: Colors.white)),
  132. onPressed: () {}),
  133. ),
  134. Center(
  135. child: Text('其他登录方式', style: TextStyle(color: Colors.white)),
  136. ),
  137. Container(
  138. padding: EdgeInsets.only(top: 30),
  139. child: Flex(
  140. direction: Axis.horizontal,
  141. children: <Widget>[
  142. Expanded(child: Container(
  143. width: 46,
  144. height: 46,
  145. child: InkWell(
  146. onTap: () => wechatLogin(),
  147. child: Image.asset("assets/images/wechat.png"),
  148. )
  149. )),
  150. ],
  151. ),
  152. )
  153. ],
  154. ),
  155. ),
  156. );
  157. }
  158. }