Skip to content

Commit 720564c

Browse files
committed
feat:增加loading效果,待完善
1 parent 22ced29 commit 720564c

File tree

10 files changed

+197
-83
lines changed

10 files changed

+197
-83
lines changed

lib/controller/index.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import 'package:efox_flutter/store/index.dart' show Store, ConfigModel, UserModel;
1+
import 'package:efox_flutter/store/index.dart'
2+
show Store, ConfigModel, UserModel;
3+
4+
import 'package:efox_flutter/utils/appVersion.dart' show AppVersion;
25

36
void initState() {
47
// 获取版本号
58
Store.valueNotCtx<ConfigModel>().$getAppVersion();
69
// 登录
710
Store.valueNotCtx<UserModel>().$getUserInfo();
8-
}
11+
Future.delayed(Duration(seconds: 3), () {
12+
AppVersion().check(Store.widgetCtx);
13+
});
14+
}

lib/http/index.dart

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import 'package:dio/dio.dart'
1010
import 'package:efox_flutter/utils/localStorage.dart' show LocalStorage;
1111
import 'log.dart' show log;
1212
import 'package:efox_flutter/store/index.dart' show Store;
13-
import 'package:flutter/material.dart';
14-
import 'loadingDialog.dart' show showAppLoading;
13+
import 'loadingDialog.dart' as AppLoading;
1514

16-
Dio getDio([Options options]) {
15+
Dio getDio({options, loading}) {
1716
if (options == null) {
1817
options = Options(
1918
headers: {
@@ -35,9 +34,7 @@ Dio getDio([Options options]) {
3534
if (options.headers['Authorization'] == null && token != null) {
3635
options.headers['Authorization'] = 'token $token';
3736
}
38-
print(
39-
' Store.widgetCtx -------------------------------- ${Store.widgetCtx}');
40-
showAppLoading();
37+
await AppLoading.beforeRequest(options.uri, loading);
4138
log('【发送请求】 ${options.uri} ', '${options.headers} ${options.data}');
4239
// Do something before request is sent
4340
return options; //continue
@@ -49,11 +46,13 @@ Dio getDio([Options options]) {
4946
onResponse: (Response response) async {
5047
log('【请求成功】 ${response.request.uri},【状态码 ${response.statusCode}】',
5148
response);
52-
showAppLoading();
53-
return {'data': response.data}; // continue
49+
return Future.delayed(Duration(seconds: 3), () async {
50+
await AppLoading.afterResponse(response.request.uri, loading);
51+
return {'data': response.data}; // continue
52+
});
5453
},
5554
onError: (DioError e) async {
56-
// showAppLoading();
55+
await AppLoading.afterResponse(e.request.uri, loading);
5756
dynamic msg = e.message;
5857
dynamic code = 0; // 错误码
5958
dynamic status = e.type; // http请求状态
@@ -67,16 +66,20 @@ Dio getDio([Options options]) {
6766
},
6867
));
6968
dio.interceptors.add(LogInterceptor(responseBody: false)); //开启请求日志
70-
69+
7170
return dio;
7271
}
7372

74-
Future<dynamic> get({url, data = const {}}) async {
75-
return getDio().get(url).then((resp) => resp.data);
73+
Future<dynamic> get({url, data = const {}, options, loading}) async {
74+
return getDio(options: options, loading: loading ?? Map())
75+
.get(url)
76+
.then((resp) => resp.data);
7677
}
7778

78-
Future post({url, data = const {}, options}) async {
79-
return getDio(options).post(url, data: data).then((resp) {
79+
Future post({url, data = const {}, options, loading}) async {
80+
return getDio(options: options, loading: loading ?? Map())
81+
.post(url, data: data)
82+
.then((resp) {
8083
return resp.data;
8184
});
8285
}

lib/http/loading2.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+
var count = 0;
6+
/**
7+
* loading: 可配置参数
8+
* requestOrComplete: 是否发送请求或已完成 true表示发送请求需要开启loading,false表示完成请求可关闭loading
9+
*/
10+
void showAppLoading(loading, requestOrComplete) {
11+
// showDialog(
12+
// context: Store.widgetCtx,
13+
// builder: (context) {
14+
// return Center(
15+
// child: CircularProgressIndicator(
16+
// // value: 0.5,
17+
// semanticsLabel: 'hsh',
18+
// semanticsValue: 'sds',
19+
// ),
20+
// );
21+
// });
22+
if (requestOrComplete) {
23+
++count;
24+
} else {
25+
--count;
26+
}
27+
if (count >= 1 && overlayEntry?.maintainState == true) return;
28+
if (overlayEntry?.maintainState == true &&
29+
requestOrComplete == false &&
30+
count == 0) {
31+
print('移除啦~~~');
32+
overlayEntry.maintainState = false;
33+
overlayEntry.remove();
34+
return;
35+
}
36+
// var overlayState = Overlay.of(Store.widgetCtx);
37+
38+
Navigator.of(Store.widgetCtx).push(MaterialPageRoute(builder: (context) {
39+
return Scaffold(
40+
backgroundColor: Colors.transparent,
41+
// body: OverlayEntry(),
42+
);
43+
}));
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/loadingDialog.dart

Lines changed: 103 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,119 @@
11
import 'package:flutter/material.dart';
22
import 'package:efox_flutter/store/index.dart' show Store;
33

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;
4+
bool loading = false;
5+
Set dict = Set();
6+
7+
void beforeRequest(uri, Map<dynamic, dynamic> options) {
8+
dict.add(uri);
9+
if (loading == false) {
10+
showAppLoading(options);
11+
loading = true;
12+
}
13+
}
14+
15+
void afterResponse(uri, Map<dynamic, dynamic> options) {
16+
dict.remove(uri);
17+
if (dict.length == 0 && loading == true) {
18+
Navigator.of(Store.widgetCtx, rootNavigator: true).pop('close dialog');
19+
loading = false;
2220
}
23-
var overlayState = Overlay.of(Store.widgetCtx);
24-
overlayEntry = new OverlayEntry(builder: (context) {
21+
}
22+
23+
/**
24+
* loading: 可配置参数
25+
* requestOrComplete: 是否发送请求或已完成 true表示发送请求需要开启loading,false表示完成请求可关闭loading
26+
*/
27+
void showAppLoading(Map<dynamic, dynamic> options) {
28+
options = {
29+
'notLoading': options['notLoading'] ?? false,
30+
'text': options['text'] ?? 'loading...'
31+
};
32+
showDialog(
33+
context: Store.widgetCtx,
34+
builder: (context) {
35+
return LoadingDialog(text: options['text']);
36+
},
37+
);
38+
}
39+
40+
class LoadingDialog extends StatefulWidget {
41+
final String text;
42+
LoadingDialog({Key key, @required this.text}) : super(key: key);
43+
44+
@override
45+
LoadingDialogState createState() => LoadingDialogState();
46+
}
47+
48+
class LoadingDialogState extends State<LoadingDialog> {
49+
@override
50+
Widget build(BuildContext context) {
2551
return Scaffold(
2652
backgroundColor: Colors.transparent,
27-
body: GestureDetector(
53+
body: Center(
2854
child: Column(
29-
crossAxisAlignment: CrossAxisAlignment.center,
3055
mainAxisAlignment: MainAxisAlignment.center,
31-
children: <Widget>[CircularProgressIndicator(), Text('loading...')],
56+
children: <Widget>[
57+
SizedBox(
58+
child: CircularProgressIndicator(
59+
strokeWidth: 5,
60+
),
61+
height: 50,
62+
width: 50,
63+
),
64+
Divider(
65+
height: 10,
66+
),
67+
widget.text != null
68+
? Text(
69+
widget.text,
70+
style: TextStyle(
71+
color: Theme.of(context).primaryTextTheme.title.color),
72+
)
73+
: ''
74+
],
3275
),
33-
onTap: () {
34-
print('移除啦~~~ ${overlayEntry.maintainState}');
35-
overlayEntry.maintainState = false;
36-
overlayEntry?.remove();
37-
print('overlayEntry ${overlayEntry}');
38-
},
3976
),
4077
);
41-
});
42-
// 加载框
43-
overlayState.insert(overlayEntry);
44-
overlayEntry.maintainState = true;
78+
}
4579

46-
// Navigator.of(Store.widgetCtx).push(MaterialPageRoute(builder: (context) {
47-
// return Index();
48-
// }));
80+
@override
81+
void dispose() {
82+
super.dispose();
83+
loading = false;
84+
dict.clear();
85+
}
4986
}
5087

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...')],
88+
// var overlayState = Overlay.of(Store.widgetCtx);
89+
// overlayEntry = new OverlayEntry(builder: (context) {
90+
// return Scaffold(
91+
// backgroundColor: Colors.transparent,
92+
// body: SizedBox.expand(
93+
// child: Container(
94+
// decoration: BoxDecoration(
95+
// color: Color.fromARGB(100, 255, 255, 255),
96+
// ),
97+
// child: GestureDetector(
98+
// child: Column(
99+
// crossAxisAlignment: CrossAxisAlignment.center,
100+
// mainAxisAlignment: MainAxisAlignment.center,
101+
// children: <Widget>[
102+
// CircularProgressIndicator(),
103+
// Text('loading...')
104+
// ],
105+
// ),
106+
// onTap: () {
107+
// print('移除啦~~~ ${overlayEntry.maintainState}');
108+
// overlayEntry?.remove();
109+
// loading = false;
110+
// print('overlayEntry ${overlayEntry}');
111+
// },
60112
// ),
61113
// ),
62-
// );
63-
// }
64-
// }
114+
// ),
115+
// );
116+
// });
117+
// 加载框
118+
// overlayState.insert(overlayEntry);
119+
// overlayEntry.maintainState = true;

lib/main.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class MainApp extends StatefulWidget {
1717
@override
1818
MainAppState createState() => MainAppState();
1919
}
20-
2120
class MainAppState extends State<MainApp> {
2221
// 定义全局 语言代理
2322
AppLocalizationsDelegate _delegate;

lib/page/component/tabs.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,10 @@ class _IndexState extends State<Index>
123123
],
124124
),
125125
onPressed: () {
126-
// FluroRouter.router.navigateTo(
127-
// context,
128-
// nameSpaces + _tmpWidgetList[index].title,
129-
// );
130-
131-
Scaffold.of(context).showSnackBar(SnackBar(
132-
content: Text('haha'),
133-
));
126+
FluroRouter.router.navigateTo(
127+
context,
128+
nameSpaces + _tmpWidgetList[index].title,
129+
);
134130
},
135131
),
136132
);

lib/page/home.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'component/tabs.dart' as TabIndex;
66
import 'mine/index.dart' as MyIndex;
77
import 'app-login/index.dart' as LoginIndex;
88

9-
import 'package:efox_flutter/utils/appVersion.dart' show AppVersion;
109
import 'package:efox_flutter/store/index.dart' show Store, UserModel;
1110

1211
class Index extends StatefulWidget {
@@ -21,10 +20,7 @@ class _IndexState extends State<Index> {
2120
void initState() {
2221
super.initState();
2322
_pageController = PageController();
24-
print('==============home context $context');
25-
// Store.setContext(context);
2623
Controller.initState();
27-
AppVersion().check(context);
2824
}
2925

3026
@override

lib/router/controller.dart

Whitespace-only changes.

lib/store/index.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,20 @@ class Store {
3535
* 设置数据层上下文
3636
*/
3737
static setStoreCtx(context) {
38-
print('setStoreCtx ============== $storeCtx ');
3938
storeCtx = context;
40-
print('context====================== $storeCtx');
4139
}
4240

4341
/**
4442
* 设置Widget上下文
4543
*/
4644
static setWidgetCtx(context) {
47-
print('setWidgetCtx ============== $storeCtx ');
4845
widgetCtx = context;
49-
print('setWidgetCtx ====================== $storeCtx');
5046
}
5147

5248
/**
5349
* 获取
5450
*/
5551
static T valueNotCtx<T>() {
56-
print('============valueNotCtx $storeCtx ');
5752
return Provide.value<T>(storeCtx);
5853
}
5954

lib/store/models/user_model.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class UserModel with ChangeNotifier {
1313
* 登录控制
1414
*/
1515
Future $loginController(context, payload) async {
16-
dynamic result = $login(payload);
16+
dynamic result = await $login(payload);
1717
if (result == true) {
1818
Scaffold.of(context).showSnackBar(new SnackBar(
1919
content: new Text('登录成功'),

0 commit comments

Comments
 (0)