login_page.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 'showAlert.dart';
  7. class LoginPage extends StatefulWidget {
  8. LoginPage({
  9. Key key,
  10. }): super(key: key);
  11. _LoginPageState createState() => _LoginPageState();
  12. }
  13. class _LoginPageState extends State < LoginPage > {
  14. String phone ='';
  15. String password='';
  16. bool loginStatus = true;
  17. @override
  18. Widget build(BuildContext context) {
  19. ScreenUtil.instance = ScreenUtil(width: 750, height: 1334,allowFontScaling:false)..init(context);
  20. return Scaffold(
  21. body: Container(
  22. color: Colors.white,
  23. child: ListView(
  24. children: < Widget > [
  25. Container(
  26. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(55), 0, ScreenUtil.getInstance().setHeight(55), 0),
  27. child: Column(
  28. children: < Widget > [
  29. Padding(
  30. padding: EdgeInsets.fromLTRB(0, ScreenUtil.getInstance().setHeight(150), 0, ScreenUtil.getInstance().setHeight(60)),
  31. child: Center(
  32. child: Image.asset(
  33. 'lib/images/icon_logo.png',
  34. width: ScreenUtil.getInstance().setWidth(210),
  35. height: ScreenUtil.getInstance().setHeight(210),
  36. fit: BoxFit.cover,
  37. ),
  38. ),
  39. ),
  40. Padding(
  41. padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(60)),
  42. child: TextField(
  43. keyboardType: TextInputType.phone,
  44. decoration: InputDecoration(
  45. border: UnderlineInputBorder(),
  46. hintText: "请输入手机号",
  47. hintStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(32), color: Color.fromRGBO(155, 155, 155, 1)),
  48. ),
  49. onChanged: (value){
  50. setState(() {
  51. phone = value;
  52. });
  53. print(phone);
  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. setState(() {
  103. loginStatus = false;
  104. });
  105. login(phone,password).then((resp){
  106. if(resp.success){
  107. setKey('token', resp.data.token);
  108. setKey('phone',phone);
  109. setKey('password',password);
  110. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context){
  111. return HomePage();
  112. }));
  113. }else{
  114. showDialog < Null > (
  115. context: context, //BuildContext对象
  116. barrierDismissible: false,
  117. builder: (BuildContext context) {
  118. return new LoadingDialog( //调用对话框
  119. text: '手机号或密码错误', childCallback: (val) {
  120. if (val) {
  121. setState(() {
  122. loginStatus = true;
  123. });
  124. } else {
  125. return;
  126. }
  127. },
  128. );
  129. });
  130. }
  131. });
  132. },
  133. )
  134. ],
  135. ),
  136. )
  137. ],
  138. ),
  139. ),
  140. );
  141. }
  142. }