Skip to content

Commit 0d72126

Browse files
committed
add toast
1 parent eab8f82 commit 0d72126

7 files changed

Lines changed: 62 additions & 30 deletions

File tree

‎lib/common/dao/UserDao.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class UserDao {
3737
print(res.data.toString());
3838
}
3939
if (callback != null) {
40-
callback(res.result);
40+
callback(res);
4141
}
4242
}
4343

‎lib/common/net/Api.dart‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
import 'package:dio/dio.dart';
42
import 'package:gsy_github_app_flutter/common/net/Code.dart';
53

@@ -26,7 +24,6 @@ class HttpManager {
2624
///[ text] 是否text返回
2725
///[ option] 配置
2826
static netFetch(url, params, Map<String, String> header, Options option) async {
29-
3027
//todo 没有网络
3128
/*var isConnected = await NetInfo.isConnected.fetch().done;
3229
if (!isConnected) {
@@ -55,7 +52,24 @@ class HttpManager {
5552
option.headers = headers;
5653

5754
Dio dio = new Dio();
58-
Response response = await dio.get(url, data: params, options: option);
55+
Response response;
56+
try {
57+
response = await dio.get(url, data: params, options: option);
58+
} on DioError catch (e) {
59+
Response errorResponse;
60+
if (e.response != null) {
61+
errorResponse = e.response;
62+
} else {
63+
errorResponse = new Response(statusCode: 666);
64+
}
65+
if (e.type == DioErrorType.CONNECT_TIMEOUT) {
66+
errorResponse.statusCode = Code.NETWORK_TIMEOUT;
67+
}
68+
if (Config.DEBUG) {
69+
print('请求异常: ' + e.toString());
70+
}
71+
return new ResultData(Code.errorHandleFunction(errorResponse.statusCode, e.message), false, errorResponse.statusCode);
72+
}
5973

6074
if (Config.DEBUG) {
6175
print('请求url: ' + url);
@@ -84,7 +98,7 @@ class HttpManager {
8498
print(e.toString() + url);
8599
return new ResultData(response.data, false, response.statusCode, headers: response.headers);
86100
}
87-
return new ResultData(Code.errorHandleFunction(response.statusCode), false, response.statusCode);
101+
return new ResultData(Code.errorHandleFunction(response.statusCode, ""), false, response.statusCode);
88102
}
89103

90104
///清除授权

‎lib/common/net/Code.dart‎

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
import 'package:fluttertoast/fluttertoast.dart';
3+
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
4+
25
///错误编码
36
class Code {
47
//网络错误
@@ -10,35 +13,29 @@ class Code {
1013

1114
static const SUCCESS = 200;
1215

13-
static errorHandleFunction(code) {
14-
/*switch (code) {
16+
static errorHandleFunction(code, message) {
17+
switch (code) {
1518
case 401:
16-
//授权逻辑
17-
if (Actions.currentScene !== 'LoginPage') {
19+
//TODO 授权逻辑
20+
/*if (Actions.currentScene !== 'LoginPage') {
1821
Actions.reset("LoginPage");
19-
}
20-
return "未授权或授权失败"; //401 Unauthorized
22+
}*/
23+
Fluttertoast.showToast(msg: GSYStrings.network_error_401);
24+
return GSYStrings.network_error_401; //401 Unauthorized
2125
case 403:
22-
Toast(I18n('noPower'));
23-
return "403权限错误";
26+
Fluttertoast.showToast(msg: GSYStrings.network_error_403);
27+
return GSYStrings.network_error_403;
2428
case 404:
25-
//Toast(I18n('notFound'));
26-
return "404错误";
27-
case 410:
28-
Toast(I18n('gone410'));
29-
return "410错误";
29+
Fluttertoast.showToast(msg: GSYStrings.network_error_404);
30+
return GSYStrings.network_error_404;
3031
case NETWORK_TIMEOUT:
3132
//超时
32-
Toast(I18n('netTimeout'));
33-
return I18n('netTimeout');
33+
Fluttertoast.showToast(msg: GSYStrings.network_error_timeout);
34+
return GSYStrings.network_error_timeout;
3435
default:
35-
if (statusText) {
36-
Toast(statusText);
37-
} else {
38-
Toast(I18n('errorUnKnow'));
39-
}
40-
return "其他异常"
41-
}*/
36+
Fluttertoast.showToast(msg: GSYStrings.network_error_unknown + " " + message);
37+
return GSYStrings.network_error_unknown;
38+
}
4239
}
4340
}
4441

‎lib/common/style/GSYStyle.dart‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ class GSYStrings {
3434
static const String login_text = "登录";
3535
static const String login_username_hint_text = "请输入github用户名";
3636
static const String login_password_hint_text = "请输入密码";
37+
static const String login_success = "登录成功";
38+
39+
static const String network_error_401 = "未授权或授权登录失败";
40+
static const String network_error_403 = "403权限错误";
41+
static const String network_error_404 = "404错误";
42+
static const String network_error_timeout = "请求超时";
43+
static const String network_error_unknown = "其他异常";
44+
45+
3746
}

‎lib/page/LoginPage.dart‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:fluttertoast/fluttertoast.dart';
23
import 'package:gsy_github_app_flutter/common/config/Config.dart';
34
import 'package:gsy_github_app_flutter/common/dao/UserDao.dart';
45
import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart';
@@ -67,7 +68,6 @@ class _LoginPageState extends State<LoginPage> {
6768
hintText: GSYStrings.login_password_hint_text,
6869
iconData: Icons.access_alarm,
6970
onChanged: (String value) {
70-
print(value);
7171
_password = value;
7272
},
7373
controller: pwController,
@@ -84,7 +84,11 @@ class _LoginPageState extends State<LoginPage> {
8484
if (_password == null || _password.length == 0) {
8585
return;
8686
}
87-
UserDao.login(_userName, _password, (data) {});
87+
UserDao.login(_userName, _password, (data) {
88+
if (data != null && data.result == true) {
89+
Fluttertoast.showToast(msg: GSYStrings.login_success);
90+
}
91+
});
8892
},
8993
)
9094
],

‎pubspec.lock‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ packages:
9595
description: flutter
9696
source: sdk
9797
version: "0.0.0"
98+
fluttertoast:
99+
dependency: "direct main"
100+
description:
101+
name: fluttertoast
102+
url: "https://pub.flutter-io.cn"
103+
source: hosted
104+
version: "2.0.4"
98105
front_end:
99106
dependency: transitive
100107
description:

‎pubspec.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies:
1010
cupertino_icons: ^0.1.2
1111
dio: ^0.0.14
1212
shared_preferences: ^0.4.2
13+
fluttertoast: ^2.0.4
1314

1415
dev_dependencies:
1516
flutter_test:

0 commit comments

Comments
 (0)