Skip to content

Commit 22ced29

Browse files
committed
feat:增加loading‘
1 parent bbd3bec commit 22ced29

File tree

14 files changed

+397
-226
lines changed

14 files changed

+397
-226
lines changed

lib/http/index.dart

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import 'package:dio/dio.dart'
2+
show
3+
Dio,
4+
DioError,
5+
Options,
6+
InterceptorsWrapper,
7+
RequestOptions,
8+
LogInterceptor,
9+
Response;
10+
import 'package:efox_flutter/utils/localStorage.dart' show LocalStorage;
11+
import 'log.dart' show log;
12+
import 'package:efox_flutter/store/index.dart' show Store;
13+
import 'package:flutter/material.dart';
14+
import 'loadingDialog.dart' show showAppLoading;
15+
16+
Dio getDio([Options options]) {
17+
if (options == null) {
18+
options = Options(
19+
headers: {
20+
'context-type': 'application/json',
21+
},
22+
);
23+
}
24+
Dio dio = new Dio(); // with default Options
25+
26+
// dio.options.baseUrl = "http://www.dtworkroom.com/doris/1/2.0.0/";
27+
dio.options.connectTimeout = 30 * 1000; //5s
28+
dio.options.receiveTimeout = 30 * 1000;
29+
dio.options.headers = options.headers;
30+
31+
// Add request interceptor
32+
dio.interceptors.add(InterceptorsWrapper(
33+
onRequest: (RequestOptions options) async {
34+
String token = await LocalStorage.get('githubRespLoginToken');
35+
if (options.headers['Authorization'] == null && token != null) {
36+
options.headers['Authorization'] = 'token $token';
37+
}
38+
print(
39+
' Store.widgetCtx -------------------------------- ${Store.widgetCtx}');
40+
showAppLoading();
41+
log('【发送请求】 ${options.uri} ', '${options.headers} ${options.data}');
42+
// Do something before request is sent
43+
return options; //continue
44+
// If you want to resolve the request with some custom data,
45+
// you can return a `Response` object or return `dio.resolve(data)`.
46+
// If you want to reject the request with a error message,
47+
// you can return a `DioError` object or return `dio.reject(errMsg)`
48+
},
49+
onResponse: (Response response) async {
50+
log('【请求成功】 ${response.request.uri},【状态码 ${response.statusCode}】',
51+
response);
52+
showAppLoading();
53+
return {'data': response.data}; // continue
54+
},
55+
onError: (DioError e) async {
56+
// showAppLoading();
57+
dynamic msg = e.message;
58+
dynamic code = 0; // 错误码
59+
dynamic status = e.type; // http请求状态
60+
if (e.response != null && e.response.data != null) {
61+
code = 1;
62+
msg = e.response.data;
63+
status = e.response.statusCode;
64+
}
65+
log('【请求失败】 ${e.request.uri},【状态码 ${status}】【code】: ${code}', msg);
66+
return {'msg': msg, 'code': code, 'status': status};
67+
},
68+
));
69+
dio.interceptors.add(LogInterceptor(responseBody: false)); //开启请求日志
70+
71+
return dio;
72+
}
73+
74+
Future<dynamic> get({url, data = const {}}) async {
75+
return getDio().get(url).then((resp) => resp.data);
76+
}
77+
78+
Future post({url, data = const {}, options}) async {
79+
return getDio(options).post(url, data: data).then((resp) {
80+
return resp.data;
81+
});
82+
}

lib/http/loading.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'package:flutter/material.dart';
2+
3+
// ignore: must_be_immutable
4+
class NetLoadingDialog extends StatefulWidget {
5+
String loadingText;
6+
bool outsideDismiss;
7+
8+
Function dismissDialog;
9+
10+
NetLoadingDialog(
11+
{Key key,
12+
this.loadingText = "loading...",
13+
this.outsideDismiss = true,
14+
this.dismissDialog})
15+
: super(key: key);
16+
17+
@override
18+
State<NetLoadingDialog> createState() => _LoadingDialog();
19+
}
20+
21+
class _LoadingDialog extends State<NetLoadingDialog> {
22+
_dismissDialog() {
23+
Navigator.of(context).pop();
24+
}
25+
26+
@override
27+
void initState() {
28+
super.initState();
29+
if (widget.dismissDialog != null) {
30+
widget.dismissDialog(
31+
32+
//将关闭 dialog的方法传递到调用的页面.
33+
(){Navigator.of(context).pop();}
34+
35+
);
36+
}
37+
}
38+
39+
@override
40+
Widget build(BuildContext context) {
41+
return Text('haha');
42+
}
43+
}

lib/http/loadingDialog.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:efox_flutter/store/index.dart' show Store;
3+
4+
OverlayEntry overlayEntry = null;
5+
void showAppLoading() {
6+
// showDialog(
7+
// context: Store.widgetCtx,
8+
// builder: (context) {
9+
// return Center(
10+
// child: CircularProgressIndicator(
11+
// // value: 0.5,
12+
// semanticsLabel: 'hsh',
13+
// semanticsValue: 'sds',
14+
// ),
15+
// );
16+
// });
17+
if (overlayEntry?.maintainState == true) {
18+
print('移除啦~~~');
19+
overlayEntry.maintainState = false;
20+
overlayEntry.remove();
21+
return;
22+
}
23+
var overlayState = Overlay.of(Store.widgetCtx);
24+
overlayEntry = new OverlayEntry(builder: (context) {
25+
return Scaffold(
26+
backgroundColor: Colors.transparent,
27+
body: GestureDetector(
28+
child: Column(
29+
crossAxisAlignment: CrossAxisAlignment.center,
30+
mainAxisAlignment: MainAxisAlignment.center,
31+
children: <Widget>[CircularProgressIndicator(), Text('loading...')],
32+
),
33+
onTap: () {
34+
print('移除啦~~~ ${overlayEntry.maintainState}');
35+
overlayEntry.maintainState = false;
36+
overlayEntry?.remove();
37+
print('overlayEntry ${overlayEntry}');
38+
},
39+
),
40+
);
41+
});
42+
// 加载框
43+
overlayState.insert(overlayEntry);
44+
overlayEntry.maintainState = true;
45+
46+
// Navigator.of(Store.widgetCtx).push(MaterialPageRoute(builder: (context) {
47+
// return Index();
48+
// }));
49+
}
50+
51+
// class Index extends Dialog {
52+
// @override
53+
// Widget build(BuildContext context) {
54+
// final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
55+
// return Scaffold(
56+
// backgroundColor: Colors.transparent,
57+
// body: Center(
58+
// child: Column(
59+
// children: <Widget>[CircularProgressIndicator(), Text('loading...')],
60+
// ),
61+
// ),
62+
// );
63+
// }
64+
// }

lib/http/log.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* 日志打印
3+
*/
4+
void log(title, message) {
5+
if (message == null) {
6+
print('$title');
7+
} else {
8+
print('$title');
9+
print('$message');
10+
print('end');
11+
};
12+
}

lib/main.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ class MainAppState extends State<MainApp> {
2626
//实例化多语言
2727
super.initState();
2828
_delegate = AppLocalizationsDelegate();
29+
Store.setStoreCtx(context); // 初始化数据层
2930
}
3031

3132
@override
3233
Widget build(BuildContext context) {
33-
print('main view rebuild $context');
3434
Store.value<ConfigModel>(context).$getTheme();
35-
Store.setContext(context);
35+
3636
return Store.connect<ConfigModel>(
3737
builder: (context, child, model) {
38-
print('store connect context =$context');
3938
return MaterialApp(
4039
localeResolutionCallback: (deviceLocale, supportedLocales) {
4140
print(
@@ -45,7 +44,7 @@ class MainAppState extends State<MainApp> {
4544
: Locale('zh');
4645
return _locale;
4746
},
48-
onGenerateTitle: (context) {
47+
onGenerateTitle: (ctx) {
4948
// 设置多语言代理
5049
AppLocalizations.setProxy(setState, _delegate);
5150
return 'flutter';

lib/page/app-login/index.dart

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'package:flutter/material.dart';
22
import 'package:efox_flutter/lang/index.dart' show AppLocalizations;
33
import 'package:efox_flutter/store/index.dart' show Store, UserModel;
4-
class Index extends StatefulWidget {
54

5+
class Index extends StatefulWidget {
66
Index({Key key}) : super(key: key);
77

88
@override
@@ -15,7 +15,7 @@ class _IndexState extends State<Index> {
1515

1616
GlobalKey _formKey = GlobalKey<FormState>();
1717
@override
18-
Widget build(BuildContext context) {
18+
Widget build(BuildContext ctx) {
1919
return Scaffold(
2020
appBar: AppBar(
2121
centerTitle: true,
@@ -25,70 +25,74 @@ class _IndexState extends State<Index> {
2525
),
2626
automaticallyImplyLeading: false,
2727
),
28-
body: SingleChildScrollView(
29-
child: Padding(
30-
padding: EdgeInsets.symmetric(vertical: 50, horizontal: 24),
31-
child: Form(
32-
key: _formKey,
33-
autovalidate: true,
34-
child: Column(
35-
children: <Widget>[
36-
TextFormField(
37-
controller: nameCtl,
38-
autofocus: true,
39-
decoration: InputDecoration(
40-
labelText: '账户名',
41-
hintText: '请输入Github账户名',
42-
icon: Icon(Icons.person),
28+
body: Builder(builder: (BuildContext context) {
29+
// Store.setWidgetCtx(context);
30+
return SingleChildScrollView(
31+
child: Padding(
32+
padding: EdgeInsets.symmetric(vertical: 50, horizontal: 24),
33+
child: Form(
34+
key: _formKey,
35+
autovalidate: true,
36+
child: Column(
37+
children: <Widget>[
38+
TextFormField(
39+
controller: nameCtl,
40+
autofocus: true,
41+
decoration: InputDecoration(
42+
labelText: '账户名',
43+
hintText: '请输入Github账户名',
44+
icon: Icon(Icons.person),
45+
),
46+
validator: (v) {
47+
return v.trim().length > 0 ? null : '用户名不能为空';
48+
},
4349
),
44-
validator: (v) {
45-
return v.trim().length > 0 ? null : '用户名不能为空';
46-
},
47-
),
48-
TextFormField(
49-
controller: pwdCtl,
50-
decoration: InputDecoration(
51-
labelText: '密码',
52-
hintText: '请输入登录密码',
53-
icon: Icon(Icons.lock),
50+
TextFormField(
51+
controller: pwdCtl,
52+
decoration: InputDecoration(
53+
labelText: '密码',
54+
hintText: '请输入登录密码',
55+
icon: Icon(Icons.lock),
56+
),
57+
obscureText: true,
58+
validator: (v) {
59+
return v.trim().length > 5 ? null : "密码不能少于6位";
60+
},
5461
),
55-
obscureText: true,
56-
validator: (v) {
57-
return v.trim().length > 5 ? null : "密码不能少于6位";
58-
},
59-
),
60-
Padding(
61-
padding: EdgeInsets.only(top: 50),
62-
child: Row(
63-
children: <Widget>[
64-
Expanded(
65-
child: RaisedButton(
66-
padding: EdgeInsets.all(15),
67-
color: Theme.of(context).primaryColor,
68-
textColor:
69-
Theme.of(context).primaryTextTheme.title.color,
70-
child: Text(
71-
AppLocalizations.$t('common.login'),
62+
Padding(
63+
padding: EdgeInsets.only(top: 50),
64+
child: Row(
65+
children: <Widget>[
66+
Expanded(
67+
child: RaisedButton(
68+
padding: EdgeInsets.all(15),
69+
color: Theme.of(context).primaryColor,
70+
textColor:
71+
Theme.of(context).primaryTextTheme.title.color,
72+
child: Text(
73+
AppLocalizations.$t('common.login'),
74+
),
75+
onPressed: () async {
76+
if ((_formKey.currentState as FormState)
77+
.validate()) {
78+
Store.value<UserModel>(context)
79+
.$loginController(context, {
80+
'name': nameCtl.text,
81+
'pwd': pwdCtl.text
82+
});
83+
}
84+
},
7285
),
73-
onPressed: () {
74-
if ((_formKey.currentState as FormState)
75-
.validate()) {
76-
Store.value<UserModel>(context).$login({
77-
'name': nameCtl.text,
78-
'pwd':pwdCtl.text
79-
});
80-
}
81-
},
82-
),
83-
)
84-
],
85-
),
86-
)
87-
],
86+
)
87+
],
88+
),
89+
)
90+
],
91+
),
8892
),
8993
),
90-
),
91-
),
94+
);
95+
}),
9296
);
9397
}
9498
}

0 commit comments

Comments
 (0)