Skip to content

Commit 4df5288

Browse files
committed
Login 登陆界面
1 parent ea765cd commit 4df5288

File tree

4 files changed

+87
-14
lines changed

4 files changed

+87
-14
lines changed

lib/api/api.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Api{
2+
static const String BASE_URL = 'http://127.0.0.1:6001/';
3+
4+
static const String DO_LOGIN = BASE_URL+'doLogin';
5+
}

lib/model/user_info.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class UserInfo {
2+
String username;
3+
int id;
4+
String avatarPic;
5+
String themeColor;
6+
String urlName;
7+
8+
UserInfo({
9+
this.avatarPic,
10+
this.id,
11+
this.themeColor,
12+
this.urlName,
13+
this.username,
14+
});
15+
16+
factory UserInfo.fromJson(Map<String, dynamic> json) {
17+
return UserInfo(
18+
avatarPic: json['avatar_pic'],
19+
id: json['id'],
20+
username: json['username'],
21+
themeColor: json['theme_color'],
22+
urlName: json['url_name']);
23+
}
24+
}

lib/utils/data_utils.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dart:async' show Future;
2+
3+
import './net_utils.dart';
4+
import '../model/user_info.dart';
5+
import 'package:flutter_go/api/api.dart';
6+
7+
8+
class DataUtils{
9+
// 登陆获取用户信息
10+
static Future<UserInfo> doLogin(Map<String,String> params) async{
11+
var response = await NetUtils.post(Api.DO_LOGIN, params);
12+
print('url:${Api.DO_LOGIN} $response');
13+
UserInfo userInfo = UserInfo.fromJson(response['data']);
14+
return userInfo;
15+
}
16+
}

lib/views/login_page/login_page.dart

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_spinkit/flutter_spinkit.dart';
3+
import 'package:flutter_go/utils/data_utils.dart';
34

45
class LoginPage extends StatefulWidget {
56
@override
@@ -17,6 +18,7 @@ class _LoginPageState extends State<LoginPage> {
1718
bool isShowPassWord = false;
1819
String username = '';
1920
String password = '';
21+
bool isLoading = false;
2022

2123
// 创建登录界面的TextForm
2224
Widget buildSignInTextForm() {
@@ -136,22 +138,58 @@ class _LoginPageState extends State<LoginPage> {
136138
// 利用key来获取widget的状态FormState,可以用过FormState对Form的子孙FromField进行统一的操作
137139
if (_signInFormKey.currentState.validate()) {
138140
// 如果输入都检验通过,则进行登录操作
139-
Scaffold.of(context)
140-
.showSnackBar(new SnackBar(content: new Text("执行登录操作")));
141+
// Scaffold.of(context)
142+
// .showSnackBar(new SnackBar(content: new Text("执行登录操作")));
141143
//调用所有自孩子的save回调,保存表单内容
142-
_signInFormKey.currentState.save();
144+
doLogin();
143145
}
144146
},
145147
);
146148
}
147149

150+
// 登陆操作
151+
doLogin() {
152+
_signInFormKey.currentState.save();
153+
setState(() {
154+
isLoading = true;
155+
});
156+
DataUtils.doLogin({'username':username,'password':password}).then((result){
157+
print(result.username);
158+
setState(() {
159+
isLoading = false;
160+
});
161+
}).catchError((onError){
162+
print(onError);
163+
setState(() {
164+
isLoading = false;
165+
});
166+
});
167+
}
168+
148169
// 点击控制密码是否显示
149170
void showPassWord() {
150171
setState(() {
151172
isShowPassWord = !isShowPassWord;
152173
});
153174
}
154175

176+
Widget buildLoading() {
177+
if (isLoading) {
178+
return Opacity(
179+
opacity: .5,
180+
child: Container(
181+
width: MediaQuery.of(context).size.width * 0.85,
182+
decoration: BoxDecoration(
183+
borderRadius: BorderRadius.all(Radius.circular(8.0)),
184+
color: Colors.black,
185+
),
186+
child: SpinKitPouringHourglass(color: Colors.white),
187+
),
188+
);
189+
}
190+
return Container();
191+
}
192+
155193
@override
156194
Widget build(BuildContext context) {
157195
return Scaffold(
@@ -195,17 +233,7 @@ class _LoginPageState extends State<LoginPage> {
195233
top: 0,
196234
left: 0,
197235
bottom: 0,
198-
child: Opacity(
199-
opacity: .5,
200-
child: Container(
201-
width: MediaQuery.of(context).size.width * 0.85,
202-
decoration: BoxDecoration(
203-
borderRadius: BorderRadius.all(Radius.circular(8.0)),
204-
color: Colors.black,
205-
),
206-
child: SpinKitPouringHourglass(color: Colors.white),
207-
),
208-
),
236+
child: buildLoading(),
209237
)
210238
],
211239
),

0 commit comments

Comments
 (0)