|
1 | 1 | import 'package:flutter/material.dart'; |
2 | 2 | import 'package:efox_flutter/store/index.dart' show Store; |
3 | 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; |
| 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; |
22 | 20 | } |
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) { |
25 | 51 | return Scaffold( |
26 | 52 | backgroundColor: Colors.transparent, |
27 | | - body: GestureDetector( |
| 53 | + body: Center( |
28 | 54 | child: Column( |
29 | | - crossAxisAlignment: CrossAxisAlignment.center, |
30 | 55 | 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 | + ], |
32 | 75 | ), |
33 | | - onTap: () { |
34 | | - print('移除啦~~~ ${overlayEntry.maintainState}'); |
35 | | - overlayEntry.maintainState = false; |
36 | | - overlayEntry?.remove(); |
37 | | - print('overlayEntry ${overlayEntry}'); |
38 | | - }, |
39 | 76 | ), |
40 | 77 | ); |
41 | | - }); |
42 | | - // 加载框 |
43 | | - overlayState.insert(overlayEntry); |
44 | | - overlayEntry.maintainState = true; |
| 78 | + } |
45 | 79 |
|
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 | + } |
49 | 86 | } |
50 | 87 |
|
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 | +// }, |
60 | 112 | // ), |
61 | 113 | // ), |
62 | | -// ); |
63 | | -// } |
64 | | -// } |
| 114 | +// ), |
| 115 | +// ); |
| 116 | +// }); |
| 117 | +// 加载框 |
| 118 | +// overlayState.insert(overlayEntry); |
| 119 | +// overlayEntry.maintainState = true; |
0 commit comments