login_page.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'util/session.dart';
  4. import 'home_page.dart';
  5. import 'util/api.dart';
  6. import 'util/util.dart';
  7. import 'util/models.dart';
  8. class LoginPage extends StatefulWidget {
  9. LoginPage({
  10. Key key,
  11. }): super(key: key);
  12. _LoginPageState createState() => _LoginPageState();
  13. }
  14. class _LoginPageState extends State < LoginPage > {
  15. String phone ='';
  16. String password='';
  17. bool loginStatus = true;
  18. @override
  19. Widget build(BuildContext context) {
  20. ScreenUtil.instance = ScreenUtil(width: 750, height: 1334,allowFontScaling:false)..init(context);
  21. return Scaffold(
  22. body: Container(
  23. color: Colors.white,
  24. child: ListView(
  25. children: < Widget > [
  26. Container(
  27. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(55), 0, ScreenUtil.getInstance().setHeight(55), 0),
  28. child: Column(
  29. children: < Widget > [
  30. Padding(
  31. padding: EdgeInsets.fromLTRB(0, ScreenUtil.getInstance().setHeight(150), 0, ScreenUtil.getInstance().setHeight(60)),
  32. child: Center(
  33. child: Image.asset(
  34. 'lib/images/icon_logo.png',
  35. width: ScreenUtil.getInstance().setWidth(210),
  36. height: ScreenUtil.getInstance().setHeight(210),
  37. fit: BoxFit.cover,
  38. ),
  39. ),
  40. ),
  41. Padding(
  42. padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(60)),
  43. child: TextField(
  44. keyboardType: TextInputType.phone,
  45. decoration: InputDecoration(
  46. border: UnderlineInputBorder(),
  47. hintText: "请输入手机号",
  48. hintStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(32), color: Color.fromRGBO(155, 155, 155, 1)),
  49. ),
  50. onChanged: (value){
  51. setState(() {
  52. phone = value;
  53. });
  54. },
  55. ),
  56. ),
  57. Padding(
  58. padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(125)),
  59. child: TextField(
  60. cursorColor: Color.fromRGBO(153, 153, 153, 0.5),
  61. keyboardType: TextInputType.text,
  62. obscureText:true,
  63. decoration: InputDecoration(
  64. hintText: "请输入密码",
  65. hintStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(32), color: Color.fromRGBO(155, 155, 155, 1)),
  66. ),
  67. onChanged: (value){
  68. setState(() {
  69. password =value;
  70. });
  71. },
  72. )
  73. ),
  74. InkWell(
  75. child: Container(
  76. height: ScreenUtil.getInstance().setHeight(100),
  77. decoration: BoxDecoration(
  78. color: (phone!=""&&password!="")?Color.fromRGBO(64, 98, 254, 1):Colors.grey,
  79. borderRadius: BorderRadius.circular(4),
  80. ),
  81. child: Center(
  82. child:Row(
  83. mainAxisAlignment: MainAxisAlignment.center,
  84. children: <Widget>[
  85. Offstage(
  86. offstage: loginStatus,
  87. child: SizedBox(
  88. width: ScreenUtil.getInstance().setWidth(45),
  89. height: ScreenUtil.getInstance().setHeight(45),
  90. child: Image.asset('lib/images/loading.gif'),
  91. ),
  92. ),
  93. Text('登录', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(36), color: Colors.white)),
  94. ],
  95. )
  96. ),
  97. ),
  98. onTap: () {
  99. if(phone=='' || password==''){
  100. return;
  101. }
  102. if(!loginStatus) return;//防止重复提交
  103. setState(() {
  104. loginStatus = false;
  105. });
  106. login(phone,password).then((resp){
  107. if(isNotError(context, resp)){
  108. Login loginObj = Login.fromJson(resp.data);
  109. setKey('token', loginObj.data.token);
  110. setKey('phone',phone);
  111. setKey('password',password);
  112. setKey('nickName',loginObj.data.nickName);
  113. setKey('printType','1');//设置打印类型
  114. setKey('stationId', loginObj.data.stationId);//设置站点id
  115. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context){
  116. return HomePage();
  117. }));
  118. }else{
  119. setState(() {
  120. loginStatus = true;
  121. });
  122. }
  123. });
  124. },
  125. )
  126. ],
  127. ),
  128. )
  129. ],
  130. ),
  131. ),
  132. );
  133. }
  134. }