Skip to content

Commit 7981b6e

Browse files
authored
Merge pull request #12 from MMMzq/dev
Dev
2 parents faaa661 + d5774f4 commit 7981b6e

File tree

8 files changed

+72
-45
lines changed

8 files changed

+72
-45
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
## [2.1.0]
2+
* bug fix: see [#11](https://github.com/MMMzq/bot_toast/issues/11)
3+
4+
* 移除`BotToastInit``key`参数
5+
6+
* `BotToast.init`方法变为私有方法不再公开
7+
8+
* 重构了初始化的方式
9+
10+
* Remove the `key` parameter of `BotToastInit`
11+
12+
* `BotToast.init` method becomes private and no longer public
13+
14+
* Refactored the way to initialize
15+
16+
117
## [2.0.0+2]
218
* Update document
319

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Loading|Text|CustomWidget
5151
#### 1. add dependencies into you project pubspec.yaml file
5252
``` dart
5353
dependencies:
54-
bot_toast: ^2.0.0
54+
bot_toast: ^2.1.0
5555
```
5656

5757
#### 2. import BotToast lib

README_zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Loading|Text|CustomWidget
5555
#### 1. pubspec.yaml文件里添加依赖
5656
``` dart
5757
dependencies:
58-
bot_toast: ^2.0.0
58+
bot_toast: ^2.1.0
5959
```
6060

6161
#### 2. 导入BotToast库

lib/bot_toast.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library bot_toast;
22

33
export 'src/basis.dart';
4-
export 'src/bot_toast_init.dart';
4+
export 'src/bot_toast_init.dart' show BotToastInit;
55
export 'src/toast.dart';
66
export 'src/toast_navigator_observer.dart';

lib/src/bot_toast_init.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
import 'package:flutter/material.dart';
22

3-
import 'toast.dart';
3+
final GlobalKey<_BotToastInitState> botToastInitKey =
4+
GlobalKey<_BotToastInitState>();
45

56
class BotToastInit extends StatefulWidget {
6-
77
final Widget child;
88

9-
const BotToastInit({Key key, this.child}) : super(key: key);
9+
BotToastInit({@required this.child})
10+
: assert(child != null),
11+
super(key: botToastInitKey);
1012

1113
@override
1214
_BotToastInitState createState() => _BotToastInitState();
1315
}
1416

1517
class _BotToastInitState extends State<BotToastInit> {
18+
bool _needInit;
19+
20+
bool get needInit => _needInit;
21+
22+
void reset() {
23+
_needInit = false;
24+
}
25+
1626
@override
1727
Widget build(BuildContext context) {
28+
_needInit = true;
1829
return widget.child;
1930
}
2031

2132
@override
2233
void initState() {
23-
BotToast.init(context);
34+
_needInit = true;
2435
super.initState();
2536
}
2637

2738
@override
2839
void didUpdateWidget(BotToastInit oldWidget) {
29-
BotToast.init(context);
40+
_needInit = true;
3041
super.didUpdateWidget(oldWidget);
3142
}
3243
}

lib/src/toast.dart

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:ui';
44
import 'package:flutter/material.dart';
55
import 'package:flutter/scheduler.dart';
66
import 'basis.dart';
7+
import 'bot_toast_init.dart';
78
import 'bot_toast_manager.dart';
89
import 'key_board_safe_area.dart';
910
import 'toast_navigator_observer.dart';
@@ -42,6 +43,7 @@ class BotToast {
4243
static const String loadKey = '_loadKey';
4344
static const String attachedKey = '_attachedKey';
4445
static const String defaultKey = '_defaultKey';
46+
static Completer<void> _initCompleter;
4547

4648
static final Map<String, List<CancelFunc>> cacheCancelFunc = {
4749
textKey: [],
@@ -51,27 +53,34 @@ class BotToast {
5153
defaultKey: [],
5254
};
5355

54-
///一般这种情况只会出现在pop所有路由再推一个路由出现,同时根[MaterialApp.navigatorKey]改变就会出现问题,所以只能手动reInit
55-
static void init(BuildContext context) {
56+
57+
static void _init(BuildContext context) {
5658
assert(BotToastNavigatorObserver.debugInitialization, """
5759
Please initialize properly!
5860
Example:
5961
BotToastInit(
6062
child: MaterialApp(
6163
title: 'BotToast Demo',
6264
navigatorObservers: [BotToastNavigatorObserver()],
63-
home: EnterPage()
65+
home: XxxPage()
6466
),
6567
);
6668
""");
69+
_initCompleter=Completer<void>();
6770
_safeRun(() {
6871
void visitor(Element element) {
72+
assert((){
73+
if(element.widget is Localizations && ((element as StatefulElement).state as dynamic).locale==null){
74+
return false;
75+
}
76+
return true;
77+
}(), 'Initialization error : locale==null');
6978
if (element.widget is Navigator) {
7079
if (_navigatorState == null ||
7180
_managerState.currentState == null ||
7281
_navigatorState != (element as StatefulElement).state) {
7382
_navigatorState = (element as StatefulElement).state;
74-
_init();
83+
_doInit();
7584
}
7685
} else {
7786
element.visitChildElements(visitor);
@@ -88,15 +97,16 @@ class BotToast {
8897
BotToastInit(
8998
child: MaterialApp(
9099
navigatorObservers: [BotToastNavigatorObserver()],
91-
home: EnterPage(),
100+
home: XxxPage(),
92101
),
93102
);
94103
''');
104+
_initCompleter.complete();
95105
});
96106
}
97107

98108
///这里需要监听didPush是因为,当Navigator的Route集合为空再推一个Route会导致这个页面覆盖_BotToastManager上面,挡住了Toast,因此要手动移动到最后
99-
static void _init() {
109+
static void _doInit() {
100110
_managerState = GlobalKey<BotToastManagerState>();
101111
BotToastNavigatorObserverProxy observerProxy;
102112
final overlayEntry = OverlayEntry(
@@ -805,39 +815,38 @@ class BotToast {
805815
final CancelFunc cancelFunc = () {
806816
remove(uniqueKey, gk);
807817
};
808-
_safeRun(() {
809-
/*
810-
如果currentState为空说明此时BotToast还没初始化完成,此时的状态是处理showWidget和init方法都是是在同一帧里,
811-
因此要把showWidget方法放在下一帧处理
812-
*/
813-
if (_managerState.currentState == null) {
814-
_safeRun(() {
815-
_managerState.currentState
816-
.insert(gk, uniqueKey, toastBuilder(cancelFunc));
817-
});
818-
} else {
818+
()async{
819+
assert(botToastInitKey.currentState !=
820+
null, 'Please wait for BotToastInit to be attached to the Widget tree and then call');
821+
if(botToastInitKey.currentState.needInit){
822+
botToastInitKey.currentState.reset();
823+
_init(botToastInitKey.currentContext);
824+
assert(!_initCompleter.isCompleted);
825+
}
826+
await _initCompleter.future;
827+
_safeRun(() {
819828
_managerState.currentState
820829
.insert(gk, uniqueKey, toastBuilder(cancelFunc));
821-
}
822-
});
830+
});
831+
}();
823832
return cancelFunc;
824833
}
825834

826835
static void remove(UniqueKey key, [String groupKey]) {
827836
_safeRun(() {
828-
_managerState.currentState.remove(groupKey ?? defaultKey, key);
837+
_managerState.currentState?.remove(groupKey ?? defaultKey, key);
829838
});
830839
}
831840

832841
static void removeAll([String groupKey]) {
833842
_safeRun(() {
834-
_managerState.currentState.removeAll(groupKey ?? defaultKey);
843+
_managerState.currentState?.removeAll(groupKey ?? defaultKey);
835844
});
836845
}
837846

838847
static void cleanAll() {
839848
_safeRun(() {
840-
_managerState.currentState.cleanAll();
849+
_managerState.currentState?.cleanAll();
841850
});
842851
}
843852

lib/src/toast_navigator_observer.dart

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ class BotToastNavigatorObserverProxy {
1818
}
1919

2020
///如果你项目有多个[Navigator],请将该BotToastNavigatorObserver添加到[Navigator.observers]
21-
///可以参考[BotToast.init]方法添加
22-
///或者在创建[Navigator]时直接添加
23-
/// Example:
24-
/// MaterialApp(
25-
/// navigatorObservers: [BotToastNavigatorObserver()],
26-
/// title: 'Flutter Demo',
27-
/// home: ToastWidget(
28-
/// child: XxxxPage(title: 'Flutter Demo Home Page'),
29-
/// ),
30-
/// );
3121
class BotToastNavigatorObserver extends NavigatorObserver {
3222
static final List<BotToastNavigatorObserverProxy> _leavePageCallbacks = [];
3323

@@ -44,11 +34,12 @@ class BotToastNavigatorObserver extends NavigatorObserver {
4434
assert(debugInitialization, """
4535
Please initialize!
4636
Example:
47-
MaterialApp(
48-
title: 'BotToast Demo',
37+
BotToastInit(
38+
child:MaterialApp(
39+
title: 'Xxxx Demo',
4940
navigatorObservers: [BotToastNavigatorObserver()],
50-
home: BotToastInit(child: EnterPage()),
51-
);
41+
home: XxxxPage(),
42+
));
5243
""");
5344
assert(botToastNavigatorObserverProxy != null);
5445
_leavePageCallbacks.add(botToastNavigatorObserverProxy);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: MMMzq<vivask770@163.com>
44
homepage: https://github.com/MMMzq/bot_toast
55
email: vivask770@163.com
66

7-
version: 2.0.0+2
7+
version: 2.1.0
88

99
environment:
1010
sdk: ">=2.1.0 <3.0.0"

0 commit comments

Comments
 (0)