main.dart
6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import 'package:flutter/material.dart';
import 'package:flutter_mes2/utils/Constant.dart';
import 'package:flutter_mes2/utils/ToastUtil.dart';
import 'package:flutter_mes2/widget/chooseWork.dart';
import 'package:flutter_mes2/widget/chooseWork.dart';
import 'package:flutter_mes2/widget/homePage.dart';
import 'package:oktoast/oktoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'bean/LoginrResult.dart';
import 'https/dio_utils.dart';
import 'https/http_utils.dart';
import 'setNetwork.dart';
import 'dart:convert';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return OKToast( // 这一步
child: new MaterialApp(
title: 'MES',
debugShowCheckedModeBanner: false,
theme: new ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: new MyHomePage(title: '登录页面'),
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
TextFieldAndCheckPageState createState() => TextFieldAndCheckPageState();
}
class TextFieldAndCheckPageState extends State<MyHomePage> {
FocusNode nameFocusNode = FocusNode();
FocusNode passFocusNode = FocusNode();
//手机号的控制器
TextEditingController nameController = TextEditingController();
//密码的控制器
TextEditingController passController = TextEditingController();
@override
void setState(fn) {
super.setState(fn);
}
@override
void initState() {
// TODO: implement initState
super.initState();
Future<String> mNetwork = getNetwork();
mNetwork.then((value) => DioUtils.network = value);
}
Future<String> getNetwork() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String network = prefs.getString(Constant.NETWORK);
if(network == null) {
network = Constant.DEFAULT_NETWORK;
}
return network;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body:
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new GestureDetector(
onTap: () {
///路由跳转
Navigator.push(context, MaterialPageRoute(builder: (_) {
return new setNetwork();
}));
},
child: Container(
width: 100,
height: 100,
margin: EdgeInsets.only(bottom: 30),
// child: Image.asset("images/a.jpeg",
// fit:BoxFit.cover,
// ),
child: Image.asset("images/sanyi2.png",
fit:BoxFit.cover,
),
),
),
Container(
margin: EdgeInsets.only(left:30, right: 30, bottom: 10),
child:
TextField(
controller: nameController,
focusNode: nameFocusNode,
decoration: InputDecoration(
border: OutlineInputBorder(),
contentPadding: EdgeInsets.all(10.0),
labelText: '请输入账号',
prefixIcon: Icon(Icons.person),
),
style:TextStyle(fontSize: 16)
),
),
Container(
margin: EdgeInsets.only(left:30, right: 30, bottom: 10),
child:
TextField(
controller: passController,
focusNode: passFocusNode,
decoration: InputDecoration(
border: OutlineInputBorder(),
contentPadding: EdgeInsets.all(10.0),
labelText: '请输入密码',
prefixIcon: Icon(Icons.lock)),
style:TextStyle(fontSize: 16),
obscureText: true),
),
Container(
height: 40,
margin: EdgeInsets.only(left:30, right: 30, bottom: 10, top:10),
width: double.infinity,
child: RaisedButton(
onPressed: _login,
shape: StadiumBorder(),
child: Text('登 录',
style: TextStyle(
fontSize: 20,
color: Colors.white
),),
color: Colors.deepOrangeAccent,
textColor: Colors.white,
),
),
],
),
),
);
}
void _login() {
print({'phone': nameController.text, 'password': passController.text});
String name = nameController.text.toString();
String password = passController.text.toString();
if(name.isEmpty) {
ToastUtil.showText( "请输入账号");
return;
}
if(password.isEmpty) {
ToastUtil.showText( "请输入密码");
return;
}
HttpUtils.login(context, {"username": name, "password": password}, success: (result) {
var moreData = result['data'];
LoginResult loginResult = LoginResult.fromJson(moreData.first);
nameFocusNode.unfocus();
passFocusNode.unfocus();
DioUtils.TOKEN = loginResult.token;
Navigator.push(context, MaterialPageRoute(builder: (_) {
return new chooseWork();
}));
}, fail: (code) {
});
}
void onTextClear() {
setState(() {
nameController.clear();
passController.clear();
});
}
}