login.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:dio/dio.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/cupertino.dart';
  7. import 'package:fluttertoast/fluttertoast.dart';
  8. import 'package:keyboard_avoider/keyboard_avoider.dart';
  9. import 'package:twongCustomer/utils/index.dart';
  10. import '../utils/http.dart';
  11. import '../utils/cache.dart';
  12. import '../routes/utils.dart';
  13. import '../utils/loading.dart';
  14. import '../utils/size_fit.dart';
  15. import '../widgets/input_box.dart';
  16. import '../utils/notification.dart';
  17. class LoginPage extends StatefulWidget {
  18. @override
  19. State<StatefulWidget> createState() {
  20. return _LoginPageState();
  21. }
  22. }
  23. class _LoginPageState extends State<LoginPage> {
  24. bool _loading = false;
  25. String username = "";
  26. String password = "";
  27. FocusNode userNode = new FocusNode();
  28. FocusNode passNode = new FocusNode();
  29. HttpClient http = new HttpClient();
  30. final ScrollController _scrollController = ScrollController();
  31. @override
  32. void initState() {
  33. super.initState();
  34. Http.init();
  35. Notifications.init(context);
  36. username = Cache.get("username");
  37. password = Cache.get("password");
  38. username = username == null ? "" : username;
  39. password = password == null ? "" : password;
  40. // if (Cache.get("autoLogin") != null) {
  41. // Loading.show(context);
  42. // Http.autoLogin().then((value) {
  43. // // RouterUtils.toMain(context);
  44. // }).catchError((err) {
  45. // Loading.hide(context);
  46. // Fluttertoast.showToast(msg: err.toString());
  47. // });
  48. // }
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. HYSizeFit.initialize(context);
  53. return Scaffold(
  54. body: SingleChildScrollView(
  55. child: Column(
  56. children: [
  57. Container(
  58. margin: EdgeInsets.only(top: 20.px),
  59. child: Row(
  60. children: [
  61. Spacer(),
  62. IconButton(icon: Icon(Icons.settings), onPressed: toSetting)
  63. ],
  64. ),
  65. ),
  66. Container(height: 80.px),
  67. Container(
  68. margin: EdgeInsets.only(left: 12.px, right: 12.px),
  69. child: InputBoxContainer(
  70. value: username,
  71. focusNode: userNode,
  72. hintText: "请输入您的账号",
  73. action: TextInputAction.next,
  74. onChange: (value) => username = value,
  75. submit: (value) {
  76. FocusScope.of(context).requestFocus(passNode);
  77. },
  78. icon: Icons.account_circle,
  79. ),
  80. ),
  81. Container(
  82. margin: EdgeInsets.only(left: 12.px, right: 12.px),
  83. child: InputBoxContainer(
  84. password: true,
  85. value: password,
  86. focusNode: passNode,
  87. hintText: "请输入您的密码",
  88. action: TextInputAction.done,
  89. onChange: (value) => password = value,
  90. submit: (value) => _login(),
  91. icon: Icons.lock_sharp,
  92. ),
  93. ),
  94. _buildLoginBtn()
  95. ],
  96. ),
  97. ),
  98. );
  99. }
  100. Widget _buildLoginBtn() {
  101. if (_loading) return Center(
  102. child: CircularProgressIndicator(),
  103. );
  104. return Container(
  105. child: FlatButton(
  106. height: 40,
  107. minWidth: 300,
  108. shape: RoundedRectangleBorder(
  109. borderRadius: BorderRadius.all(Radius.circular(20))
  110. ),
  111. textColor: Colors.white,
  112. color: Colors.blue,
  113. onPressed: _login,
  114. child: Text("登陆"),
  115. ));
  116. }
  117. toSetting () {
  118. // Routers.router().navigateTo(context, "setting");
  119. RouterUtils.toSetting(context);
  120. }
  121. _login () async {
  122. if(_loading) return;
  123. if(username.trim() == "" || password.trim() == "") {
  124. Fluttertoast.showToast(msg: "用户名密码不能为空!");
  125. return;
  126. }
  127. setState(() {
  128. _loading = true;
  129. });
  130. Loading.show(context);
  131. // HttpClient client = new HttpClient();
  132. // var httpRequest = await client.getUrl(Uri.parse("https://twongkefu.shotshock.shop/kefuinfo"));
  133. // HttpClientResponse response = await httpRequest.close();
  134. // response.transform(utf8.decoder).join().then((value){
  135. // print(value);
  136. // Loading.hide(context);
  137. // setState(() {
  138. // _loading = false;
  139. // });
  140. // });
  141. // var dio = new Dio();
  142. // dio.post("https://google.com", data: FormData.fromMap({
  143. // "username": username, "password": password
  144. // })).then((data) {
  145. // print(data.data);
  146. // setState(() {
  147. // _loading = false;
  148. // });
  149. // Loading.hide(context);
  150. // });
  151. Http.login({ "username": username, "password": password}).then((data){
  152. Loading.hide(context);
  153. Cache.set("username", username);
  154. Cache.set("password", password);
  155. Cache.set("autoLogin", "true");
  156. RouterUtils.toMain(context);
  157. }).catchError((err) {
  158. print(err);
  159. Loading.hide(context);
  160. setState(() {
  161. _loading = false;
  162. });
  163. Fluttertoast.showToast(msg: err.toString());
  164. });
  165. }
  166. }