login_page.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import 'package:flutter/material.dart';
  2. import 'package:fluttertoast/fluttertoast.dart';
  3. import 'package:provider/provider.dart';
  4. import '../model/user.dart';
  5. import 'welcome_page.dart';
  6. class LoginPage extends StatefulWidget {
  7. static const routeName = "/";
  8. @override
  9. _LoginPageState createState() {
  10. return _LoginPageState();
  11. }
  12. }
  13. class _LoginPageState extends State<LoginPage> {
  14. TextEditingController _pwdEditController;
  15. TextEditingController _userNameEditController;
  16. final FocusNode _userNameFocusNode = FocusNode();
  17. final FocusNode _pwdFocusNode = FocusNode();
  18. @override
  19. void initState() {
  20. super.initState();
  21. _pwdEditController = TextEditingController();
  22. _userNameEditController = TextEditingController();
  23. _pwdEditController.addListener(() => setState(() => {}));
  24. _userNameEditController.addListener(() => setState(() => {}));
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. final loginStatus = Provider.of<UserStatus>(context);
  29. return Scaffold(
  30. appBar: AppBar(
  31. leading: Icon(Icons.settings),
  32. title: Text("员工登录"),
  33. backgroundColor: Colors.green,
  34. elevation: 0,
  35. ),
  36. body: SingleChildScrollView(
  37. child: Column(
  38. crossAxisAlignment: CrossAxisAlignment.start,
  39. children: [
  40. buildTopWidget(context),
  41. SizedBox(
  42. height: 80,
  43. ),
  44. buildEditWidget(context),
  45. buildLoginButton(loginStatus)
  46. ],
  47. ),
  48. ),
  49. );
  50. }
  51. /// 头部
  52. Widget buildTopWidget(BuildContext context) {
  53. double height = 200.0;
  54. double width = MediaQuery.of(context).size.width;
  55. return Container(
  56. width: width,
  57. height: height,
  58. color: Colors.green,
  59. child: Stack(
  60. overflow: Overflow.visible, // 超出部分显示
  61. children: [
  62. Positioned(
  63. left: (width - 90) / 2.0,
  64. top: height - 45,
  65. child: Container(
  66. width: 90.0,
  67. height: 90.0,
  68. decoration: BoxDecoration(
  69. ///阴影
  70. boxShadow: [
  71. BoxShadow(color: Theme.of(context).cardColor, blurRadius: 4.0)
  72. ],
  73. ///形状
  74. shape: BoxShape.circle,
  75. ///图片
  76. image: DecorationImage(
  77. fit: BoxFit.cover,
  78. image: NetworkImage('https://picsum.photos/id/1/150'),
  79. ),
  80. ),
  81. ),
  82. )
  83. ],
  84. ),
  85. );
  86. }
  87. Widget buildEditWidget(BuildContext context) {
  88. return Container(
  89. margin: EdgeInsets.only(left: 15, right: 15),
  90. child: Column(
  91. children: [
  92. buildLoginNameTextField(),
  93. SizedBox(height: 20.0),
  94. buildPwdTextField(),
  95. ],
  96. ),
  97. );
  98. }
  99. Widget buildLoginNameTextField() {
  100. return Container(
  101. height: 40,
  102. decoration: BoxDecoration(
  103. color: Colors.grey[200],
  104. borderRadius: BorderRadius.all(Radius.circular(20.0)),
  105. ),
  106. child: Stack(
  107. children: [
  108. Positioned(
  109. left: 16,
  110. top: 11,
  111. width: 18,
  112. height: 18,
  113. child: Icon(Icons.supervised_user_circle),
  114. ),
  115. Positioned(
  116. left: 45,
  117. top: 10,
  118. bottom: 10,
  119. width: 1,
  120. child: Container(
  121. color: Colors.black,
  122. ),
  123. ),
  124. Positioned(
  125. left: 55,
  126. right: 10,
  127. top: 10,
  128. height: 30,
  129. child: TextField(
  130. controller: _userNameEditController,
  131. focusNode: _userNameFocusNode,
  132. decoration: InputDecoration(
  133. hintText: "请输入用户名",
  134. border: InputBorder.none,
  135. ),
  136. style: TextStyle(fontSize: 14),
  137. ),
  138. )
  139. ],
  140. ),
  141. );
  142. }
  143. Widget buildPwdTextField() {
  144. return Container(
  145. height: 40,
  146. decoration: BoxDecoration(
  147. color: Colors.grey[200],
  148. borderRadius: BorderRadius.all(Radius.circular(20.0)),
  149. ),
  150. child: Stack(
  151. children: [
  152. Positioned(
  153. left: 16,
  154. top: 11,
  155. width: 18,
  156. height: 18,
  157. child: Icon(Icons.security),
  158. ),
  159. Positioned(
  160. left: 45,
  161. top: 10,
  162. bottom: 10,
  163. width: 1,
  164. child: Container(
  165. color: Colors.black,
  166. ),
  167. ),
  168. Positioned(
  169. left: 55,
  170. right: 10,
  171. top: 10,
  172. height: 30,
  173. child: TextField(
  174. controller: _pwdEditController,
  175. focusNode: _pwdFocusNode,
  176. decoration: InputDecoration(
  177. hintText: "请输入密码",
  178. border: InputBorder.none,
  179. ),
  180. style: TextStyle(fontSize: 14),
  181. obscureText: true,
  182. /// 设置密码
  183. ),
  184. )
  185. ],
  186. ));
  187. }
  188. Widget buildLoginButton(UserStatus loginStatus) {
  189. return Container(
  190. margin: EdgeInsets.only(top: 40, left: 10, right: 10),
  191. padding: EdgeInsets.all(0),
  192. width: MediaQuery.of(context).size.width - 20,
  193. height: 40,
  194. child: RaisedButton(
  195. onPressed: () {
  196. print("【LoginPage】尝试登陆...");
  197. if (checkInput()) {
  198. Fluttertoast.showToast(
  199. msg: "登录成功",
  200. gravity: ToastGravity.CENTER,
  201. timeInSecForIos: 2,
  202. textColor: Colors.white,
  203. fontSize: 14.0);
  204. loginStatus.value = true;
  205. // print("loginStatus" + loginStatus.value);
  206. // Navigator.popAndPushNamed(context, OrderPage.routeName);
  207. print("【LoginPage】登陆完成,即将进行路由跳转...");
  208. Navigator.popAndPushNamed(context, WelcomePage.routeName);
  209. } else {
  210. Fluttertoast.showToast(
  211. msg: "账号或密码错误",
  212. gravity: ToastGravity.CENTER,
  213. timeInSecForIos: 2,
  214. textColor: Colors.white,
  215. fontSize: 14.0);
  216. }
  217. },
  218. child: Text("登录"),
  219. color: Colors.green,
  220. textColor: Colors.white,
  221. shape: RoundedRectangleBorder(
  222. borderRadius: BorderRadius.all(Radius.circular(20.0)),
  223. ),
  224. ),
  225. );
  226. }
  227. bool checkInput() {
  228. if (_userNameEditController.text.length == 0) {
  229. Fluttertoast.showToast(
  230. msg: "请输入用户名",
  231. gravity: ToastGravity.CENTER,
  232. timeInSecForIos: 2,
  233. textColor: Colors.white,
  234. fontSize: 14.0);
  235. return false;
  236. } else if (_pwdEditController.text.length == 0) {
  237. Fluttertoast.showToast(
  238. msg: "请输入密码",
  239. gravity: ToastGravity.CENTER,
  240. timeInSecForIos: 2,
  241. textColor: Colors.white,
  242. fontSize: 14.0);
  243. return false;
  244. }
  245. return true;
  246. }
  247. }