login_page.dart 7.0 KB

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