Skip to content

Commit 8be7467

Browse files
committed
完成登录页
1 parent 17975bd commit 8be7467

File tree

9 files changed

+107
-76
lines changed

9 files changed

+107
-76
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
- [x] 首页(文章列表)
1212
- [x] 文章详情
1313
- [x] 新增文章
14-
- [ ] 登录
14+
- [x] 登录
1515
- [ ] 发表评论

lib/config.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
22

33
const appConfig = {'title': 'flutter个人博客模板'};
44

5-
const baseUrl = 'http://192.168.1.138:3001';
5+
const baseUrl = 'http://2.0.1.173:3001';
66

77
const primaryColor = const MaterialColor(
88
0xFF66BB6A,

lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:blog_flutter/pages/login.dart';
12
import 'package:blog_flutter/router.dart';
23
import 'package:flutter/material.dart';
34
import 'config.dart';
@@ -14,6 +15,6 @@ class App extends StatelessWidget {
1415
primarySwatch: primaryColor,
1516
),
1617
routes: routerMap,
17-
home: homeWidget);
18+
home: LoginWidget());
1819
}
1920
}

lib/pages/home.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class HomeWidgetState extends State<HomeWidget> {
1919

2020
void _getArticles() async {
2121
var response = await ArticleService.getArticles(_start, _limit);
22-
print(response);
2322
setState(() {
2423
_articles.addAll(response.data['data']);
2524
_total = response.data['total'];

lib/pages/login.dart

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import 'package:blog_flutter/config.dart';
2+
import 'package:blog_flutter/services/user_service.dart';
13
import 'package:flutter/material.dart';
4+
import 'package:fluttertoast/fluttertoast.dart';
25

36
class LoginWidget extends StatefulWidget {
47
@override
@@ -8,10 +11,15 @@ class LoginWidget extends StatefulWidget {
811
class LoginWidgetState extends State<LoginWidget> {
912
final title = '新增文章';
1013

11-
TextEditingController _unameController = new TextEditingController();
12-
TextEditingController _pwdController = new TextEditingController();
14+
TextEditingController _userNameController = new TextEditingController();
15+
TextEditingController _passwordController = new TextEditingController();
1316
GlobalKey _formKey = new GlobalKey<FormState>();
1417

18+
void _login(String userName, String password) async {
19+
await UserService.login(userName, password);
20+
Navigator.of(context).pushNamed('/home');
21+
}
22+
1523
@override
1624
Widget build(BuildContext context) {
1725
return Scaffold(
@@ -27,48 +35,39 @@ class LoginWidgetState extends State<LoginWidget> {
2735
children: <Widget>[
2836
TextFormField(
2937
autofocus: true,
30-
controller: _unameController,
31-
decoration: InputDecoration(
32-
labelText: "用户名",
33-
hintText: "用户名或邮箱",
34-
icon: Icon(Icons.person)),
38+
controller: _userNameController,
39+
decoration: InputDecoration(labelText: "用户名", hintText: "用户名或邮箱", icon: Icon(Icons.person)),
3540
// 校验用户名
3641
validator: (v) {
3742
return v.trim().length > 0 ? null : "用户名不能为空";
3843
}),
3944
TextFormField(
40-
controller: _pwdController,
41-
decoration: InputDecoration(
42-
labelText: "密码",
43-
hintText: "您的登录密码",
44-
icon: Icon(Icons.lock)),
45+
controller: _passwordController,
46+
decoration: InputDecoration(labelText: "密码", hintText: "您的登录密码", icon: Icon(Icons.lock)),
4547
obscureText: true,
4648
//校验密码
4749
validator: (v) {
48-
return v.trim().length > 5 ? null : "密码不能少于6位";
50+
return v.trim().length > 3 ? null : "密码不能少于4位";
4951
}),
5052
// 登录按钮
5153
Padding(
5254
padding: const EdgeInsets.only(top: 28.0),
5355
child: Row(
5456
children: <Widget>[
5557
Expanded(
56-
child: RaisedButton(
57-
padding: EdgeInsets.all(15.0),
58+
child: ElevatedButton(
5859
child: Text("登录"),
59-
color: Theme.of(context).primaryColor,
60-
textColor: Colors.white,
60+
style: ButtonStyle(
61+
foregroundColor: MaterialStateProperty.all(Colors.white),
62+
backgroundColor: MaterialStateProperty.resolveWith((states) {
63+
if (states.contains(MaterialState.pressed)) {
64+
return primaryColor[600];
65+
}
66+
return primaryColor;
67+
})),
6168
onPressed: () {
62-
//在这里不能通过此方式获取FormState,context不对
63-
//print(Form.of(context));
64-
65-
// 通过_formKey.currentState 获取FormState后,
66-
// 调用validate()方法校验用户名密码是否合法,校验
67-
// 通过后再提交数据。
6869
if ((_formKey.currentState as FormState).validate()) {
69-
//验证通过提交数据
70-
print(_unameController.text);
71-
print(_pwdController.text);
70+
_login(_userNameController.text, _passwordController.text);
7271
}
7372
},
7473
),

lib/services/http.dart

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:blog_flutter/config.dart';
22
import 'package:dio/dio.dart';
3-
import 'package:flutter/cupertino.dart';
43
import 'package:flutter/material.dart';
4+
import 'package:fluttertoast/fluttertoast.dart';
55

66
class Http {
77
factory Http() => _getInstance();
@@ -18,26 +18,26 @@ class Http {
1818
}
1919

2020
Http._() {
21-
BaseOptions options =
22-
new BaseOptions(baseUrl: baseUrl);
23-
InterceptorsWrapper interceptorsWrapper = new InterceptorsWrapper(
24-
onResponse: (res, handler) {
25-
// if (res.data.code != 0) {
26-
// return handler.reject(DioError(requestOptions: res.requestOptions));
27-
// }
28-
return handler.next(res);
29-
},
30-
onError: (e, handler) {
31-
AlertDialog(
32-
title: Text('网络出错'),
33-
content: Text(e.toString()),
34-
);
35-
return handler.next(e);
36-
},
37-
);
21+
BaseOptions options = new BaseOptions(baseUrl: baseUrl);
22+
DioInterceptor dioInterceptor = new DioInterceptor();
23+
_http = Dio(options)..interceptors.add(dioInterceptor);
24+
}
25+
}
26+
27+
class DioInterceptor extends InterceptorsWrapper {
28+
@override
29+
onResponse(response, handler) {
30+
if (response.data['code'] != 1) {
31+
Fluttertoast.showToast(msg: response.data['msg']);
32+
return null;
33+
}
34+
return handler.next(response);
35+
}
3836

39-
_http = Dio(options);
40-
_http.interceptors.add(interceptorsWrapper);
37+
@override
38+
onError(e, handler) {
39+
print(e.toString());
40+
return handler.next(e);
4141
}
4242
}
4343

lib/services/user_service.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
import 'package:blog_flutter/services/http.dart';
3+
4+
abstract class UserService {
5+
static Future login(String userName, String password) {
6+
return http.post('/login', data: {
7+
'userName': userName,
8+
'password': password
9+
});
10+
}
11+
}

0 commit comments

Comments
 (0)