login_page.dart 6.0 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. },
  54. ),
  55. ),
  56. Padding(
  57. padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(125)),
  58. child: TextField(
  59. cursorColor: Color.fromRGBO(153, 153, 153, 0.5),
  60. keyboardType: TextInputType.text,
  61. obscureText:true,
  62. decoration: InputDecoration(
  63. hintText: "请输入密码",
  64. hintStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(32), color: Color.fromRGBO(155, 155, 155, 1)),
  65. ),
  66. onChanged: (value){
  67. setState(() {
  68. password =value;
  69. });
  70. },
  71. )
  72. ),
  73. InkWell(
  74. child: Container(
  75. height: ScreenUtil.getInstance().setHeight(100),
  76. decoration: BoxDecoration(
  77. color: (phone!=""&&password!="")?Color.fromRGBO(64, 98, 254, 1):Colors.grey,
  78. borderRadius: BorderRadius.circular(4),
  79. ),
  80. child: Center(
  81. child:Row(
  82. mainAxisAlignment: MainAxisAlignment.center,
  83. children: <Widget>[
  84. Offstage(
  85. offstage: loginStatus,
  86. child: SizedBox(
  87. width: ScreenUtil.getInstance().setWidth(45),
  88. height: ScreenUtil.getInstance().setHeight(45),
  89. child: Image.asset('lib/images/loading.gif'),
  90. ),
  91. ),
  92. Text('登录', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(36), color: Colors.white)),
  93. ],
  94. )
  95. ),
  96. ),
  97. onTap: () {
  98. if(phone=='' || password==''){
  99. return;
  100. }
  101. setState(() {
  102. loginStatus = false;
  103. });
  104. login(phone,password).then((resp){
  105. if(resp.success){
  106. setKey('token', resp.data.token);
  107. setKey('phone',phone);
  108. setKey('password',password);
  109. setKey('nickName',resp.data.nickName);
  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. }